简体   繁体   English

为什么本机服务器发送事件(SSE)比polyfill替代方案更有效?

[英]Why are native Server-Sent Events (SSE) more efficient than the polyfill alternative?

Can someone please explain what the author means below by saying that "native XHR streaming" is more efficient than "XHR polling." 有人可以通过说“本机XHR流”比“ XHR轮询”更有效来解释作者在下面的含义。

I'm very interested to know if there's a more efficient way to stream data from the server to client (using XHR - this is not a question about web-sockets). 我非常想知道是否存在一种更有效的方法来将数据从服务器流传输到客户端(使用XHR-这不是有关Web套接字的问题)。 From what I can tell SSE's basically just provides a API for doing a "chunked" server response and adds some feature like connection resume. 据我所知,SSE基本上只提供了用于执行“块状”服务器响应的API,并添加了一些功能,例如连接恢复。 Has there actually been a performance increase in the SSE implementation? SSE实施中实际上是否有性能提升?

While a polyfill will provide a consistent API, be aware that the underlying XHR transport will not be as efficient: 尽管polyfill将提供一致的API,但是请注意,底层XHR传输效率不高:

XHR polling will incur message delays and high request overhead. XHR轮询将导致消息延迟和高请求开销。 XHR long-polling minimizes latency delays but has high request overhead. XHR长轮询最大程度地减少了延迟延迟,但请求开销却很高。 XHR streaming support is limited and buffers all the data in memory. XHR流支持有限,并且将所有数据缓冲在内存中。

Without native support for efficient XHR streaming of event stream data, the polyfill library can fallback to polling, long-polling, or XHR streaming, each of which has its own performance costs. 如果没有对事件流数据的有效XHR流本机支持 ,则polyfill库可能会回退到轮询,长轮询或XHR流,每种方法都有其自己的性能成本。

High Performance Browser Networking by Ilya Grigorik https://hpbn.co/server-sent-events-sse/#emulating-eventsource-with-custom-javascript Ilya Grigorik的高性能浏览器网络 https://hpbn.co/server-sent-events-sse/#emulating-eventsource-with-custom-javascript

XHR streaming and SSEs are very similar in terms of performance except that XHR streaming is generally considered "an unsupported feature in modern browsers" because there's no way to clear the buffer unless you reconnect. XHR流和SSE在性能方面非常相似,不同之处在于XHR流通常被认为是“现代浏览器中不支持的功能”,因为除非重新连接,否则无法清除缓冲区。 This answer explains all that and proposes a very cumbersome workaround: Memory efficient message chunk processing using a XMLHttpRequest . 这个答案解释了所有这些,并提出了一个非常麻烦的解决方法: 使用XMLHttpRequest进行内存高效的消息块处理

The SSE implementation simply clears the buffer after each event is received see: https://dxr.mozilla.org/mozilla-central/source/dom/base/EventSource.cpp . SSE实现仅在收到每个事件后清除缓冲区,请参见: https : //dxr.mozilla.org/mozilla-central/source/dom/base/EventSource.cpp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM