问题描述
我正在开发一个网络应用程序,我想知道哪种方法适合我的项目.
I am developing a web app and I was wondering which method should be suitable for my project.
基本上我想向用户显示的是一些从对其他服务器的请求中获取的通知.我的 node.js 应用程序获取所有信息,然后将其传播给用户,将副本保存到我的 MongoDB 中.
Basically what I want to display to the users are some notifications which are taken from requests to other servers. My node.js app gets all the info and then it spread out to the users, saving a copy into my MongoDB.
这个想法很简单,但在阅读方法时我发现了这两种技术:
The idea is quite simple but reading about methods I found these two techniques:
Ajax:客户端会一直检查服务器上是否有新内容.这将通过使用 jquery ajax get 到我的服务器 API(每 30/60 秒)来完成.
Ajax : Client side would be checking all time if there is new content on the server. This would be done by using a jquery ajax get to my server API (every 30/60 seconds).
Socket.io:客户端连接一次,然后保持一个永久的 TCP 连接(更实时).
Socket.io : The client connects once, and then a permanent TCP connection is maintained (more realtime).
现在我已经说明了情况,我有以下问题:
Now I have explained the situation, I have the following questions :
我不会有太多的 ajax 请求吗?想象一下,我想每分钟检查一次服务器,如果我们将应用程序扩展到 100 个用户,它将每分钟给我 100 个查询.有一个套接字会在系统资源上更便宜"吗?
Would I not have too many requests with ajax ? imagine I want a check every minute to the server, if we scale the app to 100 users, it will give me 100 queries per minute. Would it be "cheaper" in system resources to have a socket ?
socket.io 对移动设备有问题吗?带宽和性能.服务器的响应始终是 JSON 格式的信息.
Would the socket.io be a problem for mobile devices ? bandwith and performance. The response of the server is always info in JSON format.
我读到 now.js 可以用于此,但似乎不再支持该项目,因此不确定使用它是否是个好主意.
I read that now.js could be used for this but it seems the project is no longer supported, so not sure if using it would be a good idea.
这两种方法的缓存如何?我正在考虑为每个用户创建一个缓存文件,这将由服务器端的 node.js 更新.我想这可以很好地与 ajax 一起使用,但是 socket.io 呢?
How is the caching on both methods ? I was considering to create a cache file for each user and this would be updated by the node.js in the server side. I guess this could work really well with ajax but what about socket.io ?
socket.io 与许多浏览器完全不兼容是真的吗?我的应用会更专注于移动设备,我认为这会让我考虑选择 ajax.
Is it true that socket.io is not compatible at all with many browsers ? My app would be more focused to mobile devices and I think this could make me think about choosing ajax instead.
有其他建议吗?
我希望这能让我和其他处于同样情况的人清醒过来:)谢谢
I hope this could clear my mind and others who are in the same situation :)Thanks
推荐答案
此处讨论了 webSocket 和 Ajax 之间的许多通用权衡:
Many of the generic tradeoffs between webSocket and Ajax are discussed here:
此处讨论了移动设备的一些权衡问题:
Some tradeoff issues for mobile devices are discussed here:
Cordova:套接字、PushNotifications 或重复轮询服务器?
简而言之,如果您的数据主要是服务器驱动的,然后需要发送到客户端,并且您希望客户端看到新数据时有相当好的延迟,那么这正是 webSockets 擅长的问题.webSockets 在这种情况下效果最好,因为客户端不需要频繁轮询,服务器不需要处理来自大量客户端的定期轮询请求.相反,每个客户端只设置一个持久的 webSocket 通信通道,然后服务器可以随时根据需要向下发送数据.
In a nutshell, if your data is primarily server-driven and then needs to be sent out to clients and you desire fairly good latency for when the clients see the new data, then that is the exact problem that webSockets are good for. webSockets work best in this situation because the client does not need to poll frequently, the server does not need to handle regular polling requests from lots of clients. Instead, each client just sets up the one persistent webSocket communication channel which the server can then send data down upon demand at any time.
我不会有太多的 ajax 请求吗?想象我想要一张支票每分钟到服务器,如果我们将应用程序扩展到 100 个用户,它将每分钟给我 100 个查询.它会在系统中更便宜"吗拥有套接字的资源?
套接字在不活动时只需要很少的资源,所以是的,持久的 webSocket 比许多客户端无休止地轮询更有效.这就是发明 webSockets 的原因,因为它们更擅长解决这个特定问题.
Sockets require very little resources when they are not active so yes, a peristent webSocket is more efficient than lots of clients polling endlessly. This is why webSockets were invented because they are better at solving this particular problem.
socket.io 对移动设备有问题吗?带宽和表现.服务器的响应始终是 JSON 格式的信息.
socket.io 不是带宽或性能的问题.尝试在后台使用 webSockets 存在一些移动问题,因为移动设备也在尝试进行主动电源管理,尽管客户端轮询也存在类似问题.
socket.io is not a problem for bandwidth or performance. There are some mobile issues with trying to use webSockets in the background because mobile devices are also trying to do active power management, though a similar issue exists with client polling too.
这两种方法的缓存如何?我正在考虑创建一个每个用户的缓存文件,这将由 node.js 中的服务器端.我想这可以很好地与 ajax 一起使用,但是socket.io 怎么样?
不清楚您对缓存的要求是什么?在 webSocket 实现中,服务器获取数据,然后将其发送给每个用户.通常不需要服务器端缓存.在客户端 Ajax 轮询实现中,服务器必须将数据存储在某处并等待"每个客户端然后请求数据.webSocket 或 Ajax 都没有内置"缓存机制.
It is unclear what you are asking about caching? In a webSocket implementation, the server gets the data and then just sends it to each user. No server-side caching would generally be needed. In a client Ajax polling implementation, the server would have to store the data somewhere and "wait" for each client to then request the data. There is no "built-in" caching mechanism for either webSocket or Ajax.
socket.io 与很多浏览器完全不兼容是真的吗?我的应用程序将更专注于移动设备,我认为这可以让我考虑选择 ajax.
socket.io 与所有具有 webSockets 的浏览器完全兼容,除了 IE9 及更早版本之外,它几乎是当今使用的所有浏览器.如果您使用 socket.io 库,如果 webSockets 不存在,它将自动回退到长轮询.无论您是进行常规轮询还是 webSocket,您的移动问题都可能相似,因为移动设备想要对长时间运行的事物进行电源管理,但您又不想停止轮询.我不认为这是避免 webSockets/socket.io 的原因.socket.io 在失去连接时有一些非常好的自动重新连接逻辑,这非常有用.
socket.io is fully compatible with all browsers that have webSockets which is pretty much everything in use today except for IE9 and older. If you use the socket.io library, it will automatically fall back to long polling if webSockets don't exist. Your mobile issues are likely going to be similar whether you're doing regular polling or webSocket because the mobile device wants to power manage long running things, but you don't want to stop polling. I don't think this is a reason to avoid webSockets/socket.io. socket.io has some really nice auto-reconnect logic whenever it loses the connection which can be really useful.
在移动世界中,我认为您会发现如果不使用某种可以插入本机推送"系统的本机应用程序组件,您就无法在后台可靠地进行实时通知设备,因为这是唯一一个既能高效利用电池又能与电源管理完全兼容的系统.只要网页不是前台任务或设备空闲,就会对其进行电源管理.
In the mobile world, I think you're just going to find that you can't reliably do real-time notifications in the background without using some sort of native app component that can plug into the native "push" system on the device because that's the only system that is both battery efficient and fully compatible with power management. A web page is going to be power managed as soon as it is not the foreground task or when the device has been idle.
这篇关于Ajax 与 Socket.io的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!