views:

866

answers:

5

I'm working on an iPhone application which will use long-polling to send event notifications from the server to the client over HTTP. After opening a connection on the server I'm sending small bits of JSON that represent events, as they occur. I am finding that -[NSURLConnectionDelegate connection:didReceiveData] is not being called until after I close the connection, regardless of the cache settings I use when creating the NSURLRequest. I've verified that the server end is working as expected - the first JSON event will be sent immediately, and subsequent events will be sent over the wire as they occur. Is there a way to use NSURLConnection to receive these events as they occur, or will I need to instead drop down to the CFSocket API?

I'm starting to work on integrating CocoaAsyncSocket, but would prefer to continue using NSURLConnection if possible as it fits much better with the rest of my REST/JSON-based web service structure.

A: 

It sounds as if you need to flush the socket on the server-side, although it's really difficult to say for sure. If you can't easily change the server to do that, then it may help to sniff the network connection to see when stuff is actually getting sent from the server.

You can use a tool like Wireshark to sniff your network.

Another option for seeing what's getting sent/received to/from the phone is described in the following article:

http://blog.jerodsanto.net/2009/06/sniff-your-iphones-network-traffic/

Good luck!

Allen Hadden
I can connect via telnet and verify that the data is coming down incrementally, as expected. It sounds like NSURLConnection always buffers the data as Matt mentioned.
pix0r
+2  A: 

NSURLConnection will buffer the data while it is downloading and give it all back to you in one chunk with the didReceiveData method. The NSURLConnection class can't tell the difference between network lag and an intentional split in the data.

You would either need to use a lower-level network API like CFSocket as you mention (you would have access to each byte as it comes in from the network interface, and could distinguish the two parts of your payload), or you could take a look at a library like CURL and see what types of output buffering/non-buffering there is there.

Matt Bridges
This is what I was afraid of. I think I'll go with CocoaAsyncSocket rather than deal with CFSocket myself.
pix0r
For the record, CocoaAsyncSocket ended up working great, though it took an extra hour or two to get everything working properly.
pix0r
Glad it worked out eventually!
Matt Bridges
A: 

We're currently doing some R&D to port our StreamLink comet libraries to the iPhone.

I have found that in the emulator you will start to get didReceiveData callbacks once 1KB of data is received. So you can send a junk 1KB block to start getting callbacks. It seems that on the device, however, this doesn't happen. In safari (on device) you need to send 2KB, but using NSURLConnection I too am getting no callbacks. Looks like I may have to take the same approach.

I might also play with multipart-replace and some other more novel headers and mime types to see if it helps stimulate NSURLConnection.

I'd really recommend using CocoaAsyncSocket instead - it's simple enough to implement the basic HTTP commands yourself if you need them. Who knows how Apple might change NSURLConnection in the future...
pix0r
+2  A: 

I ran into this today. I wrote my own class to handle this, which mimics the basic functionality of NSURLConnection.

http://github.com/nall/SZUtilities/blob/master/SZURLConnection.h

nall
A: 

There is another HTTP API Implementation named ASIHttpRequest. It doesn't have the problem stated above and provides a complete toolkit for almost every HTTP feature, including File Uploads, Cookies, Authentication, ...

http://allseeing-i.com/ASIHTTPRequest/

Ali Nadalizadeh
And unlike rolling it yourself ASIHttpRequest seems to handle HTTP proxies, authentication, etc, all of which you lose if you decide "hey, it's just a socket connection plus sending a few headers, how hard can it be?" :)One of the main reasons to use COMET over TCP or UDP is that it goes through fascist corporate firewalls.
Matthew Phillips