简体   繁体   English

使用HTTP / 2时,我们是否应该优先选择SSE + REST而不是websocket?

[英]Should we prefer SSE + REST over websocket when using HTTP/2?

When using websocket, we need a dedicated connection for bidirectionnel communication. 使用websocket时,我们需要一个专用的双向通信连接。 If we use http/2 we have a second connection maintained by the server. 如果我们使用http / 2,我们有一个由服务器维护的第二个连接。

In that case, using websocket seems to introduce an unecessary overhead because with SSE and regular http request we can have the advantage of bidirectionnal communication over a single HTTP/2 connection. 在这种情况下,使用websocket似乎会引入不必要的开销,因为使用SSE和常规http请求,我们可以通过单个HTTP / 2连接获得双向通信的优势。

What do you think? 你怎么看?

Using 2 streams in one multiplexed HTTP/2 TCP connection (one stream for server-to-client communication - Server Sent Events (SSE), and one stream for client-to-server communication and normal HTTP communication) versus using 2 TCP connections (one for normal HTTP communication and one for WebSocket) is not easy to compare. 在一个多路复用HTTP / 2 TCP连接中使用2个流(一个用于服务器到客户端通信的流 - 服务器发送事件 (SSE),以及一个用于客户端到服务器通信和正常HTTP通信的流)与使用2个TCP连接(一个用于正常HTTP通信,一个用于WebSocket)不容易比较。

Probably the mileage will vary depending on applications. 里程可能因应用程序而异。

Overhead ? 高架 ? Well, certainly the number of connections doubles up. 当然,连接数量增加了一倍。 However, WebSocket can compress messages, while SSE cannot. 但是,WebSocket可以压缩消息,而SSE则不能。

Flexibility ? 灵活性? If the connections are separated, they can use different encryptions. 如果连接是分开的,则可以使用不同的加密。 HTTP/2 typically requires very strong encryption, which may limit performance. HTTP / 2通常需要非常强大的加密,这可能会限制性能。 On the other hand, WebSocket does not require TLS. 另一方面,WebSocket不需要TLS。

Does clear-text WebSocket work in mobile networks ? 明文WebSocket是否可以在移动网络中运行? In the experience I have, it depends. 根据我的经验,这取决于。 Antiviruses, application firewalls, mobile operators may limit WebSocket traffic, or make it less reliable, depending on the country you operate. 防病毒,应用程序防火墙,移动运营商可能会限制WebSocket流量,或使其不太可靠,具体取决于您运营的国家/地区。

API availability ? API可用性? WebSocket is a wider deployed and recognized standard; WebSocket是一个更广泛的部署和认可的标准; for example in Java there is an official API ( javax.websocket ) and another is coming up ( java.net.websocket ). 例如,在Java中有一个官方API( javax.websocket ),另一个正在出现( java.net.websocket )。

I think SSE is a technically inferior solution for bidirectional web communication and as a technology it did not become very popular (no standard APIs, no books, etc - in comparison with WebSocket). 我认为SSE是双向Web通信技术上较差的解决方案,作为一种技术,它并没有变得非常流行(没有标准的API,没有书籍等 - 与WebSocket相比)。 I would not be surprised if it gets dropped from HTML5, and I would not miss it, despite being one of the first to implement it in Jetty. 如果从HTML5中删除它我不会感到惊讶,我不会错过它,尽管它是第一个在Jetty中实现它的人之一。

Depending on what you are interested in, you have to do your benchmarks or evaluate the technology for your particular case. 根据您感兴趣的内容,您必须根据具体情况进行基准测试或评估技术。

From the perspective of a web developer, the difference between Websockets and a REST interface is semantics. 从Web开发人员的角度来看,Websockets和REST接口之间的区别在于语义。 REST uses a request/response model where every message from the server is the response to a message from the client. REST使用请求/响应模型,其中来自服务器的每条消息都是对来自客户端的消息的响应。 WebSockets, on the other hand, allow both the server and the client to push messages at any time without any relation to a previous request. 另一方面,WebSockets允许服务器和客户端随时推送消息,而与先前的请求无任何关系。

Which technique to use depends on what makes more sense in the context of your application. 使用哪种技术取决于在您的应用程序环境中更有意义的内容。 Sure, you can use some tricks to simulate the behavior of one technology with the other, but it is usually preferably to use the one which fits your communication model better when used by-the-book. 当然,您可以使用一些技巧来模拟一种技术与另一种技术的行为,但通常最好在本书使用时更好地使用适合您的通信模型的技术。

Server-sent events are a rather new technology which isn't yet supported by all major browsers, so it is not yet an option for a serious web application. 服务器发送的事件是一项相当新的技术,尚未得到所有主流浏览器的支持,因此它还不是一个严肃的Web应用程序的选项。

It depends a lot on what kind of application you want to implement. 这很大程度上取决于您要实现的应用程序类型。 WebSocket is more suitable if you really need a bidirectional communication between server and client, but you will have to implement all the communication protocol and it might not be well supported by all IT infrastructures (some firewall, proxy or load balancers may not support WebSockets). 如果您确实需要服务器和客户端之间的双向通信,WebSocket更合适,但您必须实现所有通信协议,并且所有IT基础架构可能都不能很好地支持它(某些防火墙,代理或负载平衡器可能不支持WebSockets) 。 So if you do not need a 100% bidirectional link, I would advise to use SSE with REST requests for additional information from client to server. 因此,如果您不需要100%双向链接,我建议将SSE与REST请求一起使用,以获取从客户端到服务器的其他信息。 But on the other hand, SSE comes with certain caveats, like for instance in Javascript implementation, you can not overwrite headers. 但另一方面,SSE带有一些警告,例如在Javascript实现中,你不能覆盖标题。 The only solution is to pass query parameters, but then you can face an issue with the query string size limit. 唯一的解决方案是传递查询参数,但是您可能会面临查询字符串大小限制的问题。 So, again, choosing between SSE and WebSockets really depends on the kind of application you need to implement. 因此,在SSE和WebSockets之间进行选择实际上取决于您需要实现的应用程序类型。 A few months ago, I had written a blog post that may give you some information: http://streamdata.io/blog/push-sse-vs-websockets/ . 几个月前,我写了一篇博文,可能会给你一些信息: http//streamdata.io/blog/push-sse-vs-websockets/ Although at that time we didn't consider HTTP2, this can help know what question you need to ask yourself. 虽然当时我们没有考虑HTTP2,但这可以帮助您了解自己需要问什么问题。

暂无
暂无

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

相关问题 当我们想使用Javascript通过WebSocket进行通信时,套接字如何将端口从HTTP 80更改为另一个端口? - How does a socket change ports from HTTP 80 to another port when we want to communicate over WebSocket using Javascript? 通过HTTP 2.0实现AJAX与Websocket REST的性能? - Performance of AJAX vs Websocket REST over HTTP 2.0? MQTT over WebSocket和SSE(服务器发送事件)之间的区别 - Difference between MQTT over WebSocket and SSE(Server Send Event) 使用websocket和http时的nginx配置? - nginx configuration when using websocket and http? 通过 Websocket 发送 WebRTC MediaStream(RTP over HTTP/Websocket) - Sending WebRTC MediaStream over Websocket (RTP over HTTP/Websocket) Apache:当需要HTTP 101时,Websocket通过代理返回HTTP 200 - Apache: Websocket returning HTTP 200 over proxy when needing HTTP 101 GitHub 使用什么进行实时更新? 没有更多的 websocket 或 SSE? - What is GitHub using for real-time updates? No more websocket or SSE? WebSocket Stomp over SockJS - http 自定义标头 - WebSocket Stomp over SockJS - http custom headers Websocket,Server Sent Events(SSE)和HTTP2的Server Pushing之间有什么区别? - What is the difference between Websocket,Server Sent Events (SSE) and HTTP2's Server Pushing? 当通过 HTTP 路由到后端时,Nginx Ingress 如何处理通过 HTTPS (wss:...) 传入的 WebSocket 连接? (ws:...)? - How does Nginx Ingress handle an incoming WebSocket Connection over HTTPS (wss:...) when routing to backend over HTTP? (ws:...)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM