Search code examples
websockethttp2

Is there a way to support multiplexing in WebSocket? similar to stream id in http/2


WebSocket is bidirectional and full duplex like http/2. Therefore, the client can make multiple requests simultaneously.

There are concept on stream id in http/2 to support multiple requests can be interleaved and processed by the server in out of order manner. For example, client can send multiple requests r1,r2 and r3 that have stream id 1,3 and 5 respectively. And then the server can handle them and responds whenever the server completes client request. Let's assume in this case, r2, r3 and r1. Therefore, the client may receive the results in order r2, r3 and r1. The client can distinguish the results on which one is for the request by using stream id.

How can we do that in WebSocket? Does we need to implement it in application layer? Something like including stream id in WebSocket payload? Or The server should guarantee the result in order r1, r2 and r3.


Solution

  • How can we do that in WebSocket? Does we need to implement it in application layer?

    Yes, you need to implement this on-top of WebSocket. Or you can perhaps use HTTP/2 with bi-directional streams instead? since it already has this functionality.