Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid iPhone constant polling a web-service?

I have an iPhone application that needs to be updated as soon as a change is made to the server. How can I have the server "push" data to the iphone rather than the iphone constantly polling the web service?

EDIT: I want th iPhone to receive JSON updates as soon as the server processes them, without having to request.

I suppose since the server is a web service that this is called Comet, but I haven't seen a good iPhone example yet.

like image 245
DevDevDev Avatar asked Sep 24 '09 07:09

DevDevDev


2 Answers

That depends on how adventurous you are. There are two alternatives here:

  1. Apple's Push Notifications that will work even when your app isn't running.
  2. Maintain TCP connection with your server yourself. Which requires more programming effort from you - low-level NSStream juggling and trying to handle cases when iPhone decides to go from 3G to WiFi. This can also eat up the battery pretty quickly. If you choose this path, socket streams programming guide can be the good place to start from.

UPDATE: Take a look at iStreamLight - Lightstreamer protocol implementation for iPhone. If it doesn't fit your Comet web-service, you probably need to go down to the lower level, which is maintaining TCP connection using socket streams. To simplify your task in handling JSON data structures, you might want to use JSON framework for Objective-C.

like image 120
zakovyrya Avatar answered Sep 19 '22 02:09

zakovyrya


What you need is some sort of COMET framework (such as light-streamer). There are a several ways to do that - socket connections or HTTP server that hold onto your polls until there is some data available to deliver or until the HTTP request times out. Other options include using plug-ins such like Flash or Silverlight/Moonlight (assuming such a thing were possible on the iphone using monotouch?)

A good new (IIS based) COMET framework that can move a lot of data in a very performant way is WebSync from Frozen Mountain, that supports a hosted COMET based PubSub framework (called WebSync on Demand) that can scale to suit your load. It works nicely via Javascript, and has a pretty clean API.

like image 33
Andrew Matthews Avatar answered Sep 19 '22 02:09

Andrew Matthews