You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to make a chat API, where the user sends a message to the API to send a message, and the API sends a message to the user to tell them about an incoming message.
So, I want to both listen for incoming messages and have the ability to send them. But since write() takes a mutable reference, I cannot have a read() on a separate thread, and I couldn't find a method to check if a message had come in.
What am I missing? Seems like a common use case
The text was updated successfully, but these errors were encountered:
The problem you're facing is not really related to Tungstenite. You'll get exactly the same problem with just plain TcpStream from the standard library. And there is a very good reason why it is designed in such a way.
You need some kind of task planner in order to do that since you can't read and write at the same time in the same thread. And this is not so straightforward. A low-hanging fruit is to just start two threads, and it would kinda "work" in languages like C++, but it is prone to race conditions, and Rust will reject such attempts. Your code has to be smart enough to decide when it is safe to write and to read and in which order.
Fortunately, there are many readily-made options to do that. Most users choose Tokio here. We provide a binding named tokio-tungstenite for it. There are other bindings like async-tungstenite and many others for specific web frameworks.
I am trying to make a chat API, where the user sends a message to the API to send a message, and the API sends a message to the user to tell them about an incoming message.
So, I want to both listen for incoming messages and have the ability to send them. But since
write()
takes a mutable reference, I cannot have aread()
on a separate thread, and I couldn't find a method to check if a message had come in.What am I missing? Seems like a common use case
The text was updated successfully, but these errors were encountered: