Examples

Explore examples demonstrating key features of isolated-workers.

Basic Ping-Pong Worker

beginner

A simple example demonstrating the basics of isolated-workers: spawning a worker process, sending messages, and receiving responses. This example shows the fundamental request/response pattern with type-safe message definitions.

Custom Serializer

advanced

Shows how to implement and use a custom serializer for message encoding. This is useful for using binary formats like MessagePack, adding compression, or wrapping messages with metadata. Both host and worker must use the same serializer.

Error Handling

intermediate

Demonstrates how errors thrown in worker handlers propagate back to the host, including error messages and proper cleanup.

Middleware Pipeline

intermediate

Demonstrates how to use middleware to intercept and process messages. Middleware can log messages, add metadata, validate structure, or transform payloads. Both the host and worker can have their own middleware pipelines.

Unexpected Worker Shutdown Handling

intermediate

Demonstrates how to handle unexpected worker crashes with configurable retry strategies, per-message-type overrides, and detailed error context.

Timeout Configuration

advanced

Demonstrates the flexible timeout configuration system. You can set timeouts for worker startup, server connection, a default message timeout, and per-message-type timeouts for fine-grained control over long-running operations.

Worker Lifecycle Management

intermediate

Demonstrates worker lifecycle management including status checking (isActive, isConnected), state persistence across requests, and graceful shutdown. Shows how workers maintain state between messages and how to handle the worker after it's closed.

Worker Threads Driver

intermediate

Use the worker_threads driver for in-process workers with shared memory support. Worker threads run in the same process as the host, enabling SharedArrayBuffer and lower overhead compared to child processes.