ipc-dialog
v1.3.1
Published
Lightweight IPC library for Node.js with bidirectional request-response pattern and optional timeout management
Maintainers
Readme
IPC Request System
Lightweight IPC library for Node.js with bidirectional request-response pattern and optional timeout management.
Features
- Bidirectional request-response (parent ↔ worker)
- Optional adaptive timeout with ping extension
- Fire-and-forget messaging
- TypeScript support
Installation
npm install ipc-dialogQuick Start
Parent:
import { fork } from 'child_process';
import { IPCWrapper } from 'ipc-dialog';
const worker = new IPCWrapper(fork('./worker.js'));
const result = await worker.request('task', { value: 42 });
console.log(result); // { result: 84 }Worker:
import { IPCResponder } from 'ipc-dialog';
const responder = new IPCResponder(process);
responder.onRequest('task', async (data) => {
return { result: data.value * 2 };
});Worker → Parent:
import { IPCWrapper, IPCResponder } from 'ipc-dialog';
// In worker: send request to parent
const parent = new IPCWrapper(process);
const data = await parent.request('getData', { id: 123 });
// In parent: handle requests from worker
const parentResponder = new IPCResponder(worker);
parentResponder.onRequest('getData', (data) => {
return { data: [1, 2, 3] };
});API
IPCWrapper
new IPCWrapper(process: IPCProcess, options?: {
id?: string; // default: 'NOID'
adaptiveTimeoutMs?: number | null; // null/Infinity = infinite
hardTimeoutMs?: number | null; // null/Infinity = infinite
})
request<T>(type, data?, options?): Promise<T>
send(message)
message(type, data?)
process: T // Full access to ChildProcess/Worker methods
id: string // Always set (default: 'NOID')IPCResponder
new IPCResponder(process: IPCProcess, options?: {
pingIntervalMs?: number | null; // null/0 = disable
})
onRequest(type, handler: (data: any) => any)
onMessage(type, handler: (data: any) => any)
offRequest(type)
offMessage(type)
process: T // The process being listened to
// Tip: Type your data inline: handler: (data: YourInterface) => ...Types
import type { IPCMessage, IPCProcess, IPCWrapperOptions, IPCResponderOptions } from 'ipc-dialog';License
ISC © tish
