WebSockets support.
See original GitHub issueCurrently httpx
is squarely focused on HTTP’s traditional request / response paradigm, and there are well-established packages for WebSocket support such as websockets. In an HTTP/1.1-only world, this split of responsabilities makes perfect sense as HTTP requests / WebSockets work independently.
However, with HTTP/2 already widely deployed and HTTP/3 standardisation well under way I’m not sure the model holds up:
- implementations such as
websockets
are usually tied to HTTP/1.1 only, whereashttpx
has support for HTTP/2 (and hopefully soon HTTP/3) - we are missing out on the opportunity to multiplex “regular” HTTP request with WebSocket connections
Using the sans-IO wsproto
combined with httpx
’s connection management we could provide WebSocket support spanning HTTP/1.1, HTTP/2 and HTTP/3. What are your thoughts on this?
One caveat: providing WebSocket support would only make sense using the AsyncClient
interface.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:19
- Comments:40 (18 by maintainers)
Top Results From Across the Web
Web Sockets | Can I use... Support tables for HTML5, CSS3, etc
"Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers.
Read more >The WebSocket API (WebSockets) - Web APIs - MDN Web Docs
desktop desktop
Chrome Edge
WebSocket Full support. Chrome4. Toggle history Full support. Edge12. Toggl...
WebSocket() constructor Full support. Chrome4. Toggle history Full support. Edge12. Toggl...
Read more >WebSocket - Wikipedia
Most browsers support the protocol, including Google Chrome, Firefox, Microsoft Edge, Internet Explorer, Safari and Opera. Unlike HTTP, WebSocket provides full- ...
Read more >WebSockets support in ASP.NET Core - Microsoft Learn
In this article. Http/2 WebSockets support; SignalR; Prerequisites; Configure the middleware; Accept WebSocket requests; Add HTTP/2 WebSockets ...
Read more >WebSockets Support - Spring
WebSockets Support. Starting with version 4.1, Spring Integration has WebSocket support. It is based on the architecture, infrastructure, and API from the ...
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
We’ve actually got some neat stuff we can do around this now, but it needs documenting. Going to keep this open to track that.
See the
stream
extension in thehttpcore
docs for a rough outline.So my initial take on this is that we’d want to first expose a connection upgrade API, and then built websockets support over that, so something like…
Then at the client level, have websocket support, that uses
connect
under the hood.There’s a couple of potential awkwardnesses about that tho…
httpx
, rather than having it inhttpcore
.ASGITransport
would need to de-marshall the bytes on the connection back into websocket events, rather than “seeing” an API abstraction that’s at the level of the events themselves.So, after a bit more thinking, I reckon we should instead aim to provide a Transport-level interface specifically for websockets.
This wouldn’t preclude us possibly also adding a raw “just give me the connection” level abstraction too at some other point, in order to provide for low-level
CONNECT
,Upgrade
andHTTP/2
bi-directional streaming support. But we could treat that as a lower priority, depending on if we’re actually seeing any demand/use-cases for exposing those capabilities.The information returned from the low-level
websocket
method would need to be all the standard response code/headers stuff, plus an interface that just exposes something likesend_event()
,receive_event()
andclose()
.There’s also the question of what the API ought to look like at the
httpx
level. One thing that tends to be a bit fiddly here is that websockets can return eitherbytes
orstr
frames, but we’d still like our users to be able to access the data in a nice type-checked fashion. Rather than expose multiple kinds of send/receive methods, we might be able to do with just these, by…ws.send(text=...)
to distinguish on sending.msg = ws.receive(); print(msg.text)
to distinguish on receiving. The.text
property could strictly returnstr
, and could raise an error if the received data frame was actually in the unexpected binary mode.json
argument in both case. We could default to handling JSON over text frames. Optionally we might include a flag on theclient.websocket()
method setting the expected mode to either text or binary, and using that for the JSON framing, plus erroring out if the incorrect style is sent/received anytime.We can transparently deal with ping/pong frames during any
.send()
/receive()
, and context managed async transports could also send background pings.We might well also want
.iter_bytes()
,.iter_text()
, and.iter_json()
methods, for convenience.I’m aware this is all in a bit of a “jotting down” style, please do feel free to let me know if I’m not being clear enough about anything here.