简体   繁体   English

服务器端事件+客户端事件vs Websocket

[英]Server Side Events + Client Side Events vs Websocket

I am updating an old system that used to use an ajax polling mechanism. 我正在更新用于使用ajax轮询机制的旧系统。 The script would periodically call the back-end looking for updates, and rarely the user would make an ajax request to send data. 该脚本会定期调用后端以查找更新,很少用户会发出ajax请求来发送数据。 I first wanted to use Web Sockets because I could instantly get the data from push events, and because the connection stays open. 我最初想使用Web套接字,因为我可以立即从推送事件中获取数据,并且因为连接保持打开状态。 I then read about Server Side Events, and how it is one directional. 然后,我阅读有关服务器端事件的内容,以及它是如何定向的。 This fits exactly what I need because the browser is just waiting for events. 这完全符合我的需要,因为浏览器只是在等待事件。 However, there are rare cases when the user can send data. 但是,在极少数情况下,用户可以发送数据。 Is there an alternative to Server Side Events, where I can keep a connection open to send data back to the server? 是否有服务器端事件的替代方法,我可以保持连接打开状态以将数据发送回服务器? Is it better to use SSE + AJAX, SSE + (Alternative Way), or just a web socket (Even though data is rarely sent back to server)? 使用SSE + AJAX,SSE +(替代方式)还是仅使用Web套接字(即使很少将数据发送回服务器)更好?

Thank you 谢谢

This is the best explanation for SSE and its flexibilty 这是上交所及其灵活性的最好解释

Server-Sent Events vs. WebSockets 服务器发送的事件与WebSockets

Why would you choose Server-Sent Events over WebSockets? 您为什么选择通过WebSockets发送服务器发送的事件? Good question. 好问题。

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. 将SSE保留在阴影中的原因之一是因为后来的API(如WebSocket)提供了更丰富的协议来执行双向全双工通信。 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 (eg updating a client-side Web SQL Database or IndexedDB object store). 一些示例是朋友的状态更新,股票行情自动收录器,新闻提要或其他自动数据推送机制(例如,更新客户端Web SQL数据库或IndexedDB对象存储)。 If you'll need to send data to a server, XMLHttpRequest is always a friend. 如果您需要将数据发送到服务器,则XMLHttpRequest始终是朋友。

SSEs are sent over traditional HTTP. SSE通过传统的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. 另一方面,WebSockets需要全双工连接和新的Web Socket服务器来处理协议。 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. 另外,服务器发送的事件具有WebSocket设计上缺少的各种功能,例如自动重新连接,事件ID和发送任意事件的功能。

I had built a chat application using sse and ajax for my site.I would suggest sse + ajax would be way to go if there is only stream updates and very few updates from client to server for that you can use the ajax part 我已经为我的站点构建了一个使用sse和ajax的聊天应用程序。我建议如果只有流更新并且从客户端到服务器的更新很少,则可以使用sse + ajax,因为您可以使用ajax部分

Only problem that I found is its lack of support across browsers .And if you want to know more in depth about sse ask specifically what you want 我发现的唯一问题是缺乏跨浏览器的支持。如果您想进一步了解sse,请具体询问您想要什么

Browser Support List 浏览器支持列表

As you usage is mostly server pushing to client, I would recommend a combination of Server-Sent events for the push from server to client and AJAX for the other way around. 由于您的使用主要是服务器向客户端推送,因此我建议将服务器已发送事件组合起来,用于从服务器向客户端推送,以及将AJAX用于其他方式。

You should definitely read this article to get to a decision: 您绝对应该阅读本文以做出决定:

http://streamdata.io/blog/push-sse-vs-websockets/ http://streamdata.io/blog/push-sse-vs-websockets/

This will give you pros and cons of using Server-Sent events versus WebSocket. 这将为您提供使用Server-Sent事件和WebSocket的优缺点。

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

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