IPC-Link is a mini-framework for node-ipc that is fully compatible with TypeScript (and in a near future, ECMAScript Modules). It is designed to have "connection channels" where two processes send data back and forth.
This framework has a queue system that holds Promise
s temporarily until the message has been replied back, and depending on the variable success
, it may or may not reject said Promise.
You can check examples here.
Process One:
const { Server } = require('../../src/index');
console.log('Spawned test-one!');
new Server('test-one', { retry: 1500, silent: true })
.on('message', console.log)
.on('error', console.error)
.once('start', (reason) => { console.log('Successfully started!', reason); })
.start('Login!');
.send('test-two', { content: 'Hello' })
.then(console.log)
.catch(console.error);
Process Two:
const { Server } = require('../../src/index');
console.log('Spawned test-two!');
new Server('test-two', { retry: 1500, silent: true })
.on('message', message => { message.reply({ response: `${message.data.content} world!` }); })
.on('error', console.error)
.once('start', (reason) => { console.log('Successfully started!', reason); })
.start('Login!');
Process One will send Process Two an object with content Hello
, Process Two receives back and replies it with Hello
(content sent by the first process) and sends another object with response: 'Hello world!'
to Process One. Evaluating .send('test-two', { content: 'Hello' })
to Promise<{ id, success: true, response: 'Hello World' }>
, which is logged to console.
It is important that you have a single IPCLink.Server
instance because node-ipc
is basically a singleton, and creating multiple instances of this may duplicate messages or corrupt the configuration. In a near future, node-ipc
may get rewritten in a fork or in a backends/
folder in this repository for further support with latest versions of Node.js (new Buffer()
is being used, which is deprecated starting from Node.js 10).
Process One
- Let
server
be the result of evaluatingnew Server(name, options);
. - Consider there is a
message
event being listened inserver
. - Consider
server.start();
has already been called. - Let
socketName
be a string, e.g.'world'
. - Let
data
be an object literal, e.g.{ test: 'Hello ' }
. - Perform
server.send(socketName, data);
. - Let
senderSocket
be the Socket instance from this process. - Let
hasSocket
be the result of evaluatingserver.hasSocket(name);
. - If
hasSocket
istrue
, skip to the next point. Otherwise,- connect to the socket via
server.connectTo
and await its evaluation.
- connect to the socket via
- Let
socket
be the result of evaluatingserver.getSocket(name);
.- Let
IPCSocketCollection
be an object ofNodeIPC.Server
s. - Let
IPCSocketServer
be the result of accessing to the propertyname
ofIPCSocketCollection
. - If
IPCSocketServer
isundefined
, letsocket
benull
. Otherwise- Let
socket
beIPCSocketServer.socket
, being this a Socket instance.
- Let
- Let
- If
data
has a property ofid
, letid
bedata.id
. Otherwise letid
be a random base36 number generated automatically. - Let
preparedData
be an object where:id
refers toid
.sentBy
refers toserver.name
.data
refers todata
.
- Let
stringifiedData
be the result of evaluatingJSON.stringify(preparedData);
. - Perform
socket.write
, sendingstringifiedData
to the Socket. - Let
temporalPromise
be a Promise evaluated withnew Promise();
. - Let
resolve
andreject
be the first and second parameters fromtemporalPromise
's callbacks. - Let
queuePromise
be an object where:resolve
refers toresolve
.reject
refers toreject
.
- Let
promiseCollection
be the internal Promise collection from IPC-Link of typeMap<string, { resolve: Function, reject: Function }>;
. - Perform
promiseCollection.set(id, queuePromise);
. - Return
queuePromise
.
Process Two
- Let
receiverServer
be the result of evaluatingnew Server(name, options);
in the target process. - Let
messagePayload
be the result of evaluatingJSON.parse(stringifiedData);
. - Let
message
be the result of evaluatingnew Message(receiverServer, senderSocket, messagePayload);
. - Send
message
toreceiverServer
's EventEmitter for its handling. - Let
responseData
be an object. - If
responseData
has a property ofsuccess
, letsuccess
beresponseData.success
. Otherwise- Let
successArgument
be the result of evaluating the third argument fromServer#send
. - If
successArgument
isundefined
, letsuccess
betrue
. Otherwise letsuccess
besuccessArgument
.
- Let
- Let
finalizedResponseData
be an object where:id
refers toid
.success
refers tosuccess
.- All properties of
responseData
are applied over the properties ofid
andsuccess
.
- Let
stringifiedResponseData
be the result of evaluatingJSON.stringify(finalizedResponseData);
. - Perform
senderSocket.write
, sendingstringifiedResponseData
to the Socket.
Process One
- Let
parsedResponseData
be the result of evaluatingJSON.parse(stringifiedResponseData);
. - Let
responseID
be the result of evaluatingparsedResponseData.id
. - If
responseID
does not equals toid
via Strict Equality Comparison,- Ignore the request. Otherwise,
- Let
promise
bequeuePromise
.- Let
successResponse
be the result of evaluatingparsedResponseData.success
. - If
successResponse
istrue
, evaluatepromise.resolve(parsedResponseData);
, resolvingtemporalPromise
with the value ofparsedResponseData
. Otherwise evaluatepromise.reject(parsedResponseData);
, rejectingtemporalPromise
with the value ofparsedResponseData
.
- Let