Can no longer async stream fragmented message through 21.9 websockets
See original GitHub issueIn the legacy Webscokets implementation you could async stream a fragmented response through the websocket using an AsyncIterator. This seems to not be supported anymore in the 21.9 sanic implementation of websockets as the websocket.send() now only accepts str or bytes. Iterables seem to have a not implemented placeholder in the source (sanic.server.websockets.impl.WebsocketImplProtocol.send()). Is this a feature that will be added in the future?
Example:
websocket.send(async_response_stream_iterator())
async def async_response_stream_iterator():
yield b"binary data fragment 1"
yield b"binary data fragment 2"
Issue Analytics
- State:
- Created 2 years ago
- Comments:19 (17 by maintainers)
Top Results From Across the Web
Control not released while sending multiple messages #865
TL;DR: Sending many messages in a loop (via a loop of await ws.send(...) or via passing an async generator to send() once), does...
Read more >Both sides - websockets 10.4 documentation
message is an asynchronous iterator that yields control. Stopping in the middle of a fragmented message will cause a protocol error and the...
Read more >A websocket's ReceiveAsync method does not await the entire ...
ReceiveAsync () in a loop and accumulate the received chunks, until WebSocketReceiveResult.EndOfMessage is true or an error has occured. On a ...
Read more >WebSockets support in ASP.NET Core - Microsoft Learn
Learn how to get started with WebSockets in ASP. ... ObjectDisposedException: Cannot write to the response body, the response has completed.
Read more >26. WebSocket Support - Spring
WebSocket does imply a messaging architecture but does not mandate the use of ... bytes into a stream of messages (either text or...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Sorry I’m late to the party on this one, I must’ve missed it in my inbox.
During the development of the new Websockets backend before the release Sanic 21.9, I spoke briefly to the the
websockets
developer @aaugustin about specifically this issue, that is, a single message, over multiple fragments as described by @tylersalminen . Short story, is that is very difficult to do using the newwebsockets
sans-io interface, and not supported by the sans-io interface (as far as I am aware).It is not as simple as an iterator generating multiple messages to send, that pattern is easily implemented at the handler level as shown by others in this thread.
The new Sanic Websockets backend has no inter-message state. It communicates data to send directly to the
websockets
sans-io api layer, which does its own state management. Its probably possible to manipulate the api to get fragement-level sends working, but as I said, I don’t think that is a supported usage pattern.@aaugustin Is it correct what I said above? Do you have any further insights on this feature?
I would have a hard time giving a good use case.
I have anecdotal evidence that sending fragmented messages is rare. It took several years I introduced compression in websockets until someone noticed that I misunderstood the spec and that browsers would reject fragmented, compressed messages generated by websockets. Compression was enabled by default. This shows that almost no one uses fragmentation.
“We don’t support fragmentation, sorry” is a defensible position. I think the amount of time I spent supporting fragmentation in websockets is out of proportion of the usefulness of this “feature” in the real world.