D
Deno

help

Most efficient way to broadcast messages globally across main script and workers?

Jjcayzac11/15/2023
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?
ABTAndreu Botella (they/them)11/15/2023
BroadcastChannel could work
Jjcayzac11/15/2023
Thanks @Andreu Botella (they/them) ! I didn't know that API, it looks like it's exactly what I want.
> const bc = new BroadcastChannel("test_channel");
Uncaught ReferenceError: BroadcastChannel is not defined
at <anonymous>:2:12
> const bc = new BroadcastChannel("test_channel");
Uncaught ReferenceError: BroadcastChannel is not defined
at <anonymous>:2:12
Hmm 😕
ABTAndreu Botella (they/them)11/15/2023
--unstable
Jjcayzac11/15/2023
Oh… it works, yay! How do you pass the broadcast channel to each worker? Is it a transferable object? type Transferable doesn't list it
ABTAndreu Botella (they/them)11/15/2023
no, if you create multiple broadcastchannels with the same string passed to the constructor, they get linked together
Jjcayzac11/15/2023
I see. Thank you very much!
ABTAndreu Botella (they/them)11/15/2023
even if they're created from different workers
ABAltair 680b11/15/2023
BroadcastChannel works great for me.
Eeric.uhd11/15/2023
How does that work exactly? What are the use cases?
ABTAndreu Botella (they/them)11/15/2023
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 BroadcastChannels in existence I think
Jjcayzac11/16/2023
I 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/queues
Deno 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.

Looking for more? Join the community!

Recommended Posts
What is the process for deploying a jupyter notebook made using deno?I have webdev experience and was using Observable. I was looking for alternatives that use javascripPostGraphile on DenoDoes PostGraphile work with Deno? Trying to see if I can run PostGraphile as a serverless function oIs there any way to disable Deno's Intellisense feature without disabling Deno itself?When programming with Deno, I've noticed how relatively slow the lang server is, and I need intellisDeploying Lume to github pagesI'm totaly new to this, but have been following instructions to build a site with lume. It works locBest hardware for Deno development?I am looking for a new laptop / desktop for Deno development as my current setup seems no longer viaHow do I make the crypto.subtle.digest algo flexible?This is a learning moment, bear with me. 😅 I can hard code an algo for the digest like this: ```twhat’s the current best practice for bundling?Is it deno esbuild? Wondering what supports npm specifiers and bundling atmHow do I get a d.ts file to be used?I'm extending a type from `hono` by adding that code in a `hono.d.ts` file in my `src` folder. ```“Log streaming API” on Deno DeployWhat does “Log streaming API” refers to in the analytics section of the pricing page of Deno Deploy?Read-only FS detectionHi, I have few deps that by default create cache/log dirs... I can disable it, but the detection itsIs there a built-in parser for the string that Deno.inspect produces?I found out in another thread that Deno.inspect(myObj) produces a visually gorgeous string representHow to get the nice Deno console.log format into a string?When I do a console.log on an instantiated class object, I get a beautiful format in the terminal, tbyonm - force use of DenoWhen I use byonm enabled in my deno.json, and say I have a vite project triggered from a package.jsoCan I disable deno linting for individual files, or a path?I'm using VSCode and the Deno extension. I want to use the inbuilt functionality of VSCode for type Rust run JS/TSHow can I embed this project into a rust application so that rust can run js ts and has runtime api Set-Cookie - Header shows as "deleted"I am trying to use deno to script/scrape a website. I can successfully log in to the site (using fetI keep running into python download failed while trying to run 'cargo test' commandI am currently following the steps I. Deno's Contributing Guide and I am running cargo test but I kehow I can create an restful api with deno and firebase as database?how I can create an restful api with deno and firebase as database?How can I replace writeAllSync with the Streams API?How would one best refactor the following code to use the Web Streams API as suggested by the deprecIs possible to run nodejs (typescript) project with esm modules?I'm trying to run small nodejs project with esm modules. Simple example ```ts // foo.ts import { b