-
Notifications
You must be signed in to change notification settings - Fork 236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tokio_core::Framed for tokio_tungstenite? #32
Comments
Well, the
In theory, based on the interface of the trait I would assume that it's quite easy to implement both of them for the The reason why we did not implement I'm not sure if we're going to implement both of these traits in the near future, I'll have to check the current state of tokio, it seems that the current direction of tokio went in the direction opposite of |
Hi! I'm also interested in being able to use Encoder and Decoder (they seem to have been moved to the I'm very new to Rust and Tokio, so I'm not sure if I understood correctly, but it seems like this would require me to implement a Codec struct for which I would need to implement the The main problem I found with this is that it would require Any idea if this is possible? And if so, how could be implemented? Thanks! |
@jeffesquivels If you really want to have the The reason why As for the
Actually you can use them in this way even with the current state of |
@application-developer-DA thanks for your response! I didn't quite understand the last part (i.e. how it could be implemented in the current state), if you have any examples of projects that are using I think my main problem is that one of my application's message is composed of one or more WebSocket messages, which means sometimes I will need to wait for more WS messages to arrive before I can return one application message. I was thinking may be the right approach is to have my own |
I mean you could split the
Indeed, this is one of the approaches you may use! You may also want to to implement |
Ok, got it now.
Nice, thanks! I wanted to make sure trying this approach made sense; And yes, I will need |
@daniel-abramov I've noticed your comment in #260 and have been reading the discussion here, but I fail to fully understand what is possible and what is not 😊 I'm mainly looking to implement a codec to encode and decode data being send over a connected websocket into a custom message format. As far as I understand, it's not possible to send raw binary data over the websocket as So I guess the next best thing is to put our raw data into a websocket binary message (so as binary data). But as this is a high throughput connection, I wonder what this means for the overhead? Looking at the websocket specs I guess that the encoding/decoding done by Additionally I'm not sure how I can (if possible at all) use our own encoder/decoder as a "wrapper" around So reading the messages from the stream as they come; check if it's a binary message; grab the payload and decode/de-serialize it using @jeffesquivels can you maybe share what you ended up with? Thanks for both your time! |
That's totally possible (have always been 🙂).
What do you mean by raw binary data? - With WebSockets, you can send any type of data over the connection. I.e. if test messages are not what you want, you could use the binary data message to send any arbitrary
Right. I would say the throughput degradation is pretty negligible for the majority of use cases (especially if you don't have any alternative). That said, if the only thing that you need is to send a stream of binary data to the server and you have the possibility to choose any protocol, then the WebSockets might not necessarily be the best option here (I would say a plain TCP stream or QUIC would be a better choice here).
Yep, this sounds sensible. Though you could actually elegantly combine them as
I think w.r.t. the protocol, WebSockets might not necessary be the most performant option here (in general), irrespective of the library that you choose to use for the WebSocket. I.e. if you have a choice, you could choose a more well-suited protocol. However, depending on your requirements, using the approach that you described may not be that bad actually. I think the majority fo the users send some data over WebSockets that they end up serializing/deserializing in some format (e.g. usage of protocol buffers together with |
I meant that I cannot "just" send raw bytes over the wire (like with a
So this is probably the most important and interesting part (for me)... We already have an implementation of the Tokio encoder and decoder traits for our protocol, but I don't understand (I probably leak some general knowledge around this topic) how to combine those parts? Is there a way to get a "stream of payloads" from I understand that I might be asking for a lot, but any additional info or any pointers to an example would be highly appreciated!!
I'm afraid we have some constraints that cannot be met with a regular TCP stream or QUIC, so we are kind of forced to use websockets here 😏 |
After discussing our requirements once more, we came tot the conclusion that we can use a Thanks for triggering us to rethink/check our requirements, as the end result is much better now! |
Sorry for the late reply, glad that you found a more appropriate solution meanwhile!
I see. Probably the easiest way would be wrapping the sink together with the encoder and the stream with the decoder.
You could wrap your decoder along with the reading part of Or even something simpler like this: let (read, write) = websocket.split();
let decoded_stream = read.map(|msg| decoder.decode(msg.into_bytes_mut())); That way you turn a stream of |
Thank you very much for the additional info @daniel-abramov! Very helpful and I'm sure I'm going to need/use this somewhere in the near future. Thanks! |
Just incase anyone comes upon this later, a small example of using this library with tokio codecs: https://github.com/conorbros/websocket-experiment |
I'm attempting to implement websockets for the rumqtt MQTT library and I'm running into some issues using this crate. My main problem is mentioned here:
AtherEnergy/rumqtt#70
But what I'd like to do is create an instance of tokio_core::Framed (to keep consistency with the rest of rumqtt) from a WebSocketStream. I am unsure how I could implement Read / AsyncRead for WebSocketStream, and was wondering if you had any suggestions?
I can't really see a way to do it with the current interfaces, since as far as I can tell, WebSocketStream is essentially equivalent to Framed, but without the ability to add encoders and decoders
I'm extremely new to tokio, so forgive me if I've completely misunderstood anything here.
The text was updated successfully, but these errors were encountered: