Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chat app synchronization on background in IOS

I have a chat application developed by JS. I want to send PING to server once in a while. Its not a problem if app runs on fore ground. The problem is when user minimizes it or open another app. My app looses its focus and gets into suspended state.

I have following two use-cases.

  1. To keep the chat session open I need to send PING to server (Its an IRC server) every X minutes even the app runs in background.
  2. We also need to check for new messages (by ajax on a local http server) and add a local notification to the notification queue so when user clicks on it app can resume

I have found apple does not allow running apps in the background. if they allow they require special permission. I found some apps does it by requesting finite length execution time. What is the best way to get highest possible background execution time? As a chat app can I request permission for voip, location or any other way ?

Note: the app will be running in an environment where there is no Internet. Hence push notification will not work here.

Update: After doing a lot searching I found background fetch. It seem background fetch will suite it. But still the problem remains, its not called in a timely manner.

like image 586
Shiplu Mokaddim Avatar asked Jan 12 '14 18:01

Shiplu Mokaddim


2 Answers

This sounds like an interesting problem. From reading the various comments, it sounds like you want this to work when you're on a local network - so you have wifi, but the wifi router/base station isn't connected to the actual internet?

Because background refresh isn't going to be predictable - you'll never know when it is going to update - you might want to get creative.

You could look into exploiting iOS VOIP support, only without the Voice! Apple has some tips on VOIP here. VOIP basically uses something called SIP (Session Initiation Protocol), which is signalling layer of the call, and a lot like HTTP. It's this SIP layer that you want to take advantage of.

This isn't going to be terribly easy, but it should be achievable. Setup your app to use VOIP, and then look into something like PJSip as your SIP library. Then, on your local network have a SIP Server (I'm sure there are plenty open source implementations) that you can register your iPhone against (so your server knows where your phone is, pretending to be a VOIP phone). This should work, because it doesn't need to go through Apple as far as I am aware... And will run happily on your local network.

Then, the server can send a message via SIP to the handset, as if it were instigating a VOIP session. You app is awoken, gets the messages - ideally from the SIP message if possible - and then just doesn't start the session. SIP was designed just for creating sessions, not just VOIP. When I worked in Telecoms R&D (a long time ago) we were using it to swap between Text/Voice/Video, all using local servers.

You'll have to jump a lot of hoops to make this work, but it would be pretty awesome. I have never tried this actual use case - especially with iOS, but I'm fairly sure it will work. It is a bit of a fudge, but should get you where you need to go.

Good luck!

like image 167
xmjw Avatar answered Oct 20 '22 04:10

xmjw


You can use something like PubNub to build this chat app with iOS using native Objective-C code, or with the Phonegap (Cordova) libs.

The beauty with using a real-time messaging network like PubNub is that when the app goes to the background, you can easily have the chat messages come in on APNS.

When the app is in the foreground, it can just receive them as the native (PubNub) message. And if it needs to "catch-up" with the messages it missed while in the background (but received via APNS), its trivial to implement.

Also, PubNub is platform agnostic -- so you can easily also use it on Web, Android, BB, Windows Phone, etc.

http://www.pubnub.com/blog/build-real-time-chat-10-lines-code/

http://www.pubnub.com/blog/html5-websockets-beautiful-real-time-chat-on-mobile-using-pubnubs-channel-presence/

https://github.com/pubnub/objective-c/tree/master/iOS

https://github.com/pubnub/javascript/tree/master/phonegap

geremy

like image 39
Geremy Avatar answered Oct 20 '22 04:10

Geremy