This module can be used to provide handy functionality on top of remote connections such as Inter-Process Communication (IPC). It's designed to be small and easy to use.
It has the following features:
- Send and receive events with arbitrary arguments
- RPC-style remote command calling
- Can be used on both sides of a communication channel
wormhole is developed with
child_process (over IPC) in mind.
TypeScript definitions included!
npm i @art-of-coding/wormhole
This is an attempt to write a complete example. In this example we fork a process and then we want to communicate with it. By using wormhole on both ends, we can send and receive events and commands in both directions.
The master process:
const childProcess =const Wormhole =const child = childProcessconst wormhole = child// Register a `startup` event handlerwormholeevents// Register an `add` commandwormhole// Send the `quit` event to the child
The child process:
const Wormhole =// Without the `channel` argument, `process` is selected by defaultconst wormhole =// Register a `quit` event handlerwormholeevents// Send an eventwormholeevent'startup'// Call a remote commandwormhole
This API is applicable to version 1.0.x! For the 0.x.x documentation, see here.
new Wormhole (channel = process)
Instantiates a new wormhole instance.
channel: The channel to use (must either be
processor an instance of
A Wormhole instance is an
EventEmitter with the following events:
error: There was an error
message: A received message that isn't handled by Wormhole
disconnect: The channel is disconnected
true if the channel is connected.
Returns the amount of pending command callbacks.
Getter to get the
wormhole.define (name, fn, context)
Define a command.
If the command is not a promise, the result will be cast into one. Throwing
Error (or subclassed) results in the rejection of the promise.
name: The name for this command
fn: The function for this command
context: The command context (optional)
wormhole.event (event, ...args)
Emit an event named
event: The name of the event
...args: The event arguments
wormhole.command (name, ...args)
Call a remote command named
name: The name of the command to call
...args: The command arguments
wormhole.write (message, handle)
Writes a message over the channel.
message: The message primitive or object
handle: An instance of
net.Socketto transfer (optional)
Disconnects the channel and releases all resources. After calling this, the instance is no longer usable.
Copyright 2017 Art of Coding.
This software is licensed under the MIT License.