Streaming over WebSocket

The WebSocket protocol is message based.

And that's a problem. We want to stream a continuous flow of information, much like the WebSocket protocol does in our last blog post. If you're familiar with our diagram from the protocol post you'll recall WebSocket messages are made up of a starting text or binary message followed by continuation frames that allow the break up of large messages. This is essentially what we want but for our application.

So how do we do it?

Write your own websocket protocol parser. While somewhat of a joke this is likely necessary depending on your language and ecosystem. What we want is for a library that parses text/binary starting frames and feeds the payload directly to our application. Subsequently, when continuation frames arrive the payload is also delivered immediately. Most frameworks are going to offer message-based API abstractions like defining an onMessage handler that waits for all the data to arrive before firing.

Application protocol?

Now you're application needs its own protocol for determining the bounds of data. It could be deliminated with a sequence not found in the data. Or it can have a leading size indicator or in general a header with information that controls parsing. In both cases your websocket powered application will need to be cautious about how much it trusts the input.

Okay, what about the browser?

Right, so the browser's not going to give us anything more than WebSocket anytime soon. The new cool thing is WebTransport and deserves more attention, there's work to make that available over http/2 so half the internet may be able to use it, woo! Since we control the server and have already written our own solution, the best we can do is control the way we construct responses. If we want the browser to get delivered messages in streaming chunks as well, then we're going to need to send each chunk as a complete message with a FIN bit set. If we send payloads across continuation frames the browser won't fire our listener until its received that FIN message.