Javascript 实时 Web 应用程序的短轮询与长轮询?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4642598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Short-polling vs Long-polling for real time web applications?
提问by Jeff
I'm building a real-time web application As far as I know, the most popular choices are short-polling and long-polling. What are the advantages and disadvantages might there be for measuring one over the other?
我正在构建一个实时 Web 应用程序 据我所知,最流行的选择是短轮询和长轮询。衡量一个相对于另一个可能有哪些优点和缺点?
采纳答案by jmarranz
Short polling (a.k.a. AJAX based timer):
Pros: simpler, not server consuming (if the time between requests is long).
Cons: bad if you need to be notified WHEN the server event happens with no delay. Example(ItsNatbased)Long polling (a.k.a. Comet based on XHR)
Pros: you are notified WHEN the server event happens with no delay. Cons: more complex and more server resources used. Example(ItsNat based)
回答by sp3c1
Just for the sake of argument.
只是为了辩论。
Both are http request (xhr), and its at least partially untrue it uses more server resources (depends totally on technology, will explain later).
两者都是http请求(xhr),它至少部分不真实,它使用更多的服务器资源(完全取决于技术,稍后解释)。
Short polling.
短投票。
Lot of request that are processed as they come on server. Creates a lot of traffic (uses resources, but frees them as soon as response is send back):
大量请求在服务器上进行处理。产生大量流量(使用资源,但在响应发回后立即释放它们):
00:00:00 C-> Is the cake ready?
00:00:01 S-> No, wait.
00:00:01 C-> Is the cake ready?
00:00:02 S-> No, wait.
00:00:02 C-> Is the cake ready?
00:00:03 S-> Yes. Have some lad.
00:00:03 C-> Is the other cake ready? ..
Long polling
长轮询
One request goes to server and client is waiting for the response to come (its unresolved). In case of Server with php/apache would mean a spawned thread to handle, that reserve resources, till its done. So the traffic is smaller, but you eat up your resources fast (or rather you block resources). But if you use for example Node (or any other async approach - c++ qt for example), you can potentially minimize the resource usage a lot (store response object for http request and use it when the work is ready)
一个请求发送到服务器,客户端正在等待响应的到来(未解决)。如果使用 php/apache 的服务器将意味着要处理的衍生线程,该线程保留资源,直到完成。所以流量更小,但你很快就会吃掉你的资源(或者说你阻塞了资源)。但是,如果您使用例如 Node(或任何其他异步方法 - 例如 c++ qt),您可能会最大限度地减少资源使用(存储 http 请求的响应对象并在工作准备就绪时使用它)
12:00 00:00:00 C-> Is the cake ready?
12:00 00:00:03 S-> Yes.Have some lad.
12:00 00:00:03 C-> Is the cake ready?
If you compare that to short polling, you will see that potentially in short poll you used more transfer, but during those 3s you actually take 1,5s of processing time (means something could execute in between your calls). In case for long poll the same resources were used all the time. Now usually php with all libs starts with 4MB memory - then you have a framework 4-20MB. Assume you have 1024MB RAM available (free). Say lets be pessimistic and assume that you will use 25 MB per one php instace. It means you can get only as much as 40 long polled connection scripts.
如果您将其与短轮询进行比较,您会发现在短轮询中您可能使用了更多的传输,但在这 3 秒内,您实际上需要 1.5 秒的处理时间(意味着可能在您的调用之间执行某些操作)。在长时间轮询的情况下,始终使用相同的资源。现在通常带有所有库的 php 以 4MB 内存开始 - 那么你有一个 4-20MB 的框架。假设您有 1024MB RAM 可用(免费)。假设让我们悲观地假设每个 php 实例将使用 25 MB。这意味着您最多只能获得 40 个长轮询连接脚本。
Its precisely the reason why you could serve potentially a lot more with Node, as node would not spawn its instances (unless you want to use workers etc), so with same memory you could probably get easily to 10k connections hanging. You would get a spike in the CPU as they will come, and when they will potentially be released, but when they are idle its like they are not there (you pay only for the memory structures you would keep in node/c++).
这正是您可以使用 Node 提供更多潜在服务的原因,因为 node 不会产生它的实例(除非您想使用工作程序等),因此使用相同的内存,您可能很容易挂起 10k 个连接。当它们到来时,您会在 CPU 中出现峰值,并且它们何时可能被释放,但是当它们空闲时,它们就像不在那里一样(您只需为将保留在 node/c++ 中的内存结构付费)。
Websocket
网络套接字
Now if you want to send few things, whenever they are in or out of client, go for the websockets (ws protocol). First call is size of http request, but later you send just the messages, from the client to server (new questions) and server to client(answers or pushes - can even do broadcast for all connected clients). There are php websocekts libs but again, use some different technology - node or c++ preferably.
现在,如果您想发送一些东西,无论何时它们进出客户端,都可以使用 websockets(ws 协议)。第一次调用是 http 请求的大小,但后来你只发送消息,从客户端到服务器(新问题)和服务器到客户端(答案或推送 - 甚至可以为所有连接的客户端进行广播)。有 php websocekts 库,但同样,使用一些不同的技术 - 最好使用 node 或 c++。
Some libs, like socket.io have a hierarchy of its own, so when websocket fails, it goes back to long or short polling.
一些库,比如 socket.io 有自己的层次结构,所以当 websocket 失败时,它会回到长轮询或短轮询。
When to use.
何时使用。
Short polling- well, never ^^.
短轮询- 好吧,永远不会^^。
Long polling- potentially when you are exchanging single call with server, and server is doing some work in background. Also when you won't query server on the same page anymore. Also when you are not using php as layer to handle the long polled connection (node/c++ can be a simple middle layer). Note long polling can be really beneficial, but only when you make it so.
长轮询- 可能是当您与服务器交换单个呼叫时,服务器正在后台执行一些工作。此外,当您不再在同一页面上查询服务器时。此外,当您不使用 php 作为层来处理长轮询连接时(node/c++ 可以是一个简单的中间层)。请注意,长轮询确实很有用,但前提是您这样做了。
Websocket- you potentially will exchange more then one or two calls with server, or something might come from server you did not expected / asked, like notification of email or something. You should plan different "rooms", depend on functionalities. Embrace the event based nature of javascript ;]
Websocket- 您可能会与服务器交换一两个以上的调用,或者某些内容可能来自您不期望/询问的服务器,例如电子邮件通知或其他内容。您应该根据功能规划不同的“房间”。拥抱 javascript 基于事件的性质;]
回答by Ibrahim Hasanov
If you want to get real time application based on database you can use ajax long poll and comet combination. Long pollis really good for your bandwith and also it is really useful for user MB.Because when user send request user will pay for it like MB or some kind of internet connection.For example for Short pollwhen you do something like sending request per second user internet usage will be more because each connection user will pay for it(It means that user loose Mb )but in the long polling user only will pay just for new messages.
如果您想获得基于数据库的实时应用程序,您可以使用 ajax long poll 和 Comet 组合。 长轮询是你的带宽非常好,也很为用户MB.Because真正有用的,当用户发送请求的用户将为此付出代价像MB或某种互联网connection.For例如对短调查,当你做这样的事情每发送请求第二个用户的互联网使用量会更多,因为每个连接用户都会为此付费(这意味着用户松了 Mb )但在长轮询中,用户只会为新消息付费。
Websocketis really good thing but when you use it you should think about one big problem that a lot of people cannot use chat sistem because Websocket is for just new version of browsers
Websocket确实是个好东西,但是当你使用它时,你应该考虑一个大问题,很多人无法使用聊天系统,因为 Websocket 只是针对新版本的浏览器