facebookincubator/SocketRocket

Connection pausing while in background mode

jasonsilberman opened this issue · 14 comments

I am running iOS 7, and once my app leaves the foreground, messages are being "passed" and not being passed through to the delegate. Then once I open the app, all the data comes through at once.

Does anyone know what is happening?

Thanks!

This is expected behavior no? Give Multitasking in iOS 7 a quick read - I don't think the WebSocket as configured supports Background Fetch either.

Yes, but is it possible to make it so it does my pause?

Hi Jason,

As, you are using asynchronous mode of network connection, and app is
moving in background mode, so asynchronous callback is not working. To make
it work either you can manage the current run loop (CFRunLoop) for
multitasking or it will be better if you can make sendSynchronousRequest in
background thread. Just try to make call in
dispatch_synch("backgroundREquest", block) and in block make synchronous
request using NSURLConnection's sendSynchronousRequest API .

On Sunday, March 9, 2014, Jason Silberman notifications@github.com wrote:

I am running iOS 7, and once my app leaves the foreground, messages are
being "passed" and not being passed through to the delegate. Then once I
open the app, all the data comes through at once.

Does anyone know what is happening?

Thanks!

Reply to this email directly or view it on GitHubhttps://github.com//issues/152
.

Thanks & Regards
Ankit Thakur
+91-9654894439
iOS/NodeJS Developer
E-mail : ankitthakur85@gmail.com

I'm not quite sure how I would do this, do you think you could show a little demo?

Jason, can you share your code, I will try to convert it in background request. It is very helpful, if we can get the snippet of code, which you are trying to implement.

Here is my connection code:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:host]];

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

If you would like I can also post my message receiving code, but that is a bit more complex.

Thanks for the help!

The

[_websocket setDelegateOperationQueue:[NSOperationQueue new]];

line, was suggested here.

My goal is that if my app is recently backgrounded, and a message comes in, I would like to call this method. However it is not being called until my app is foregrounded.

- (void)setLocalNotificationForType:(SPKLocalNotificationType)notificationType fromUser:(NSString *)userName withMessageText:(NSString *)msgText {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"setLocalNotificationForType:fromUser:withMessageText:");

        if (! useNotifications) { return; }

        NSLog(@"setting notif...");

        UILocalNotification *notif = [UILocalNotification new];
        notif.userInfo = @{@"handle": _convoHandle, @"room": _convoName};
        notif.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
//            notif.fireDate = [[NSDate date] dateByAddingTimeInterval:10];
//            notif.timeZone = [NSTimeZone defaultTimeZone];

        if (notificationType == 1) { // message
            notif.alertBody = [NSString stringWithFormat:@"@%@ said: \"%@\"", userName, msgText];
        } else if (notificationType == 2) { // image
            notif.alertBody = [NSString stringWithFormat:@"@%@ sent an image", userName];
        } else {
            return;
        }

//          [[UIApplication sharedApplication] scheduleLocalNotification:notif];
        [[UIApplication sharedApplication] presentLocalNotificationNow:notif];
    });
}

Any update on this? Regardless on the delegate operation queue override, all the background modes enabled and reconnecting after entering in background mode, SocketRocket seems paused.

Looks like a work-around, which probably won't pass Apple's approval, is to enable "Voice over IP" background mode and add to SocketRocket:

[_inputStream setProperty:NSStreamNetworkServiceTypeVoIP forKey:NSStreamNetworkServiceType];

@nuthinking your solution works! Can we add an option to enable background work if your app is VOIP? Maybe you can read it from Info.plist and enable it? My app is really VOIP app and it would make my life easier if you can enable this option.

Thanks for pointing it out. I hope it will be accepted!

Doing some housekeeping. Please reopen if still relevant