Skip to content

Commit e7d2e56

Browse files
worker: expose sender on BroadcastChannel MessageEvent
1 parent 9b911b3 commit e7d2e56

3 files changed

Lines changed: 71 additions & 4 deletions

File tree

doc/api/worker_threads.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,65 @@ added: v15.4.0
995995
996996
* `message` {any} Any cloneable JavaScript value.
997997
998+
### `messageEvent.sender`
999+
1000+
<!-- YAML added: REPLACEME -->
1001+
1002+
> Stability: 1 - Experimental
1003+
1004+
Type: {number}
1005+
1006+
A Node.js-specific extension that identifies the sender of a
1007+
BroadcastChannel message.
1008+
1009+
When a message originates, sender is the threadId of the
1010+
worker that posted the message.
1011+
1012+
```mjs
1013+
import {
1014+
isMainThread,
1015+
BroadcastChannel,
1016+
Worker,
1017+
} from 'node:worker_threads';
1018+
1019+
const bc = new BroadcastChannel('hello');
1020+
1021+
if (isMainThread) {
1022+
let c = 0;
1023+
bc.onmessage = ({ data, sender }) => {
1024+
console.log(`Received "${data}" from worker ${sender}`);
1025+
if (++c === 10) bc.close();
1026+
};
1027+
for (let n = 0; n < 10; n++)
1028+
new Worker(new URL(import.meta.url));
1029+
} else {
1030+
bc.postMessage('hello from every worker');
1031+
bc.close();
1032+
}
1033+
```
1034+
1035+
```cjs
1036+
const {
1037+
isMainThread,
1038+
BroadcastChannel,
1039+
Worker,
1040+
} = require('node:worker_threads');
1041+
1042+
const bc = new BroadcastChannel('hello');
1043+
1044+
if (isMainThread) {
1045+
let c = 0;
1046+
bc.onmessage = ({ data, sender }) => {
1047+
console.log(`Received "${data}" from worker ${sender}`);
1048+
if (++c === 10) bc.close();
1049+
};
1050+
for (let n = 0; n < 10; n++)
1051+
new Worker(__filename);
1052+
} else {
1053+
bc.postMessage('hello from every worker');
1054+
bc.close();
1055+
}
1056+
```
9981057
### `broadcastChannel.ref()`
9991058
10001059
<!-- YAML

lib/internal/worker/io.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,15 @@ function receiveMessageOnPort(port) {
347347
}
348348

349349
function onMessageEvent(type, data) {
350-
this.dispatchEvent(lazyMessageEvent(type, { data: data.value, source: data.source }));
350+
const event = lazyMessageEvent(type, { data: data.value });
351+
ObjectDefineProperty(event, 'sender', {
352+
__proto__: null,
353+
value: data.sender,
354+
enumerable: false,
355+
writable: false,
356+
configurable: false,
357+
});
358+
this.dispatchEvent(event);
351359
}
352360

353361
function isBroadcastChannel(value) {
@@ -421,7 +429,7 @@ class BroadcastChannel extends EventTarget {
421429
*/
422430
postMessage(message) {
423431
const broadcastMessage = {
424-
source: threadId,
432+
sender: threadId,
425433
value: message,
426434
};
427435
if (!isBroadcastChannel(this))

test/parallel/test-worker-broadcastchannel.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ assert.throws(() => new BroadcastChannel(), {
195195
196196
bc.onmessage = (evt) => {
197197
assert.strictEqual(evt.data, 'from-main');
198-
assert.strictEqual(evt.source, 0);
198+
assert.strictEqual(evt.sender, 0);
199199
200200
bc.close();
201201
};
@@ -206,7 +206,7 @@ assert.throws(() => new BroadcastChannel(), {
206206
bc.onmessage = common.mustCall((evt) => {
207207
assert.strictEqual(evt.data, 'from-worker');
208208

209-
assert.ok(evt.source > 0);
209+
assert.ok(evt.sender > 0);
210210

211211
bc.postMessage('from-main');
212212
bc.close();

0 commit comments

Comments
 (0)