Most efficient way to broadcast messages globally across main script and workers?
I basically want a simple message bus that is global to all my workers + the main script, without relying on any dependency.
Is there something more efficient, maybe using MessageChannel, than using postMessage() in workers to send things to the main script and iterate through all workers in the main script to postMessage() again on each worker?
11 Replies
BroadcastChannel
could workThanks @Andreu Botella (they/them) !
I didn't know that API, it looks like it's exactly what I want.
Hmm 😕
--unstable
Oh…
it works, yay!
How do you pass the broadcast channel to each worker? Is it a transferable object?
type Transferable
doesn't list itno, if you create multiple broadcastchannels with the same string passed to the constructor, they get linked together
I see. Thank you very much!
even if they're created from different workers
BroadcastChannel works great for me.
How does that work exactly? What are the use cases?
the initial use case in the browser was to let every tab from a same origin do multiple publisher multiple consumer communication
like, you log in on one tab and it notifies every other tab so it shows you as logged in without having to refresh
and you might want multiple different channels, for different things, so you give them different names
technically it works much like
postMessage
and MessagePort
, except deno knows all of the BroadcastChannel
s in existence
I thinkI think
BroadcastChannel
works best for my use case but, for reference, let me also mention Deno Queues if you want at-least-once delivery guarantees, persistance, and communication across deno
instances when using the new denokv
service.
https://deno.com/blog/queuesDeno Blog
Announcing Deno Queues
Introducing Deno Queues - zero config, scalable messaging with a guaranteed
at-least-once delivery. This new primitive builds on the foundation set by
Deno KV, and is available today in the Deno JavaScript runtime and Deno
Deploy.