WebSockets和Server-Sent Events都能够将数据推送到浏览器。在我看来,它们似乎是相互竞争的技术。它们之间的区别是什么?什么时候你会选择一个而不是另一个?


当前回答

据caniuse.com网站报道:

98.35%的全球用户原生支持WebSockets 98.03%的全球用户原生支持服务器发送事件

您可以使用仅客户端的polyfill将SSE支持扩展到许多其他浏览器。这在WebSockets中是不太可能的。一些EventSource腻子:

Remy Sharp的EventSource,没有其他库依赖(IE7+) jQuery。Rick Waldron的EventSource Yaffle的EventSource(取代本机实现,规范跨浏览器的行为)

如果你需要支持所有的浏览器,可以考虑使用像web-socket-js, SignalR或socket这样的库。io支持多种传输,如WebSockets, SSE, Forever Frame和AJAX长轮询。这些通常也需要对服务器端进行修改。

了解更多关于SSE的信息:

HTML5 Rocks文章 W3C规范(已发布版本,编辑草案)

了解更多关于WebSockets的信息:

HTML5 Rocks文章 W3C规范(已发布版本,编辑草案)

其他的差异:

WebSockets支持任意二进制数据,SSE只使用UTF-8

其他回答

据caniuse.com网站报道:

98.35%的全球用户原生支持WebSockets 98.03%的全球用户原生支持服务器发送事件

您可以使用仅客户端的polyfill将SSE支持扩展到许多其他浏览器。这在WebSockets中是不太可能的。一些EventSource腻子:

Remy Sharp的EventSource,没有其他库依赖(IE7+) jQuery。Rick Waldron的EventSource Yaffle的EventSource(取代本机实现,规范跨浏览器的行为)

如果你需要支持所有的浏览器,可以考虑使用像web-socket-js, SignalR或socket这样的库。io支持多种传输,如WebSockets, SSE, Forever Frame和AJAX长轮询。这些通常也需要对服务器端进行修改。

了解更多关于SSE的信息:

HTML5 Rocks文章 W3C规范(已发布版本,编辑草案)

了解更多关于WebSockets的信息:

HTML5 Rocks文章 W3C规范(已发布版本,编辑草案)

其他的差异:

WebSockets支持任意二进制数据,SSE只使用UTF-8

2023年的情况与过去大不相同。

几年前,当IE仍然拥有相当大的市场份额时,SSE的一个缺点是完全缺乏IE的本地支持(而WebSockets由IE 10+支持)。现在,根据caniuse.com的数据,这两种技术在客户端的支持几乎是一样的:WebSockets的98.35% vs SSE的98.03%(这些数据是针对全球用户的)。

从历史上看,SSE的一个严重限制,每个域名6个连接的限制(当yourapp.com在许多浏览器选项卡中打开时,这是一个问题)不再是HTTP/2的问题。所有现代浏览器都支持HTTP/2(97.16%的全球用户),在服务器端HTTP/2+也在过去几年中超过了HTTP/1。

在SSE和WebSockets之间选择时需要考虑以下几点:

SSE is generally simpler to implement and easier to test/debug (a simple curl could be used). WebSockets support bidirectional (full-duplex) communication. That said, SSE could be used in conjunction with AJAX if bidirectional communication is needed. WebSockets are often said to be the simpler option in those cases, but I think such generalizations can be misleading, as it largely depends on the type of application, how it's designed and the technologies used. SSE supports UTF-8 text messages only, whereas WebSockets can also transmit binary data. From a practical standpoint, I'd also recommend to research how well your application server supports each of those technologies. Note that some rely on additional modules and/or libraries. You might want to look at some examples and maybe build a quick PoC.

它们在语义上有所不同。

Websocket具有“双向数据流”的原生语义含义。

sse的原生语义是“发布-订阅模式”或“请求-响应模式,尽管响应是一个流”。

当然,你可以自己在websocket上实现一层“发布-订阅模式”或“需求模式”。

有一件事需要注意: 我有websockets和企业防火墙的问题。(使用HTTPS会有所帮助,但并非总是如此。)

参见https://github.com/LearnBoost/socket.io/wiki/Socket.IO-and-firewall-software https://github.com/sockjs/sockjs-client/issues/94

我认为服务器发送事件没有那么多问题。但我不知道。

也就是说,WebSockets非常有趣。我有一个小网页游戏,使用websockets(通过Socket.IO) (http://minibman.com)


Websocket VS SSE


Web Sockets -它是一种协议,通过单个TCP连接提供全双工通信通道。 例如,服务器和浏览器之间的双向通信 由于协议比较复杂,服务器和浏览器不得不依赖websocket库 哪个是socket。io

Example - Online chat application.

SSE(Server-Sent Event) - In case of server sent event the communication is carried out from server to browser only and browser cannot send any data to the server. This kind of communication is mainly used when the need is only to show the updated data, then the server sends the message whenever the data gets updated. For instance a one-way communication between the Server to Browser. This protocol is less complicated, so no need to rely on the external library JAVASCRIPT itself provides the EventSource interface to receive the server sent messages.

Example - Online stock quotes or cricket score website.