Recommended alternative to webkit for server-sent events on iOS [closed]
Asked Answered
C

3

7

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.

Cords answered 17/10, 2012 at 18:24 Comment(1)
@ilmiacs raises an interesting aspect to this issue: DOM Events being a central piece to SSE consumption. As the little I do know about objective-c includes knowledge that there are scant few options for accessing DOM events in objective-C. I suppose one alternative for implementing SSE on iOS is to implement UIWebView (built-in support), then use javascript callback to objective-C function from within an iFrame. Like this. linkCords
C
3

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.

Cords answered 24/10, 2012 at 23:17 Comment(0)
P
3

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.

Perceivable answered 17/10, 2012 at 19:40 Comment(0)
C
3

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.

Cords answered 24/10, 2012 at 23:17 Comment(0)
R
0

Try this simple library which is written in Swift: https://github.com/hamin/eventsource.swift

The API is super simple. It uses NSURLConnection for now.

Riotous answered 28/11, 2015 at 13:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.