@beyond-js/ipc
v1.0.10
Published
The IPC module enables structured communication between processes in a Node.js application, including:
Downloads
40
Readme
IPC Module
The IPC module enables structured communication between processes in a Node.js application, including:
- Main process ↔ Child process communication (both directions).
- Child ↔ Child communication, routed through the main process.
The main process is referred to as "main", and it acts as the central coordinator for message routing. Child processes communicate with each other and with the main process through a unified API, allowing:
- Remote action execution (similar to RPC).
- Event-based communication (publish-subscribe).
The system automatically handles message routing, ensuring that:
- Child processes can send actions or events to other children (via the main process).
- The main process can directly interact with any child.
- Communication is asynchronous, with promise-based action resolution.
This module simplifies inter-process messaging and abstracts away the complexity of managing forks, message serialization, and routing logic.
Installation
npm install @beyond-js/ipc⸻
Usage
The unified module automatically detects if it's running in the main or a child process, so a single import is all that's needed for most contexts.
import { ipc } from '@beyond-js/ipc/wrapper';Alternatively, you can manually import the main or child handler if you prefer to manage them explicitly.
import { ipc } from '@beyond-js/ipc/main';
import { ipc } from '@beyond-js/ipc/child';⸻
API
The IPC interface exposes a unified API for both main and child processes.
The same methods are available in all contexts; behavior is handled internally based on the process type.
Register a Child Process (main only)
const child = fork('./child.js');
ipc.register('child-a', child);⸻
Handle Actions
ipc.handle('get-time', () => new Date().toISOString());Execute Actions
const time = await ipc.exec('child-a', 'get-time');Emit and Subscribe to Events
ipc.on('child-a', 'status', data => {
console.log('Status from child-a:', data);
});
ipc.emit('status', { ready: true });Unregister a Child
ipc.unregister('child-a');Remove Action Handlers
ipc.unhandle('get-time');⸻
License
MIT
