Re: Advice for iOS web app with live feed
Re: Advice for iOS web app with live feed
- Subject: Re: Advice for iOS web app with live feed
- From: Jens Alfke <email@hidden>
- Date: Wed, 14 Jan 2015 11:51:54 -0800
> On Jan 14, 2015, at 9:05 AM, Steve Mills <email@hidden> wrote:
>
> I have an opportunity to branch out to new areas (yay!) that I've never had the chance to work with before, an iOS app that acts as the frontend for a web site, receiving and sending Facebook- or Twitter-like statuses/updates. The backend and web guys on the project have mentioned using AJAX.
AJAX is a browser technology; it's basically the browser/JavaScript equivalent of NSURLConnection, i.e. letting scripts send their own requests back to the server without having to reload the page. That term isn't relevant to a native app.
What you want is a type of continuous feed: you open a connection to the server, probably using NSURLConnection, then the server keeps the connection open and sends you a message whenever something happens. It's still HTTP, but it's sort of an indefinitely-long response that gets sent a line at a time.
This is quite easy to implement with NSURLConnection or NSURLSession. Your data delegate will be called whenever the server sends something. The gotcha to avoid, though, is assuming that one callback equals one message; that's not true. The data from the server will get broken up in various ways during transport that don't correspond to message boundaries, so one callback might include a partial message or multiple message; don't assume it begins or ends on a message boundary.
The other major important details is that even though the connection is supposed to stay open indefinitely, you have to be prepared for it to be closed. This might happen because the server decides to kick you off (maybe it's got too many clients), or an intermediate load-balancer or proxy times out the connection, or there's just some network problem that kills the socket. If that happens you should try to re-open the connection after a brief delay.
Some things to look at:
- This technique used to be called COMET but I'm not sure that term is still widely used.
- The "continuous" mode of CouchDB's _changes feed works exactly like this:
http://docs.couchdb.org/en/latest/api/database/changes.html#continuous <http://docs.couchdb.org/en/latest/api/database/changes.html#continuous>
- EventSource is a fairly new browser JS API for subscribing to these kinds of feeds. That's not relevant for a native app, but the data format sent by the server is interesting:
https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events#Event_stream_format <https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events#Event_stream_format>
—Jens
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden