简体   繁体   English

用于双向消息流的 HTTP/2 与 Web 套接字

[英]HTTP/2 vs web-sockets for bidirectional message streaming

Besides for browsers, which do not expose HTTP/2 frames to Javascript, in what case would Websockets be a better choice than something like bidirectional streaming gRPCs (built on HTTP/2) for implementing real-time bidirectional message streams?除了不向 Javascript 公开 HTTP/2 帧的浏览器之外,在什么情况下 Websocket 会比双向流 gRPC(基于 HTTP/2)之类的东西更好地实现实时双向消息流? Also, doesn't HTTP 2.0 being full-duplex (and bidirectional) mean that server pushes are in fact supported?另外,HTTP 2.0 是全双工(和双向)是否意味着实际上支持服务器推送? What is the need for something like SSE, then?那么,对 SSE 之类的东西有什么需要呢? This is made obsolete, right?这已经过时了,对吧?

There is many aspects here.这里有很多方面。 Server Sent Events, JavaScript Streams API is mostly browser APIs for lower level protocols.服务器发送事件,JavaScript Streams API 主要是用于较低级别协议的浏览器 API。

Server to Server communication服务器到服务器通信

Server to Server communication using Websockets and HTTP/2 has similar properties.使用 Websockets 和 HTTP/2 的服务器到服务器通信具有类似的特性。 Both are binary and efficient protocols.两者都是二进制和高效的协议。 HTTP/2 provides per stream backpressure which can be important for clients that receives push messages from multiple sources or may be busy at times. HTTP/2 提供每个流的背压,这对于从多个源接收推送消息或有时可能很忙的客户端来说可能很重要。 gRPC is a framework on top of HTTP/2 and provides a higher abstraction to the developer. gRPC 是一个基于 HTTP/2 的框架,为开发者提供了更高的抽象。

Server to Browser communication服务器到浏览器通信

Server Sent Events服务器发送事件

Server Sent Events is a way for a client to subscribe to an event stream and continuously receive events from a server. 服务器发送事件是客户端订阅事件流并不断从服务器接收事件的一种方式。 The API is a higher abstraction and easier to use than alternatives. API 是一个更高的抽象和比替代品更容易使用。 However the format is a specified message format in text .但是,格式是text 中指定的消息格式。

Developing a web application that uses server-sent events is easier than with websockets.开发使用服务器发送事件的 Web 应用程序比使用 websockets 更容易。 You'll need a bit of code on the server to stream events to the front-end, but the client side code works almost identically to handling any other event.您需要在服务器上编写一些代码来将事件流式传输到前端,但客户端代码的工作方式几乎与处理任何其他事件相同。

Example code示例代码

const evtSource = new EventSource("/v1/stream/topic");

evtSource.onmessage = function(event) {
   // handle event
}

JavaScript Streams API JavaScript 流 API

JavaScript Streams API is a newer JavaScript API for supporting binary streams between browser and server. JavaScript Streams API是一种较新的 JavaScript API,用于支持浏览器和服务器之间的二进制流。 This can be used with the newerReadableStream from Fetch API .这可以与来自Fetch API的较新的ReadableStream一起使用。 Since this is a binary stream, it can have a broader use case and may be important for applications using eg webassembly in upcoming years.由于这是一个二进制流,它可以有更广泛的用例,并且对于未来几年使用 webassembly 等应用程序可能很重要。

The API is a bit more complex. API 有点复杂。 Example code:示例代码:

fetch("https://www.example.org/").then((response) => {
  const reader = response.body.getReader();
  const stream = new ReadableStream({
    start(controller) {
       // implementation
    }
  })

The advantage with a binary stream is that it may be used for naturally binary data eg audio or binary respresentations of custom formats.二进制流的优点是它可以用于自然的二进制数据,例如自定义格式的音频或二进制表示。

Using JavaScript Streams API on HTTP/2 or HTTP/3 Streams has the advantage over alternatives in that it supports backpressure per stream (not only per TCP connection).在 HTTP/2 或 HTTP/3 Streams 上使用 JavaScript Streams API 比替代方案更具优势,因为它支持每个流(不仅仅是每个 TCP 连接)的背压

gRPC gRPC

gRPC is a protocol using HTTP/2 streams, but it is not implemented on the JavaScript Streams API, so for browser communication it needs some middleware like grpc-web . gRPC 是一个使用 HTTP/2 流的协议,但它没有在 JavaScript Streams API 上实现,因此对于浏览器通信,它需要一些中间件,如grpc-web

WebSockets网络套接字

WebSockets is a lower level abstraction and it has broader browser support and it supports full-duplex communication. WebSockets 是较低级别的抽象,它具有更广泛的浏览器支持,并且支持全双工通信。 But since it is a lower level abstraction it often requires libraries to handle the communication.但由于它是较低级别的抽象,因此通常需要库来处理通信。

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

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