Recommended webkit alternative for server events on iOS

I want to receive Server-Sent events in my native iOS application, but do not use webkit / Safari. From what I found, NSURLConnection is poorly adapted as it will respond to pieces . I also looked at the ZTWebSocket library (obviously good, but I'm looking for SSE, not web sockets). Will CocoaAsyncSocket match? Or limited to pure TCP socket communication?

I have a suspicious suspicion that I am missing something obvious, or there is already a library or sample for this. Thanks in advance.

+4
source share
3 answers

After some more research on this subject, I believe that the best way to implement events sent by the server on iOS without WebKit is to use the custom NSConnection / NSRequest toolkit. I settled on ASIHTTPRequest . This library allows you to explicitly control the persistence attribute on the connection object (significant), manage data as it flows, save responses (for example, in local files), etc.

Not to mention the fact that it contains many other convenient extensions / settings in the field of network interaction (an improved reachability observer, a simplified API for async, a queue function, even the ability to load entire web pages (CSS, js and all).

On the server side, I use cyclone-sse (tornado) and nginx (as a reverse proxy). Interestingly enough, now I see that my SSEs are simulated simultaneously with both my iOS simulator and the browser subscriber. Cyclone even handles all the connections and gives me an API that supports a simple POST for push messages (also supports AMQP and Redit) ...

In short, ASIHTTPRequest was the perfect solution for me.

+2
source

SSE is HTTP technology in the sense that it is associated with an open HTTP connection. CocoaAsyncSocket are raw TCP / UDP sockets and don't know anything about HTTP. So no, CocoaAsyncSocket will not give you SSE, as you suspected.

I don't know of any standalone SSE implementation (in the spirit of standalone Websocket implementations) that you are probably looking for. But I don’t know whether this will make sense at all, since the SSE sends messages in the form of DOM events, which are most reasonable in the context of HTML, as far as I can tell.

If all you want to achieve is sending messages to the iOS app and you are free to choose the technology, raw sockets will do it. But Websockets are likely to meet your needs, depending on what you want. Take a look at SocketRocket .

+2
source

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

The API is very simple. He is currently using NSURLConnection.

0
source

Source: https://habr.com/ru/post/949492/


All Articles