A modern zero-dependency Worker communication and orchestration library
A zero-dependency cross-worker communication library. It works with any kind of W3 based WebWorker. It also allows setting up and orchestrating the communication between any amount of workers the orchestration is generally handled by the UI thread but any worker can orchestrate. You decide what communication takes place between your seperate workers using typescript types.
This library provides an easy to use, modern, and type-safe way of cross-worker communication.
The major primitives are a read, write, and read/write channel.
First install the package.
npm i worker-channel
Want more examples with runnable code see the examples directory.
First we start with a type file this will be used from both the worker and the client:
message-types.ts
export type MessageType = {
// This will determine the name of our channel.
type: "string",
// This will determine its data.
data: string
}
main.ts | worker.ts |
---|---|
|
|
A slightly more involved example is to read to and write from a worker over a channel. On the UI side of the application this involves setting up a webworker on the client and setting up our channel as readable and writable.
Lets use the same types as last time:
message-types.ts
export type MessageType = {
// This will determine the name of our channel.
type: "string",
// This will determine its data.
data: string
}
main.ts | worker.ts |
---|---|
|
|
An even more sophisticated example is reading to and writing from a worker using the web's built in MessageChannel. On the UI side of the application this involves using MessageChannels to orchestrate. How your workers can talk to one another.
Lets use the same types as last time:
message-types.ts
export type MessageType = {
// This will determine the name of our channel.
type: "string",
// This will determine its data.
data: string
}
main.ts | worker1.ts | worker2.ts |
---|---|---|
|
|
|
Generated using TypeDoc