WebSockets 与服务器发送的事件/事件源

问题描述:

WebSocketsServer-Sent Events 能够将数据推送到浏览器.对我来说,它们似乎是相互竞争的技术.它们之间有什么区别?你什么时候会选择一个?

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 和 SSE(服务器发送事件)都能够将数据推送到浏览器,但它们不是竞争技术.

Websockets and SSE (Server Sent Events) are both capable of pushing data to browsers, however they are not competing technologies.

Websockets 连接既可以向浏览器发送数据,也可以从浏览器接收数据.一个可以使用 websockets 的应用程序的一个很好的例子是聊天应用程序.

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 连接只能将数据推送到浏览器.在线股票报价或 Twitter 更新时间线或提要都是可以从 SSE 中受益的应用的好例子.

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.

在实践中,由于 SSE 可以完成的所有事情也可以通过 Websockets 完成,因此 Websockets 得到了更多的关注和喜爱,并且比 SSE 支持 Websockets 的浏览器更多.

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.

但是,对于某些类型的应用程序来说,它可能有点矫枉过正,而且后端可以更容易地使用 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.

此外,SSE 可以被 polyfill 到不支持它的旧浏览器中,这些浏览器只使用 JavaScript.可以在 Modernizr github 页面上找到 SSE polyfill 的一些实现.

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.

问题:

  • SSE 受到最大打开连接数的限制,这在打开各种选项卡时会特别痛苦,因为限制是每个浏览器并且设置为非常低的数字 (6).该问题已在 Chrome中标记为无法修复"> 和 Firefox.此限制是针对每个浏览器 + 域的,这意味着您可以在所有选项卡上打开 6 个到 www.example1.com 的 SSE 连接,以及另外 6 个到 www.example2.com 的 SSE 连接(感谢 Phate).
  • 只有 WS 可以同时传输二进制数据和 UTF-8,SSE 仅限于 UTF-8.(感谢 Chado Nihi).
  • 某些具有数据包检查功能的企业防火墙在处理 WebSockets(Sophos XG Firewall、WatchGuard、McAfee Web Gateway)时遇到问题.
  • 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 Firefox. This limit is per browser + domain, so that means that you can open 6 SSE connections across all of the tabs to www.example1.com and another 6 SSE connections to www.example2.com (thanks Phate).
  • Only WS can transmit both binary data and UTF-8, SSE is limited to UTF-8. (Thanks to Chado Nihi).
  • Some enterprise firewalls with packet inspection have trouble dealing with WebSockets (Sophos XG Firewall, WatchGuard, McAfee Web Gateway).

HTML5Rocks 有一些关于 SSE 的好信息.从该页面:

HTML5Rocks has some good information on SSE. From that page:

为什么您会选择服务器发送事件而不是 WebSockets?好问题.

Server-Sent Events vs. WebSockets

Why would you choose Server-Sent Events over WebSockets? Good question.

SSE 一直处于阴影中的一个原因是,后来的 API(如 WebSockets)提供了更丰富的协议来执行双向、全双工通信.拥有双向通道对于游戏、消息传递应用程序以及需要双向近实时更新的情况更具吸引力.但是,在某些情况下,不需要从客户端发送数据.您只需要来自某些服务器操作的更新.一些例子是朋友的状态更新、股票行情、新闻提要或其他自动数据推送机制(例如更新客户端 Web SQL 数据库或 IndexedDB 对象存储).如果您需要向服务器发送数据,XMLHttpRequest 永远是您的好帮手.

One reason SSEs have been kept in the shadow is because later APIs like WebSockets provide a richer protocol to perform bi-directional, full-duplex communication. Having a two-way channel is more attractive for things like games, messaging apps, and for cases where you need near real-time updates in both directions. However, in some scenarios data doesn't need to be sent from the client. You simply need updates from some server action. A few examples would be friends' status updates, stock tickers, news feeds, or other automated data push mechanisms (e.g. updating a client-side Web SQL Database or IndexedDB object store). If you'll need to send data to a server, XMLHttpRequest is always a friend.

SSE 通过传统 HTTP 发送.这意味着它们不需要特殊的协议或服务器实现即可工作.另一方面,WebSockets 需要全双工连接和新的 Web Socket 服务器来处理协议.此外,Server-Sent Events 具有 WebSockets 在设计上缺乏的多种功能,例如自动重新连接、事件 ID 以及发送任意事件的能力.

SSEs are sent over traditional HTTP. That means they do not require a special protocol or server implementation to get working. WebSockets on the other hand, require full-duplex connections and new Web Socket servers to handle the protocol. In addition, Server-Sent Events have a variety of features that WebSockets lack by design such as automatic reconnection, event IDs, and the ability to send arbitrary events.

TLDR 摘要:

SSE 相对于 Websocket 的优势:

  • 通过简单的 HTTP 而不是自定义协议传输
  • 可以用 javascript 填充以将 SSE反向移植"到尚不支持它的浏览器.
  • 内置支持重新连接和事件 ID
  • 更简单的协议
  • 企业防火墙进行数据包检查没有问题

Websockets 相对于 SSE 的优势:

  • 实时双向通信.
  • 更多浏览器的原生支持

SSE 的理想用例:

  • 股票行情流
  • 推特动态更新
  • 浏览器通知

SSE 陷阱:

  • 不支持二进制
  • 最大打开连接数限制