3

I have a Cordova/PhoneGap app.

I'd like to have some semblance of real-time updates when the app is in the foreground.

What's the least resource-intensive way to accomplish this? Should I use socket.io, pushnotification plugins, or just make an API request every few seconds? What's the least taxing on both the device and the server?

opticon
  • 3,494
  • 5
  • 37
  • 61
  • using socket.io if you manage to will be best as per my suggestion, and making request every few seconds is worst approach among these – A.B Apr 24 '15 at 20:21
  • @A.B: I'm not disagreeing but how do you come to the conclusion that sockets are better than push notifications? – Roope Hakulinen Apr 24 '15 at 20:22
  • I guess in the case that the user denies permission to use push notifications this is a fallback that'll still allow real-time updates while the app is open? – opticon Apr 24 '15 at 20:23
  • since using JS in cordova and having JS background mostly people prefer to use it as they have already used it, @sanfor thats my view so i haven't added as an answer as i am not sure about other opinions :) – A.B Apr 24 '15 at 20:27

2 Answers2

7

For a mobile device, you have a classic tradeoff between battery usage, network usage and timeliness of updates.

The push notification service built into a mobile OS is designed to try to give you the best of all these tradeoffs and it runs globally rather than per app (which is usually more efficient), though it gives you somewhat less control over implementation details.

When comparing socket.io vs. polling an API, socket.io (and more specifically webSockets) were designed to be a more efficient way of getting asynchronous notifications from a server.

In socket.io, you create a socket connection to the server. That connection stays open for the duration of your app (in the foreground) and, at any time, the server can just send you data and you will receive it immediately when it was sent. Because connections can be lost and the endpoints are not necessarily notified of that immediately, socket.io uses small heartbeat packets that are sent on a regular basis between client and server. If the heartbeat packets stop being responded to, then socket.io assumes the connection has died and will close the original socket and attempt to create a new connection. It does all this transparently and automatically for you. This heartbeat, however, has some undesirable ramifications for mobile. The data sent is tiny so it's not really an issue of bandwidth usage, but every transmission from the mobile device uses battery and that can be relevant if left to run for a long time. The heartbeat interval in socket.io is configurable. It can be turned off (not recommended) or the time interval can be set to a longer time.

Both the OS push service and socket.io are very efficient from the server-end of things because the server only does work when there is something to actually send to the client and does not have to field regular requests where there is nothing to do.

The only possible advantage of polling here would be if your desired update interval was long (e.g. once an hour) or it isn't usually on and is only used occasionally. Then, you could just send an Ajax call each hour or upon demand and the server wouldn't have to do anything except answer the occasional Ajax call. If the desired interval is shorter, then you're probably going to want to use one of the true push mechanisms (either the OS push or socket.io).

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • great @jfriend00 , will socket.io works normally in phonegap(localhost file:// issue) like that in web apps (with node.js)? have faced trouble setting it up – A.B Apr 24 '15 at 20:41
  • @A.B - Some info on using socket.io with PhoneGap: [Socket.IO with Apache Cordova](http://socket.io/socket-io-with-apache-cordova/) and [Socket.io + PhoneGap](http://stackoverflow.com/questions/10738073/socket-io-phonegap/10886574#10886574). It does look like you may have to do some appropriate phonegap configuration stuff so it can work. – jfriend00 Apr 24 '15 at 20:55
  • @A.B - did you get your question answered? – jfriend00 Apr 28 '15 at 04:38
  • thanks for sharing the links and this question, i have read it it seems quite useful but i havent tried practically it will be great if we can real time mobile apps without learning much of docs for pusher.com, firebase etc – A.B Apr 28 '15 at 14:04
3

Use websockets.

A polling based approach introduces a minimum overhead of having to issue a request every X seconds to check for any new messages, which gives you a crappy trade-off: the lower the request rate, the less responsive your app is. xhr long polling reduces this cost somewhat by keeping an http request open for 30-60 seconds or so, allowing the server to send the response as soon as it can, but this still imposes a regular request interval to take place.

A websocket is a persistent connection, meaning a connection is kept open for each client. This means memory usage, but I wouldn't fret too much. You will bottleneck on actual message traffic before persisted connections

This is a really good read to give you an idea of what kind of scale is possible with websockets on node.js: http://www.jayway.com/2015/04/13/600k-concurrent-websocket-connections-on-aws-using-node-js/

There are lots of options out there, two I would recommend are socket.io and faye. They are both very easy to get up and running. Both will also handle things for you like defining message channels, connect/disconnect protocols, and they have the added advantage of selecting the right transport that both the client and server both support. Socket.io 1.0+ (using engine.io) for example will start with long polling and upgrade to websockets if both endpoints support it, which can speed things up. Faye is a nice lightweight pub/sub system whereas socket.io is somewhat more involved, session based offering. One or the other might be a good choice depending on your needs.

If minimizing your request load is your paramount concern, implementing websockets directly with the npm ws (which is what both socket.io and faye rely on) is also an option, but this is obviously more complex.

Andrew Lavers
  • 8,023
  • 1
  • 33
  • 50