9
submitted 1 month ago* (last edited 1 month ago) by SpiderUnderUrBed@lemmy.zip to c/rust@programming.dev

Hello, I am starting to learn and play around with tokio and multithreaded code. I am now playing around with websockets, I don't quite understand the difference between broadcast and mpsc, and when would you use either, I mean, I am assuming broadcast is intended for multiple clients, but multiple clients were able to connect to my mscp channel, and receive a bit of data (but it was weird and partial). So I don't quite get it.

you are viewing a single comment's thread
view the rest of the comments
[-] TehPers@beehaw.org 5 points 1 month ago

Tokio's broadcast module is a mpmc messaging queue.

The terms "mpsc" and "mpmc" refer to how the channels can be used. Breaking down the letters, there are two sides to a channel:

  • p is producer, or transmitter/sender. This side sends data
  • c is consumer, or receiver/reader. This side receives data.

Data only flows one way through a channel. Each side may have a restriction on how many members can be on that side:

  • s is single, so this side cannot be cloned. You only have one object that can interact with this side (so only one sender or only one receiver)
  • m is multiple, so this side can be cloned. Any number of objects can exist that interact with this side (so a bunch of simultaneous senders or receivers)

A mpsc channel can only have one consumer, so only one thread can receive messages at a time, and it can only receive those messages once.

A mpmc channel can have many consumers, so multiple threads can receive messages at once, and a thread can receive a message multiple times (through multiple receivers).

Both can have multiple simultaneous producers.

this post was submitted on 11 May 2025
9 points (100.0% liked)

Rust

7017 readers
35 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 2 years ago
MODERATORS