Skip to content

Commit

Permalink
docs: An example that implements broadcast communication (#656)
Browse files Browse the repository at this point in the history
Co-authored-by: Carlos Fuentes <me@metcoder.dev>
(cherry picked from commit d57084a)
  • Loading branch information
testwhygh authored and github-actions[bot] committed Sep 25, 2024
1 parent d051f13 commit d0a6754
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,56 @@ module.exports = ({ a, b }) => {
};
```
### Broadcast a message to all worker threads
Piscina supports broadcast communication via BroadcastChannel(Node v18+). Here is an example, the main thread sends a message, and other threads the receive message.
In `main.js`
```js
'use strict';

const { BroadcastChannel } = require('worker_threads');
const { resolve } = require('path');

const Piscina = require('piscina');
const piscina = new Piscina({
filename: resolve(__dirname, 'worker.js'),
useAtomics: false
});

async function main () {
const bc = new BroadcastChannel('my_channel');
// start worker
Promise.all([
piscina.run('thread 1'),
piscina.run('thread 2')
]);
// post message in one second
setTimeout(() => {
bc.postMessage('Main thread message');
}, 1000);
}

main();

```
In `worker.js`
```js
'use strict';
const { BroadcastChannel } = require('worker_threads');

module.exports = async (thread) => {
const bc = new BroadcastChannel('my_channel');
bc.onmessage = (event) => {
console.log(thread + ' Received from:' + event.data);
};
await new Promise((resolve) => {
setTimeout(resolve, 2000);
});
};

```
### Additional Examples
Additional examples can be found in the GitHub repo at
Expand Down
61 changes: 61 additions & 0 deletions docs/docs/examples/broadcast.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
id: Simple
sidebar_position: 23
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import { WorkerWrapperComponent } from '@site/src/components/WorkerWrapper.mdx'

In this example, we create a Piscina instance that uses BroadcastChannel(Node v18+) to implement broadcast communication. The main thread sends a message, and other threads the receive message.

<Tabs>
<TabItem value="Javascript">

```javascript title="index.js"
'use strict';

const { BroadcastChannel } = require('worker_threads');
const { resolve } = require('path');

const Piscina = require('piscina');
const piscina = new Piscina({
filename: resolve(__dirname, 'worker.js'),
useAtomics: false
});

async function main () {
const bc = new BroadcastChannel('my_channel');
// start worker
Promise.all([
piscina.run('thread 1'),
piscina.run('thread 2')
]);
// post message in one second
setTimeout(() => {
bc.postMessage('Main thread message');
}, 1000);
}

main();

```

```javascript title="worker.js"
'use strict';
const { BroadcastChannel } = require('worker_threads');

module.exports = async (thread) => {
const bc = new BroadcastChannel('my_channel');
bc.onmessage = (event) => {
console.log(thread + ' Received from:' + event.data);
};
await new Promise((resolve) => {
setTimeout(resolve, 2000);
});
};

```
</TabItem>
</Tabs>

You can also check out this example on [github](https://github.com/piscinajs/piscina/tree/current/examples/broadcast).
26 changes: 26 additions & 0 deletions examples/broadcast/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

const { BroadcastChannel } = require('worker_threads');
const { resolve } = require('path');

const Piscina = require('piscina');
const piscina = new Piscina({
filename: resolve(__dirname, 'worker.js'),
// Set useAtomics to false to avoid threads being blocked when idle
useAtomics: false
});

async function main () {
const bc = new BroadcastChannel('my_channel');
// start worker
Promise.all([
piscina.run('thread 1'),
piscina.run('thread 2')
]);
// post message in one second
setTimeout(() => {
bc.postMessage('Main thread message');
}, 1000);
}

main();
12 changes: 12 additions & 0 deletions examples/broadcast/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
const { BroadcastChannel } = require('worker_threads');

module.exports = async (thread) => {
const bc = new BroadcastChannel('my_channel');
bc.onmessage = (event) => {
console.log(thread + ' Received from:' + event.data);
};
await new Promise((resolve) => {
setTimeout(resolve, 2000);
});
};

0 comments on commit d0a6754

Please sign in to comment.