views:

742

answers:

4

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.

+11  A: 

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 on 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.

zakovyrya
Surely this isn't the correct response.
DevDevDev
By that I mean if I have a web service the iPhone needs to get data from, I need to use Comet? But how to do this without using Javascript libraries?
DevDevDev
No, you don't need JavaScript. Comet is just an idea. The simplest implementation would be an NSURLConnection with a very long timeout running asynchronously or in a non-UI thread. The connection should stay open until the server has data, in which case it should send the data and close the connection, and you'll get it. Then immediately open a new connection to the server and start waiting.
Sidnicious
@SidneySM: Actually, you don't have to close connection after you get your data and open another one. It's too resource consuming. You receive chunk of data, process it (let's say it's complete serialized JSON data structure), and wait for another one. Of course, you better still close connection after certain period and open a new one just to avoid memory leaks
zakovyrya
@zakovyrya can you give me a clear example of this?
DevDevDev
A: 

For Web Service Push, aka Comet you may want to checkout this other StackOverflow question here.

Nate Bross
+1  A: 

This may have been mentioned in the other post, but http://code.google.com/p/istreamlight/ is a good place to check out. It uses the lightstreamer server (don't know about it.) You might take a look to see if this is how you would like to implement a comet client for your application.

I honestly wish I could be more helpful. If you were asking for a way to do this in javascript, I'd be all over it!

Good luck,

-Todd

fauxtrot
Yup tehre is tons of info out there for doing this in JS. Thanks for the link I will look into it.
DevDevDev
+3  A: 

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.

Andrew Matthews
I've been very impressed with the WebSync (been using their server express version, not on demand) software so far. The documentation is just a tad thin at the moment, but they monitor their google groups community such that I've never really had to wait more than a few hours to get a response to a question.
Matt