I would like to receive Server-Sent Events in my native iOS app however am not using webkit/Safari. From what I've found, NSURLConnection is poor fit as it chunks response . I've also looked at ZTWebSocket library (obviously nice, but I'm seeking SSE, not web sockets). Would CocoaAsyncSocket be appropriate? Or limited to pure TCP Socket communication?
I have a sneaking suspicion that I am missing something obvious, or there'd be a library or sample for this already. Thanks in advance.
SSE is a HTTP technology in the sense that it is bound to an open HTTP connection. CocoaAsyncSocket are raw TCP/UDP sockets and do not know anything about HTTP. So no, CocoaAsyncSocket won't give you SSE, as you suspected.
I don't know of any standalone implementation of SSE (in the spirit of standalone Websocket implementations), which is maybe what you are searching for. But I don't know either whether that would make sense at all, since SSE is sending messages in form of DOM-events which are most sensible in the context of HTML, as far as I can see.
If all you want to achieve is sending messages to your iOS app and you are free in the choice of technology, raw sockets would do. But Websockets more likely could suit your needs, depending on what you want. Take a look at SocketRocket.
After some more research on this, it's my opinion that the best way to implement Server Sent Events on iOS without WebKit is to use a customized NSConnection/NSRequest toolset. I settled on ASIHTTPRequest . This library allows you to explicitly control persistence attribute on connection object (essential), control data as it is received over the stream, store responses (e.g. in local files), etc.
Not to mention it contains lots of other handy extensions/customizations in the realm of networking (improved Reachability observer, a simplified API for async, a queuing feature, even ability to load entire web pages (CSS, js and all).
On the server side, I'm using cyclone-sse (tornado) and nginx (as a reverse proxy). Pretty exciting, now I can see my SSEs pushed simulataneously to both my iOS simulator and a browser subscriber. Cyclone even handles all the connections and gives me an API which supports simple POST for message pushes (also supports AMQP and Redit)...
Long story short, ASIHTTPRequest was a perfect solution for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With