Cordova:套接字,PushNotifications或重复轮询服务器? [英] Cordova: Sockets, PushNotifications, or repeatedly polling server?

查看:122
本文介绍了Cordova:套接字,PushNotifications或重复轮询服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Cordova / PhoneGap应用程式。

I have a Cordova/PhoneGap app.

当应用程式位于前台时,我想要一些实时更新。

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

最少的资源密集型方法是什么?我应该使用socket.io,pushnotification插件,还是只是每隔几秒发出一个API请求?

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?

推荐答案

对于移动设备,您需要在电池使用率,网络使用和更新的及时性。

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.

当比较socket.io和轮询一个API,socket时,这个函数的作用比每个应用程序(通常更高效)。

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.

在socket.io中,您将创建一个套接字连接到服务器。该连接在您的应用程序的持续时间(在前台)保持打开,并且在任何时候,服务器可以只发送您的数据,并且您将在发送时立即收到它。因为连接可能丢失,并且不一定立即通知端点, socket.io 使用在客户端和服务器之间定期发送的小心跳包。如果心跳包停止响应,则socket.io假定连接已经死亡,并将关闭原始套接字并尝试创建新连接。它为你透明和自动地做到这一切。然而,这种心跳对于移动而言具有一些不希望的后果。发送的数据很小,所以这不是一个真正的带宽使用问题,但每个从移动设备的传输都使用电池,如果长时间运行,这可能是相关的。 socket.io中的心跳间隔可配置。它可以关闭(不推荐)或者时间间隔可以设置为更长的时间。

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.

OS推送服务和socket.io都是非常高效的服务器端的事情,因为服务器只有当有东西实际发送到客户端时才工作,并且不必在没有什么可做的地方定期请求。

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.

这里轮询的唯一可能的优点是,如果你想要的更新间隔很长(例如每小时一次),或者它通常不开启,并且偶尔使用。然后,您可以每小时或根据需要发送Ajax调用,服务器不必执行任何操作,除非应答偶尔的Ajax调用。如果期望的间隔更短,那么你可能想要使用真正的推送机制之一(OS push或socket.io)。

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).

这篇关于Cordova:套接字,PushNotifications或重复轮询服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆