Uh oh!
There was an error while loading. Please reload this page.
workers: experimental BroadcastChannel - #36271
Conversation
addaleax
left a comment
There was a problem hiding this comment.
I’ll have to think a bit about how this works on the C++ side … this definitely breaks the MessagePort/MessagePortData encapsulation in a big way, and probably in a way that can be avoided, and probably with less code than this would currently introduce…
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
addaleax
commented
Nov 26, 2020
Basically, what I think I’d rather aim for is:
|
@jasnell Fwiw, if you want, I can probably find the time to write a prototype for that suggestion, and you can see if you like it or not :) |
jasnell
commented
Nov 26, 2020
Thanks :-) I've already started working it up (yes it's a holiday here but this is Fun Coding not Work Coding so it's all good :-)...lol). The |
addaleax
commented
Nov 26, 2020
Yeah, I think there’s not much we can do besides fail … but we can do that during sending, right? |
jasnell
commented
Nov 26, 2020
I think so. We can fail in PostMessage if the |
8a74b55 to
08ab8d6Comparejasnell
commented
Nov 26, 2020
@addaleax ... ok, completely revised the implementation as discussed. Please take a look. Here, I'm done for the day and ready to go eat some turkey. |
addaleax
left a comment
There was a problem hiding this comment.
Yeah, I really like where this is going :)
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
08ab8d6 to
e31c009Comparejasnell
commented
Nov 30, 2020
@addaleax@benjamingr ... marking this ready for review now. There's still work that can be done on this to ensure that it's completely up to spec. |
nodejs-github-bot
commented
Nov 30, 2020
addaleax
left a comment
There was a problem hiding this comment.
Looks good, apart from the error code thing :)
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
e31c009 to
d1c68a2Comparejasnell
commented
Nov 30, 2020
@addaleax .. updated! :-) |
Uh oh!
There was an error while loading. Please reload this page.
d1c68a2 to
5e06220Comparenodejs-github-bot
commented
Nov 30, 2020
nodejs-github-bot
commented
Nov 30, 2020
Signed-off-by: James M Snell <jasnell@gmail.com>
94d2d6f to
6066197Compare| Closes the `BroadcastChannel` connection. | ||
| ### `broadcastChannel.onmessage` |
There was a problem hiding this comment.
Any reason these are documented as event handlers and not event "message" etc?
There was a problem hiding this comment.
Because from the documentation I've been able to find on web usage, the onmessage pattern tends to be more common. We can tweak the documentation later if necessary.
There was a problem hiding this comment.
Just to clarify because of the "we can tweak the docs later" comment - a LGTM from me with comments always means "This is fine to land but I have these comments/questions we should probably address at some point".
If I think something isn't ready to land I never LGTM, I typically (like in this case) check the code out, play with it, put a debugger (in test/parallel/test-worker-broadcastchannel.js here, kind of annoying in worker_threads because I do it from ndb and not vscode), add some logs and LGTM.
I typically don't leave style nits because I usually really don't care about those.
There are a few (very minor) nits here that I might follow up with a PR about later if someone ever complains about them namely stuff like onmessage not being logged when you util.inspect a BroadcastChannel here but it being logged in Chrome. I honestly think these things aren't worth mentioning in PRs most of the time.
benjamingr
left a comment
There was a problem hiding this comment.
JS LGTM
The API itself leaves a lot to be desired but I guess we should follow the DOM's MessagePort so little we can do about it here :]
Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #36271 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
jasnell
commented
Dec 1, 2020
Landed in 9e446b3 |
This addresses the `TODO` left on my request in 9e446b3. :) Refs: nodejs#36271
This looks like it landed without a CI run after the rebase. Likely related: Windows CI now fails with the test added here. |
Trott
commented
Dec 2, 2020
I don't have a Windows machine to test on locally, but I've optimistically opened #36353 changing the expected Windows behavior to be the spec-compliant behavior mentioned in the comment. If that passes CI, then I'll propose fast-tracking. |
Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #36271 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
PR-URL: #36435 Notable changes: * child_processes: * add AbortSignal support (Benjamin Gruenbaum) (#36308) * deps: * update ICU to 68.1 (Michaël Zasso) (#36187) * events: * support signal in EventTarget (Benjamin Gruenbaum) (#36258) * graduate Event, EventTarget, AbortController (James M Snell) (#35949) * http: * enable call chaining with setHeader() (pooja d.p) (#35924) * module: * add isPreloading indicator (James M Snell) (#36263) * stream: * support abort signal (Benjamin Gruenbaum) (#36061) * add FileHandle support to Read/WriteStream (Momtchil Momtchev) (#35922) * worker: * add experimental BroadcastChannel (James M Snell) (#36271)
PR-URL: #36435 Notable changes: * child_processes: * add AbortSignal support (Benjamin Gruenbaum) (#36308) * deps: * update ICU to 68.1 (Michaël Zasso) (#36187) * events: * support signal in EventTarget (Benjamin Gruenbaum) (#36258) * graduate Event, EventTarget, AbortController (James M Snell) (#35949) * http: * enable call chaining with setHeader() (pooja d.p) (#35924) * module: * add isPreloading indicator (James M Snell) (#36263) * stream: * support abort signal (Benjamin Gruenbaum) (#36061) * add FileHandle support to Read/WriteStream (Momtchil Momtchev) (#35922) * worker: * add experimental BroadcastChannel (James M Snell) (#36271)
PR-URL: #36435 Notable changes: * child_processes: * add AbortSignal support (Benjamin Gruenbaum) (#36308) * deps: * update ICU to 68.1 (Michaël Zasso) (#36187) * events: * support signal in EventTarget (Benjamin Gruenbaum) (#36258) * graduate Event, EventTarget, AbortController (James M Snell) (#35949) * http: * enable call chaining with setHeader() (pooja d.p) (#35924) * module: * add isPreloading indicator (James M Snell) (#36263) * stream: * support abort signal (Benjamin Gruenbaum) (#36061) * add FileHandle support to Read/WriteStream (Momtchil Momtchev) (#35922) * worker: * add experimental BroadcastChannel (James M Snell) (#36271)
targos
commented
Aug 4, 2021
This will need a backport to land on v14.x |
Currently very experimental and definitely not finished implementation of
BroadcastChannelfor Node.js.The intent for this is for it to be completely compatible with the browser API. It's not there yet.
For folks who are not familiar with
BroadcastChannel, it is essentially a one-to-many alternative toMessageChannel.All
BroadcastChannelinstances that share the same channel name, created in the main thread or worker threads, will receive a copy of the posted message.Message delivery is at-most once, with no retention. When a message is posted, it is synchronously copied and queued into each of the other instances. Those other instances will deliver the message copy asynchronously.
Transferables are not supported with
BroadcastChannel, so only messages that can be cloned are supported.One use case of
BroadcastChannelis to signal a group of worker threads that a process is shutting down, without having to send individual signals to each worker:Worker:
Main:
@addaleax: I'd be interested in your opinions on how to improve the C++ part of the implementation.
@benjamingr: I'd be interested in your thoughts on the JavaScript side of the implementation.
This is definitely a work in progress still, motivated by some needs around Piscina.
Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passes