问题描述
和能够将数据推送到浏览器。对我来说,他们似乎是竞争技术。他们有什么区别? Web服务器和SSE(服务器发送事件)都能够将数据推送到浏览器上但是,它们不是竞争技术。
Websockets连接既可以将数据发送到浏览器,也可以从浏览器接收数据。一个可以使用websockets的应用程序的好例子是一个聊天应用程序。
SSE连接只能将数据推送到浏览器。在线股票报价或更新时间线或订阅源的推文是可以从SSE中受益的应用程序的好例子。
在实践中,因为SSE所能做的一切都可以使用Websockets,Websockets得到了更多的关注和喜爱,并且更多的浏览器支持Websockets而不是SSE。然而,对于某些类型的应用程序来说,它可能有点过分,并且使用SSE等协议可以更容易地实现后端。
此外,SSE可以复制到旧式浏览器中,而这些浏览器本身仅使用JavaScript来支持它。可以在上找到SSE polyfill的一些实现。 。
陷阱:
限制打开连接的最大数量,当打开各种标签时特别痛苦,因为限制为每个浏览器 并且设置为非常低的数字(6)。该问题已在
只有WS可以传输二进制数据和UTF-8,SSE仅限于UTF-8。 (感谢Chado Nihi)。
对SSE有一些很好的信息。从该页面:
TLDR概要:
SSE优于Websockets的优点 p>
可以通过简单的HTTP而不是自定义协议传输
可以用JavaScript填充backport SSE到尚不支持它的浏览器。
内置支持重新连接和事件ID
简单协议
Websocket优于SSE的优势: 上海证券交易所的理想用例: SSE陷阱: Both WebSockets and Server-Sent Events are capable of pushing data to browsers. To me they seem to be competing technologies. What is the difference between them? When would you choose one over the other? Websockets and SSE (Server Sent Events) are both capable of pushing data to browsers, however they are not competing technologies. Websockets connections can both send data to the browser and receive data from the browser. A good example of an application that could use websockets is a chat application. SSE connections can only push data to the browser. Online stock quotes, or twitters updating timeline or feed are good examples of an application that could benefit from SSE. In practice since everything that can be done with SSE can also be done with Websockets, Websockets is getting a lot more attention and love, and many more browsers support Websockets than SSE. However, it can be overkill for some types of application, and the backend could be easier to implement with a protocol such as SSE. Furthermore SSE can be polyfilled into older browsers that do not support it natively using just JavaScript. Some implementations of SSE polyfills can be found on the Modernizr github page. Gotchas: HTML5Rocks has some good information on SSE. From that page: Advantages of SSE over Websockets: Advantages of Websockets over SSE: Ideal use cases of SSE: SSE gotchas: 这篇关于WebSockets与Server-Sent事件/ EventSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
实时,双向沟通。
更多浏览器的本地支持
股票代码流
twitter Feed更新
浏览器通知
无二进制支持
最大开放连接数限制
SSE suffers from a limitation to the maximum number of open connections, which can be specially painful when opening various tabs as the limit is per browser and set to a very low number (6). The issue has been marked as "Won't fix" in Chrome and FirefoxOnly WS can transmit both binary data and UTF-8, SSE is limited to UTF-8. (Thanks to Chado Nihi).
TLDR summary:
Transported over simple HTTP instead of a custom protocolCan be poly-filled with javascript to "backport" SSE to browsers that do not support it yet.Built in support for re-connection and event-idSimpler protocol
Real time, two directional communication.Native support in more browsers
Stock ticker streamingtwitter feed updatingNotifications to browser
No binary supportMaximum open connections limit