4

I am using SocketRocket, but I cannot get it to deliver messages while in the background. When I open the app again, it resumes the connection (without reconnecting) and the messages all come in at once.

Here is my connection code:

- (void)_reconnect {
    _websocket.delegate = nil;
    [_websocket close];

    NSString *host = @"ws://localhost:3030/primus";

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:host]];
    [request setValue:[NSString stringWithFormat:@"<%@>", apiKey] forHTTPHeaderField:@"Authentication"];

    _websocket = [[SRWebSocket alloc] initWithURLRequest:request];
    [_websocket setDelegateOperationQueue:[NSOperationQueue new]];
    _websocket.delegate = self;

    [_websocket open];

    if (DEBUG) {
        NSLog(@"Using: %@", host);
    }
}

I have also tried without the

[_websocket setDelegateOperationQueue:[NSOperationQueue new]];

line, but that doesn't help anything.

Do you have any idea what's going on?

Thanks!

Jason Silberman
  • 2,471
  • 6
  • 29
  • 47

2 Answers2

1

I'm facing the same problem, and still don't have a correct answer for it.

It seems that Socket Rocket stops sending messages to the assigned SRDelegate if the main thread is frozen (when the application is in background).

I've even tried the other method without success :

    [_webSocket setDelegateDispatchQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)];

However, I did find a workaround for keeping the main thread unfrozen. This isn't very nice, especially since it will lead to battery draining, but it works.

I've noticed that if your application has location updates while in background, the WebSocket continues to run just fine.

In order to start location services, you can do the following on your applicationDidEnterBackground :

CLLocationManager* locationManager = [[CLLocationManager alloc] init];
[locationManager startUpdatingLocation];

When coming back, you can stop them by calling the following code on your applicationWillEnterForeground :

[locationManger stopUpdatingLocation];

Ofc you will need to keep a strong reference to locationManager not just in the scope.

This is not by far a recommended solution, but if nothing works ...

MichaelCMS
  • 4,703
  • 2
  • 23
  • 29
0

Hi just stated working on the same issue. New version of socket rocket can help to resolve this problem. Some background modes are now supported. See source code.

I didn't test that yet but I'm sure this should help (make things one step closer to final solution).

Marek R
  • 32,568
  • 6
  • 55
  • 140
  • i am using latest version of socket rocket and i am still facing the issue. using '0.4.2' . FYI https://github.com/facebook/SocketRocket/issues/479 – Ashok R Apr 18 '17 at 08:31
  • for me it worked. If I remember correctly since that time Apple has deprecated VoIP flag on sockets so currently it may not work. Based on my experience this was good Apple decision, since it was easy to achieve state when application was killed by system since it was waken up to often. This happen when server was implemented incorrectly or when mutiple sockets with VoiP flag where used (in this case synchronization of servers would be needed). The only choice is to use push notifications. – Marek R Apr 18 '17 at 13:59