open-channel
v0.1.12
Published
A self-hosting inter-process communication channel for NodeJS.
Downloads
36
Maintainers
Readme
Open-Channel
A self-hosting inter-process communication channel for NodeJS.
Design Purpose
Even if NodeJS provides its IPC communication abilities internally and there are a lot third party packages that provides IPC functionality. However, all of them require starting an IPC server or based on cluster/child_process, which are very unsuitable and may not work with the some situations:
- The developer doesn't have authority to write any logic in the master process, e.g. your app runs under PM2 supervision.
- If the IPC server is down, all communications will be lost and must manually restart the server.
- The developer must set exact protocol and net port (probably) to communicate, which may not be available when run-time environment is changed.
Open-Channel is meant to resolve these problems. With it, you will have these advantages:
- No need of master process accessibility, all code is written for a single process (whether it will be run as child-process or individual process).
- No need to start the IPC server manually, the channel will ship it internally.
- Automatically switch the best protocol to transmit, on Unit, it uses domain socket, on Windows, it uses named pipe by default and changed to net port when in cluster mode.
- Automatically reconnect and resend messages if the connection is lost, always keep communications ongoing.
- Sending messages even before the connection established, they will be queued and sent once connection is ready.
Example
const openChannel = require("open-channel").default;
var channel = openChannel(socket => {
// This is a connection lister that passed to net.createServer().
// The server instance will not be created until the first time calling
// channel.connect().
// Put Your logic here to handle client connections and communications with
// the server.
});
// Gets a net.Socket instance connected to the server.
// you can pass an optional timeout argument in milliseconds to connect().
// The connection will not be immediately ready, but you still can send messages.
var socket = channel.connect();
// Put you logic here to communicate with the server.
API
openChannel(connectionListener: (socket: net.Socket) => void): ProcessChannel
openChannel(name: string, connectionListener: (socket: net.Socket) => void): ProcessChannel
name
The name of the channel. It's required to set a unique name of the channel when you have multiple channels on the same machine.connectionListener
is set fornet.createServer()
.
channel.connect(timeout?: number): net.Socket
Gets the socket client that will connect to the internal server.timeout
Default value is5000
ms.
channel.name: string
The same name passed toopenChannel()
.channel.state: string
Returns the status of the channel, which will either beinitiated
,connecting
,connected
orclosed
.channel.connected: boolean
Whether the channel is connected to the internal server.socket.write()
The client side write method has been re-written to implement sending messages even when the channel state is connecting.socket.destroy()
The client side destroy method has been re-written to implement closing both the channel and the socket. (The internal server will still be working, and currently unable to close the channel on server side.)