What is the advantage over tungstenite vs tungstenite async?

I'm trying to dive into the WebSocket ecosystem and can't tell which solution to use.
I am used to really easy to use websocket libraries such as ws in JS.

It works like

const ws = ws.connect(url)

ws.on('message', || {}) 
ws.on('error', || {}) 
ws.on('close', || {}) 

Something similar and easy to use here too?

Rust doesn't have a callback based websocket library. Callbacks are generally very inconvenient to use in Rust.

and why are they so inconvenient to use? should they be avoided if possible?

Callbacks are inconvenient in Rust because they clash with the single-ownership design that Rust uses. Values accessed in callbacks generally need to be accessed from outside the callback as well, which requires that the object is shared in some way. Sharing generally involves something like Rc/RefCell, which is generally considered inconvenient to work with.

The most popular WebSocket library would be tungstenite, which can either be used async with tokio-tungstenite (tokio_tungstenite - Rust), or without async with tungstenite (tungstenite - Rust).

They are stream-based solutions where you call a function in a loop to get new messages.

If you are providing a websocket server as part of your web application, you may also consider looking into whatever web framework you use provides.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.