Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 10 months ago.
The community reviewed whether to reopen this question 5 months ago and left it closed:
Original close reason(s) were not resolved
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
Websockets 和 SSE(服务器发送事件)都能够将数据推送到浏览器,但它们不是竞争技术。
Websockets 连接既可以向浏览器发送数据,也可以从浏览器接收数据。可以使用 Websocket 的应用程序的一个很好的示例是聊天应用程序。
SSE 连接只能将数据推送到浏览器。在线股票报价或更新时间线或提要的 Twitter 都是可以从 SSE 中受益的应用程序的好例子。
在实践中,由于 SSE 可以完成的所有事情也可以通过 Websockets 完成,因此 Websockets 受到了更多的关注和喜爱,支持 Websockets 的浏览器也比 SSE 多得多。
然而,对于某些类型的应用程序来说,它可能有点过头了,并且使用 SSE 等协议可以更轻松地实现后端。
此外,SSE 可以仅使用 JavaScript 填充到本身不支持它的旧浏览器中。 SSE polyfill 的一些实现可以在 Modernizr github 页面 上找到。
陷阱:
www.example1.com
的 SSE 连接,以及另外 6 个到www.example2.com 的 SSE 连接
(感谢 Phate)。HTML5Rocks 有一些关于 SSE 的好信息。从该页面:
TLDR 摘要:
SSE 相对于 Websockets 的优点:
Websockets 相对于 SSE 的优势:
SSE 的理想用例:
SSE 问题:
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:
www.example1.com
and another 6 SSE connections towww.example2.com
(thanks Phate).HTML5Rocks has some good information on SSE. From that page:
TLDR summary:
Advantages of SSE over Websockets:
Advantages of Websockets over SSE:
Ideal use cases of SSE:
SSE gotchas:
据 caniuse.com 称:
您可以使用仅客户端的填充来将对 SSE 的支持扩展到许多其他浏览器。对于 WebSockets,这种情况不太可能发生。一些 EventSource polyfills:
如果您需要支持所有浏览器,请考虑使用类似 web-socket-js、SignalR 或 socket.io 支持多种传输,例如 WebSockets、SSE、Forever Frame 和 AJAX 长轮询。这些通常也需要对服务器端进行修改。
了解有关 SSE 的更多信息,请访问:
从以下位置了解有关 WebSocket 的更多信息:
其他区别:
According to caniuse.com:
You can use a client-only polyfill to extend support of SSE to many other browsers. This is less likely with WebSockets. Some EventSource polyfills:
If you need to support all the browsers, consider using a library like web-socket-js, SignalR or socket.io which support multiple transports such as WebSockets, SSE, Forever Frame and AJAX long polling. These often require modifications to the server side as well.
Learn more about SSE from:
Learn more about WebSockets from:
Other differences:
到了2023年,情况已不再像以前那样了。
几年前,当 IE 仍然拥有重要的市场份额时,SSE 的一个缺点是总量IE 缺乏本机支持(而 IE 10+ 支持 WebSockets)。如今,根据caniuse.com,这两种技术在客户端几乎得到同样好的支持:WebSockets 为 98.35% 与 SSE 为 98.03%< /a> (这些统计数据适用于全球用户)。
从历史上看,SSE 的一个严重限制,即每个域 6 个连接限制(在许多浏览器选项卡中打开
yourapp.com
时出现的问题)对于HTTP/2
。所有现代浏览器都支持HTTP/2
(97.16% 的全球用户)并在服务器上-sideHTTP/2+
在过去几年中也超越了HTTP/1
。在 SSE 和 WebSocket 之间进行选择时需要考虑各种因素:
curl
)。In 2023 the situation is not quite as it used to be.
Years ago, when IE still had a significant market share, one downside of SSE was the total lack of native support by IE (whereas WebSockets was supported by IE 10+). Nowadays, according to caniuse.com, both technologies are supported almost equally well on the client side: 98.35% for WebSockets vs 98.03% for SSE (those stats are for global users).
Historically, one severe limitation of SSE, the 6-connections-per-domain limit (a problem when
yourapp.com
is opened in many browser tabs) is not an issue anymore withHTTP/2
. All modern browsers supportHTTP/2
(97.16% of global users) and on the server-sideHTTP/2+
has also surpassedHTTP/1
the last couple of years.Various things need to be considered when chosing between SSE and WebSockets:
curl
could be used).Websocket VS SSE
Web Sockets - 它是一种通过单个 TCP 连接提供全双工通信通道的协议。
例如服务器和浏览器之间的双向通信
由于协议比较复杂,服务器和浏览器都必须依赖websocket库
这是
socket.io
SSE(服务器发送事件)-
在服务器发送事件的情况下,通信仅从服务器到浏览器进行,浏览器不能向服务器发送任何数据。这种通讯方式主要用于
当只需要显示更新的数据时,服务器会在数据更新时发送消息。
例如服务器到浏览器之间的单向通信。
这种协议不太复杂,所以不需要依赖外部库JAVASCRIPT本身提供的
EventSource
接口来接收服务器发送的消息。Websocket VS SSE
Web Sockets - It is a protocol which provides a full-duplex communication channel over a single TCP connection.
For instance a two-way communication between the Server and Browser
Since the protocol is more complicated, the server and the browser has to rely on library of websocket
which is
socket.io
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.Opera、Chrome、Safari 支持 SSE,
Chrome、Safari 在 SharedWorker 内部支持 SSE
Firefox 支持 XMLHttpRequest readState 交互,因此我们可以为 Firefox 制作 EventSource polyfil
Opera, Chrome, Safari supports SSE,
Chrome, Safari supports SSE inside of SharedWorker
Firefox supports XMLHttpRequest readyState interactive, so we can make EventSource polyfil for Firefox
需要注意的一件事:
我遇到了网络套接字和公司防火墙的问题。 (使用 HTTPS 有帮助,但并不总是如此。)
请参阅 https:// /github.com/LearnBoost/socket.io/wiki/Socket.IO-and-firewall-software
https://github.com/sockjs/sockjs-client/issues/94
我假设服务器发送的事件没有那么多问题。但我不知道。
也就是说,WebSocket 非常有趣。我有一个使用 websockets 的小网页游戏(通过 Socket.IO)(http://minibman.com)
One thing to note:
I have had issues with websockets and corporate firewalls. (Using HTTPS helps but not always.)
See https://github.com/LearnBoost/socket.io/wiki/Socket.IO-and-firewall-software
https://github.com/sockjs/sockjs-client/issues/94
I assume there aren't as many issues with Server-Sent Events. But I don't know.
That said, WebSockets are tons of fun. I have a little web game that uses websockets (via Socket.IO) (http://minibman.com)
它们在语义上是不同的。
websocket 具有“双向数据流”的原生语义含义。
而 sse 具有“发布-订阅模式”或“请求-响应模式,尽管响应是流”的本机语义含义。
当然你可以自己在websocket上实现一层“pub-sub模式”或“req-res模式”。
they are different in semantics.
websocket has a native semantic meaning of "bidirectional data stream".
while sse has a native semantic meaning of "publish-subscribe pattern" or "request-respond pattern, despite the response is a stream".
of course you can implement a layer of "pub-sub pattern" or "req-res pattern" over websocket by yourself.