javascript - Cordova: Sockets, PushNotifications, or repeatedly polling server? -
i have cordova/phonegap app.
i'd have semblance of real-time updates when app in foreground.
what's least resource-intensive way accomplish this? should use socket.io, pushnotification plugins, or make api request every few seconds? what's least taxing on both device , server?
for mobile device, have classic tradeoff between battery usage, network usage , timeliness of updates.
the push notification service built mobile os designed try give best of these tradeoffs , runs globally rather per app (which more efficient), though gives less control on implementation details.
when comparing socket.io vs. polling api, socket.io (and more websockets) designed more efficient way of getting asynchronous notifications server.
in socket.io, create socket connection server. connection stays open duration of app (in foreground) and, @ time, server can send data , receive when sent. because connections can lost , endpoints not notified of immediately, socket.io uses small heartbeat packets sent on regular basis between client , server. if heartbeat packets stop being responded to, socket.io assumes connection has died , close original socket , attempt create new connection. transparently , automatically you. heartbeat, however, has undesirable ramifications mobile. data sent tiny it's not issue of bandwidth usage, every transmission mobile device uses battery , can relevant if left run long time. heartbeat interval in socket.io configurable. can turned off (not recommended) or time interval can set longer time.
both os push service , socket.io efficient server-end of things because server work when there send client , not have field regular requests there nothing do.
the possible advantage of polling here if desired update interval long (e.g. once hour) or isn't on , used occasionally. then, send ajax call each hour or upon demand , server wouldn't have except answer occasional ajax call. if desired interval shorter, you're going want use 1 of true push mechanisms (either os push or socket.io).
Comments
Post a Comment