Buzzify is a powerful utility that makes it effortless to manage type-safe, bidirectional RPC communication between the main thread and Web Workers β while providing a concurrent worker pool out-of-the-box.
π§ Inspired by nature. Designed for speed, reliability, and elegance.
- β Typed RPC: Define request and response types for each method across threads.
- π Bidirectional communication: Workers can also call imperative main-thread methods.
- π‘ Event system: Emit and listen to typed custom events.
- π§΅ Worker pool management: Handle concurrency with dynamic pool resizing.
- π§ Fire-and-forget support: For commands that donβt require a response.
- π§ͺ Fault tolerance: Ping-check, automatic recovery, graceful fallback.
- π§Ό Disposable workers:
Symbol.disposeandusingsupport.
npm install buzzify
# or your favorite package manager// on your worker.ts
import { defineBee, InferBeeApi } from 'buzzify'
declare const self: Worker
const bee = defineBee<{
workerHandlers: {
add: (a: number, b: number) => number
hi: () => Promise<void>
}
events: { log: [string] }
mainHandlers: { hello: (name: string) => Promise<string> }
}>(self)
bee.handle('add', (a, b) => a + b)
bee.handle('hi', async () => {
bee.call('hello', 'John')
})
bee.emit('log', 'Worker started')
export type MyBeeApi = InferBeeApi<typeof bee>
// on your main.ts
import { useBee } from 'buzzify'
import type { MyBeeApi } from './worker.ts'
const bee = useBee<MyBeeApi>(new URL('/worker.js', import.meta.url))
const result = await bee.call('add', 1, 2)
console.log(result)
// events is accessor for EventListener instance which composed on bee instance
bee.events.on('log', (message) => {
console.log(message)
// will log Worker started
})//on your main.ts
import { useBee } from 'buzzify'
import type { MyBeeApi } from './worker.ts'
const bee = useBee<MyBeeApi>(new URL('/worker.js', import.meta.url), {
hello: async (name) => {
return `Hello, ${name}!`
}
})
bee.send('hi') //after a while you will see Hello, John! in console who defined in main.tsBuzzify also provides a robust pool implementation for running tasks concurrently.
import { createBeeHive } from 'buzzify'
import type { MyBeeApi } from './worker.ts'
const pool = createBeeHive<MyBeeApi>(new URL('./worker.ts', import.meta.url), {
concurrency: 4,
handlers: {
hello: (name) => `Hello, ${name}!`,
},
})
//you can acquire a worker from the pool
const worker = await pool.acquire()
// this will return a worker instance or wait for a worker to be available
//you can call methods on the worker
await worker.call('add', 1, 2)
//when you are done, you can release the worker back to the pool
pool.release(worker)if (true) {
using worker = await pool.acquire()
worker.call('add', 1, 2)
}
// here worker will be released back to the pool because of the `using` keywordawait pool.using(worker => {
worker.call('add', 1, 2)
})
// here worker will be released back to the pool because of the `using` method of the poolbeeHive instance has events accessor for EventListener instance which composed on beeHive instance. And its events are predefined. You can not define new events. They are:
export type HiveEventsMap<M extends BeeApi> = {
workerAcquired: (reason?: any) => void
workerReleased: (item: PoolItem<M>, initialReason?: any) => void
workerReserved: (item: PoolItem<M>) => void
workerRemoved: (item: PoolItem<M>) => void
workerCreated: (item: PoolItem<M>) => void
}But the bee itself, has events accessor for EventListener instance which composed on bee instance. And its events are not predefined. You should define them on the BeeApi type.
export type MyBeeApi = {
workerHandlers: {
add: (a: number, b: number) => number
hi: () => Promise<void>
}
events: {
log: [string]
}
mainHandlers: {
hello: (name: string) => Promise<string>
}
}
//on your worker.ts
const bee = defineBee<MyBeeApi>(self)
bee.emit('log', 'Worker started')
//on your main.ts
const bee = useBee<MyBeeApi>(new URL('/worker.js', import.meta.url))
bee.events.on('log', (message) => {
console.log(message)
})Every worker has ping method who can calleble from main thread.
const bee = useBee<MyBeeApi>(new URL('/worker.js', import.meta.url))
const result = await bee.ping()
console.log(result) //this will log pongconsole.log(pool.state)
/*
{
poolSize: 4,
desiredPoolSize: 4,
busyWorkers: [{ workerId: 1, reason: 'math' }],
idleWorkers: [2, 3, 4],
waitingList: []
}
*/InferBeeApi<T>: Extract the BeeApi from a defineBee instance.
Because like bees:
- π Your workers are fast, efficient, and never idle.
- π§ The hive (pool) manages them with intelligence.
- π Communication is clean and organized.