-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
- I have looked for existing issues (including closed) about this
Feature Request
It'd be super nice to update to the latest version of [tokio-]tungstenite.
Motivation
The newest versions of tungstenite have Bytes as the payload type in Message instead of Vec<u8>, which lets you send shared data a client, e.g. sending the same message to N different clients without allocating N different buffers. Especially with axum 0.8 coming out as an API-breaking release, this would be super nice to have.
Proposal
Update to tungstenite 0.26 and replace Vec<u8> in axum::extract::ws::Message with Bytes.
The one tricky thing is that for Text, tungstenite defines a Utf8Bytes type, which is a wrapper over Bytes that guarantees utf8 validity (like String). There are a few different options for what type to use in axum's Message::Text variant, none of them obviously the right choice:
- Use
String, copying theUtf8Bytesinto it.- This obviously isn't ideal, as it means users won't be able to get those benefits listed above if they're sending Text data. It is, however, the easiest in terms of API design.
- Use
tungstenite::Utf8Bytes.- This is probably a no-go, since axum meticulously wraps all of tungstenite's types, so it wouldn't make sense to make an exception just for this.
- Make a new axum version of
Utf8Bytes- This would mean a new API surface that's not actually websocket-specific, but actually a weirdly useful general utility for a utf8
Byteswrapper. Definitely doable, but means there might be further feature requests forstrversions ofBytesmethods/Bytesversions ofstrmethods, and also that the user will need to do conversions if they're already using a version of this type from something likebytestringorbytes-utils.
- This would mean a new API surface that's not actually websocket-specific, but actually a weirdly useful general utility for a utf8
- Use an existing version of this type, like
bytestring::ByteStringorbytes_utils::Str.- This adds a new public dependency, and both these crates are popular with roughly the same amount of downloads, meaning there's no obvious right choice.
- Add a new type to the
bytescrate and use that.- This is my personal pick if I had the say, but I'm not on the tokio dev team and it's obviously a much bigger task than any of the other ones. It'd require designing and adding tons of new API surface to a whole 'nother crate in the tokio ecosystem, which if this was to be worked on for axum 0.8 would delay it quite a bit. Maybe 0.9? 👀