Objective-c – Persistent Connection to Web Server (Like AJAX on Web)


I am wanting to create a program that talks with a Cometd server to allow for pushing of data to the app.

I have done this on the web side using AJAX, but I am a little unsure of the best way to do this with Cocoa.

I can make a standard connection using NSURLRequest and NSURLConnection, but how do I keep this connection alive so I can send data when needed and get the pushed info when needed.

Am I even going about this the correct way?

Thanks in advance

Best Solution

In terms of push notifications, if the http server does not close the close the connection the the NSURLConnection will stay open, and you will keep getting data. Note that if you are designing something like that you must use the asynchronous NSURLConnection methods, as a synchronous connection will not end until the server closes the connection.

As for sending more data, it is really not designed to do that. If you want to push more data in a single http request after you have sent it (which seems like a pretty bad idea to me) you are going to have to roll your http stack of find some opensource component you can use.

Note that NSURLConnection will use keep alive and other things as it deems appropriate, so if you start multiple logical connections to the same host in your app they may end up on the wire using the same keep alive connection, etc.