Relationship between mpsc::unbounded_channel() and warp::filters::ws.split()

Hey everyone!
Im trying to create websocket server by using warp and tokio, I couldunt understand or little bit confused relationship between these :
let (client_ws_sender, mut client_ws_rcv) = ws.split();
and
let (client_sender, client_rcv) = mpsc::unbounded_channel();

Can anyone explain the relationship between client_ws_sender, client_ws_rcv, client_sender, client_rcv ?
Here is the example piece of code

use tokio::sync::mpsc;
use tokio_stream::wrappers::UnboundedReceiverStream;
use warp::ws::{Message, WebSocket};

pub async fn client_connection(ws: WebSocket, clients: Clients) {
    println!("establishing client connection... {:?}", ws);

    let (client_ws_sender, mut client_ws_rcv) = ws.split();
    let (client_sender, client_rcv) = mpsc::unbounded_channel();
}

The split method on WebSocket is splitting it into a Stream half which receives incoming data on the connection and a Sink half which allows you to send data on the connection.

The mpsc channel also has a receiver half and a sender half, but they are used for sending messages within a program. Other than the similarities in the API the implementations aren't really related at all. One is an abstraction for working with a network connection, the other is a mechanism for exchanging messages between threads.

You also don't need to call split to work with a WebSocket, that's just one option.

Is there a specific thing you're trying to do and aren't sure which to use? Or is this a theoretical question?

i have example code that using both together and works fine here it is basic-warp-websocket-server-tutorial/ws.rs at main · tmsdev82/basic-warp-websocket-server-tutorial · GitHub

I think you should be able to get rid of the mpsc channel, nothing there looks like you need to send messages to the WebSocket via an mpsc channel to me.

Using them together is fine, but I'm not sure it's doing anything you couldn't do without the mpsc channel.

Hmm im still not sure totally, every example i check using together, there must be a reason.

Ahh okay the warp example has a comment about what the mpsc channel is doing

// Use an unbounded channel to handle buffering and flushing of messages
// to the websocket...

So the goal of the channel is to avoid forcing your internal app logic to wait for WebSocket messages to finish sending. The channel is basically just serving as an "outbound message queue".

One way to understand the code is as an extremely simple application of the actor pattern.

1 Like

I did a bit research and i see using mpsc unnecessary since there is oneshot: single-producer, single consumer channel, isnt it (for my example code) ? If u check the example here basic-warp-websocket-server-tutorial/ws.rs at main · tmsdev82/basic-warp-websocket-server-tutorial · GitHub

Whenever new client comes client_connection() getting trigger so there will be unique channel for each client thats means that single producer and consumer must be enough for each client ?
What is the reason of using mpsc rather than oneshot in this example then ?

Oneshot allows only one message to be sent before the channel is destroyed.

1 Like

ah so same client cant use twice the oneshot channel then right ? if its that why mpsc used

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.