阿贾克斯VS Socket.io [英] Ajax vs Socket.io

查看:153
本文介绍了阿贾克斯VS Socket.io的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个web应用程序,我想知道哪种方法应该是适合我的项目。

I am developing a web app and I was wondering which method should be suitable for my project.

基本上我要显示给用户的是这是从请求到其他服务器采取了一些通知。我的Node.js应用程序获得所有的信息,然后它那$ P $垫出到用户,节省了拷贝到我的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:

  1. 阿贾克斯:客户端将检查所有的时间是否有在服务器上的新内容。这将通过使用来完成一个jQuery AJAX到我的服务器API(每30/60秒)。

  1. 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进行更新。我想这可能与阿贾克斯工作非常好,但怎么样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:

<一个href="http://stackoverflow.com/questions/28613399/websocket-vs-rest-api-for-real-time-data/28618369#28618369">websocket VS用于实时数据的REST API?

针对移动设备的一些折衷的问题在这里讨论:

Some tradeoff issues for mobile devices are discussed here:

<一个href="http://stackoverflow.com/questions/29856618/cordova-sockets-pushnotifications-or-repeatedly-polling-server/29857040#29857040">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查询。难道是便宜系统   资源有一个插座?

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 ?

套接字需要非常少的资源的时候都没有主动所以,是的,一个peristent的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格式信息。

Would the socket.io be a problem for mobile devices ? bandwith and performance. The response of the server is always info in JSON format.

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被更新   服务器端。我想这可能与阿贾克斯工作非常好,但   怎么样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 ?

目前还不清楚你问有关缓存?在WebSocket的实现,服务器获取数据,然后只将其发送给每个用户。没有服务器端缓存一般会需要。在客户端Ajax轮询实现中,服务器必须存储一些数据和等待为每一位客户则请求数据。没有内置的缓存机制,无论是WebSocket的或阿贾克斯。

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来代替。

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.

socket.io是与具有的WebSockets这是今天pretty的很多都在用,除了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.

这篇关于阿贾克斯VS Socket.io的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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