Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
A nodejs module for local and remote Inter Process Communication (IPC), Neural Networking, and able to facilitate machine learning.
The node-ipc package is a Node.js module that provides a simple and flexible way to handle inter-process communication (IPC) in Node.js applications. It supports various types of communication, including local sockets, TCP, and UDP, making it suitable for a wide range of use cases such as microservices, distributed systems, and real-time applications.
Local Socket Communication
This feature allows for communication between processes on the same machine using local sockets. The code sample demonstrates setting up a server that listens for messages and a client that sends and receives messages.
const ipc = require('node-ipc');
ipc.config.id = 'world';
ipc.config.retry = 1500;
ipc.config.silent = true;
ipc.serve(() => {
ipc.server.on('message', (data, socket) => {
ipc.log('Received message: ', data);
ipc.server.emit(socket, 'message', 'Hello from server!');
});
});
ipc.server.start();
// Client
ipc.connectTo('world', () => {
ipc.of.world.on('connect', () => {
ipc.log('Connected to world');
ipc.of.world.emit('message', 'Hello from client!');
});
ipc.of.world.on('message', (data) => {
ipc.log('Received message: ', data);
});
});
TCP Communication
This feature enables communication over TCP, allowing processes to communicate over a network. The code sample shows how to set up a TCP server and a client that can send and receive messages.
const ipc = require('node-ipc');
ipc.config.id = 'tcpServer';
ipc.config.retry = 1500;
ipc.config.networkPort = 8000;
ipc.config.silent = true;
ipc.serveNet(() => {
ipc.server.on('message', (data, socket) => {
ipc.log('Received message: ', data);
ipc.server.emit(socket, 'message', 'Hello from TCP server!');
});
});
ipc.server.start();
// Client
ipc.connectToNet('tcpServer', 8000, () => {
ipc.of.tcpServer.on('connect', () => {
ipc.log('Connected to TCP server');
ipc.of.tcpServer.emit('message', 'Hello from TCP client!');
});
ipc.of.tcpServer.on('message', (data) => {
ipc.log('Received message: ', data);
});
});
UDP Communication
This feature supports communication over UDP, which is useful for applications that require low-latency communication. The code sample demonstrates setting up a UDP server and a client that can send and receive messages.
const ipc = require('node-ipc');
ipc.config.id = 'udpServer';
ipc.config.retry = 1500;
ipc.config.networkPort = 8001;
ipc.config.silent = true;
ipc.serveUDP(() => {
ipc.server.on('message', (data, socket) => {
ipc.log('Received message: ', data);
ipc.server.emit(socket, 'message', 'Hello from UDP server!');
});
});
ipc.server.start();
// Client
ipc.connectToUDP('udpServer', 8001, () => {
ipc.of.udpServer.on('connect', () => {
ipc.log('Connected to UDP server');
ipc.of.udpServer.emit('message', 'Hello from UDP client!');
});
ipc.of.udpServer.on('message', (data) => {
ipc.log('Received message: ', data);
});
});
Socket.IO is a popular library for real-time web applications. It enables real-time, bidirectional communication between web clients and servers. Unlike node-ipc, which focuses on inter-process communication, Socket.IO is designed for web applications and provides features like automatic reconnection, multiplexing, and broadcasting.
Dnode is a library for asynchronous, bidirectional RPC (Remote Procedure Call) in Node.js. It allows you to call functions in another process or even on another machine as if they were local. Dnode is more focused on RPC, whereas node-ipc provides a broader range of IPC mechanisms.
ZeroMQ (also known as ØMQ) is a high-performance asynchronous messaging library. It provides a message queue but unlike traditional message brokers, it can be used for various messaging patterns like pub-sub, request-reply, and push-pull. ZeroMQ is more complex and powerful compared to node-ipc, which is simpler and easier to use for basic IPC needs.
a nodejs module for local and remote Inter Process Communication for Linux, Mac and Windows.
A great solution for Neural Networking in Node.JS
npm install node-ipc
Package details websites :
This work is licenced via the DBAD Public Licence.
Type | Stability | Definition |
---|---|---|
Unix Socket | Stable | Gives Linux and Mac lightning fast communication and avoids the network card to reduce overhead and latency. Local Unix Socket examples |
TCP Socket | Stable | Gives the most reliable communication across the network. Can be used for local IPC as well, but is slower than #1's Unix Socket Implementation because TCP sockets go through the network card while Unix Sockets do not. Local or remote network TCP Socket examples |
TLS Socket | Alpha | coming soon... |
UDP Sockets | Stable | Gives the fastest network communication. UDP is less reliable but much faster than TCP. It is best used for streaming non critical data like sound, video, or multiplayer game data as it can drop packets depending on network connectivity and other factors. UDP can be used for local IPC as well, but is slower than #1's Unix Socket Implementation because UDP sockets go through the network card while Unix Sockets do not. Local or remote network UDP Socket examples |
OS | Supported Sockets |
---|---|
Linux | Unix, TCP, TLS, UDP |
Mac | Unix, TCP, TLS, UDP |
Win | TCP, TLS, UDP |
Windows users may want to use UDP servers for the fastest local IPC. Unix Servers are the fastest option on Linux and Mac, but not available for Windows.
ipc.config
Set these variables in the ipc.config
scope to overwrite or set default values.
{
appspace : 'app.',
socketRoot : '/tmp/',
id : os.hostname(),
networkHost : 'localhost',
networkPort : 8000,
encoding : 'utf8',
silent : false,
maxConnections : 100,
retry : 500,
maxRetries : false,
stopRetrying : false
}
variable | documentation |
---|---|
appspace | used for Unix Socket (Unix Domain Socket) namespacing. If not set specifically, the Unix Domain Socket will combine the socketRoot, appspace, and id to form the Unix Socket Path for creation or binding. This is available incase you have many apps running on your system, you may have several sockets with the same id, but if you change the appspace, you will still have app specic unique sockets. |
socketRoot | the directory in which to create or bind to a Unix Socket |
id | the id of this socket or service |
networkHost | the local or remote host on which TCP, TLS or UDP Sockets should connect |
networkPort | the default port on which TCP, TLS, or UDP sockets should connect |
encoding | the default encoding for data sent on sockets |
silent | turn on/off logging default is false which means logging is on |
maxConnections | this is the max number of connections allowed to a socket. It is currently only being set on Unix Sockets. Other Socket types are using the system defaults. |
retry | this is the time in milliseconds a client will wait before trying to reconnect to a server if the connection is lost. This does not effect UDP sockets since they do not have a client server relationship like Unix Sockets and TCP Sockets. |
maxRetries | if set, it represents the maximum number of retries after each disconnect before giving up and completely killing a specific connection |
stopRetrying | Defaults to false meaning clients will continue to retry to connect to servers indefinitely at the retry interval. If set to any number the client will stop retrying when that number is exceeded after each disconnect. If set to 0, the client will NOT try to reconnect. |
These methods are available in the IPC Scope.
ipc.log(a,b,c,d,e...);
ipc.log will accept any number of arguments and if ipc.config.silent
is not set, it will concat them all with a sincle space ' ' between them and then log them to the console. This is fast because it prevents any concatenation from happening if the ipc is set to silent. That way if you leave your logging in place it should not effect performance.
The log also supports colors implementation. All of the available styles are supported and the theme styles are as follows :
{
good : 'green',
notice : 'yellow',
warn : 'red',
error : 'redBG',
debug : 'magenta',
variable: 'cyan',
data : 'blue'
}
You can override any of these settings by requireing colors and setting the theme as follows :
var colors=require('colors');
colors.setTheme(
{
good : 'zebra',
notice : 'redBG',
...
}
);
ipc.connectTo(id,path,callback);
Used for connecting as a client to local Unix Sockets. This is the fastst way for processes on the same machine to communicate because it bypasses the network card which TCP and UDP must both use.
variable | required | definition |
---|---|---|
id | required | is the string id of the socket being connected to. The socket with this id is added to the ipc.of object when created. |
path | optional | is the path of the Unix Domain Socket File, if not set this will default to ipc.config.socketRoot +ipc.config.appspace +id |
callback | optional | this is the function to execute when the socket has been created. |
examples arguments can be ommitted so long as they are still in order.
ipc.connectTo('world');
or using just an id and a callback
ipc.connectTo(
'world',
function(){
ipc.of.world.on(
'hello',
function(data){
ipc.log(data.debug);
//if data was a string, it would have the color set to the debug style applied to it
}
)
}
);
or explicitly setting the path
ipc.connectTo(
'world',
'myapp.world'
);
or explicitly setting the path with callback
ipc.connectTo(
'world',
'myapp.world',
function(){
...
}
);
ipc.connectToNet(id,host,port,callback)
Used to connect as a client to a TCP or TLS socket via the network card. This can be local or remote, if local, it is recommended that you use the Unix Socket Implementaion of connectTo
instead as it is much faster since it avoids the network card altogether.
variable | required | definition |
---|---|---|
id | required | is the string id of the socket being connected to. For TCP & TLS sockets, this id is added to the ipc.of object when the socket is created with a reference to the socket. |
host | optional | is the host on which the TCP or TLS socket resides. This will default to ipc.config.networkHost if not specified. |
port | optional | the port on which the TCP or TLS socket resides. |
callback | optional | this is the function to execute when the socket has been created. |
examples arguments can be ommitted so long as they are still in order.
So while the default is : (id,host,port,callback), the following examples will still work because they are still in order (id,port,callback) or (id,host,callback) or (id,port) etc.
ipc.connectToNet('world');
or using just an id and a callback
ipc.connectToNet(
'world',
function(){
...
}
);
or explicitly setting the host and path
ipc.connectToNet(
'world',
'myapp.com',serve(path,callback)
3435
);
or only explicitly setting port and callback
ipc.connectToNet(
'world',
3435,
function(){
...
}
);
ipc.disconnect(id)
Used to disconnect a client from a Unix, TCP or TLS socket. The socket and its refrence will be removed from memory and the ipc.of
scope. This can be local or remote. UDP clients do not maintain connections and so there are no Clients and this method has no value to them.
variable | required | definition |
---|---|---|
id | required | is the string id of the socket from which to disconnect. |
examples
ipc.disconnect('world');
ipc.serve(path,callback);
Used to create local Unix Socket Server to which Clients can bind. The server can emit
events to specific Client Sockets, or broadcast
events to all known Client Sockets.
variable | required | definition |
---|---|---|
path | optional | This is the Unix Domain Socket path to bind to. If not supplied, it will default to : ipc.config.socketRoot + ipc.config.appspace + ipc.config.id; |
callback | optional | This is a function to be called after the Server has started. This can also be done by binding an event to the start event like ipc.server.on('start',function(){}); |
examples arguments can be ommitted so long as they are still in order.
ipc.serve();
or specifying callback
ipc.serve(
function(){...}
);
or specify path
ipc.serve(
'/tmp/myapp.myservice'
);
or specifying everything
ipc.serve(
'/tmp/myapp.myservice',
function(){...}
);
serveNet(host,port,UDPType,callback)
Used to create TCP, TLS or UDP Socket Server to which Clients can bind or other servers can send data to. The server can emit
events to specific Client Sockets, or broadcast
events to all known Client Sockets.
variable | required | definition |
---|---|---|
host | optional | If not specified this defaults to localhost. For TCP, TLS & UDP servers this is most likely going to be localhost or 0.0.0.0 unless you have something like node-http-server installed to run subdomains for you. |
port | optional | The port on which the TCP, UDP, or TLS Socket server will be bound, this defaults to 8000 if not specified |
UDPType | optional | If set this will create the server as a UDP socket. 'udp4' or 'udp6' are valid values. This defaults to not being set. |
callback | optional | Function to be called when the server is created |
examples arguments can be ommitted solong as they are still in order.
default tcp server
ipc.serveNet();
default udp server
ipc.serveNet('udp4');
or specifying TCP server with callback
ipc.serveNet(
function(){...}
);
or specifying UDP server with callback
ipc.serveNet(
'udp4',
function(){...}
);
or specify port
ipc.serveNet(
3435
);
or specifying everything TCP
ipc.serveNet(
'MyMostAwesomeApp.com',
3435,
function(){...}
);
or specifying everything UDP
ipc.serveNet(
'MyMostAwesomeApp.com',
3435,
'udp4',
function(){...}
);
variable | definition |
---|---|
ipc.of | This is where socket connection refrences will be stored when connecting to them as a client via the ipc.connectTo or iupc.connectToNet . They will be stored based on the ID used to create them, eg : ipc.of.mySocket |
ipc.server | This is a refrence to the server created by ipc.serve or ipc.serveNet |
You can find Advanced Examples in the examples folder. In the examples you will find more complex demos including multi client examples.
The server is the process keeping a socket for IPC open. Multiple sockets can connect to this server and talk to it. It can also broadcast to all clients or emit to a specific client. This is the most basic example which will work for both local Unix Sockets and local or remote network TCP Sockets.
var ipc=require('node-ipc');
ipc.config.id = 'world';
ipc.config.retry= 1500;
ipc.serve(
function(){
ipc.server.on(
'message',
function(data,socket){
ipc.log('got a message : '.debug, data);
ipc.server.emit(
socket,
'message',
data+' world!'
);
}
);
}
);
ipc.server.start();
The client connects to the servers socket for Inter Process Communication. The socket will recieve events emitted to it specifically as well as events which are broadcast out on the socket by the server. This is the most basic example which will work for both local Unix Sockets and local or remote network TCP Sockets.
var ipc=require('node-ipc');
ipc.config.id = 'hello';
ipc.config.retry= 1500;
ipc.connectTo(
'world',
function(){
ipc.of.world.on(
'connect',
function(){
ipc.log('## connected to world ##'.rainbow, ipc.config.delay);
ipc.of.world.emit(
'message',
'hello'
)
}
);
ipc.of.world.on(
'disconnect',
function(){
ipc.log('disconnected from world'.notice);
}
);
ipc.of.world.on(
'message',
function(data){
ipc.log('got a message from world : '.debug, data);
}
);
}
);
UDP Sockets are different than Unix & TCP Sockets because they must be bound to a unique port on their machine to recieve messages. For example, A TCP or Unix Socket client could just connect to a seperate TCP or Unix Socket sever. That client could then exchange, both send and recive, data on the servers port or location. UDP Sockets can not do this. They must bind to a port to recieve or send data.
This means a UDP Client and Server are the same thing because in order to recieve data, a UDP Socket must have its own port to recieve data on, and only one process can use this port at a time. It also means that inorder to emit
or broadcast
data the UDP server will need to know the host and port of the Socket it intends to broadcast the data to.
This is the most basic example which will work for both local Unix Sockets and local or remote network TCP Sockets.
var ipc=require('../../../node-ipc');
ipc.config.id = 'world';
ipc.config.retry= 1500;
ipc.serveNet(
'udp4',
function(){
console.log(123);
ipc.server.on(
'message',
function(data,socket){
ipc.log('got a message from '.debug, data.from.variable ,' : '.debug, data.message.variable);
ipc.server.emit(
socket,
'message',
{
from : ipc.config.id,
message : data.message+' world!'
}
);
}
);
console.log(ipc.server);
}
);
ipc.server.define.listen.message='This event type listens for message strings as value of data key.';
ipc.server.start();
note we set the port here to 8001 because the world server is already using the default ipc.config.networkPort of 8000. So we can not bind to 8000 while world is using it.
ipc.config.id = 'hello';
ipc.config.retry= 1500;
ipc.serveNet(
8001,
'udp4',
function(){
ipc.server.on(
'message',
function(data){
ipc.log('got Data');
ipc.log('got a message from '.debug, data.from.variable ,' : '.debug, data.message.variable);
}
);
ipc.server.emit(
{
address : 'localhost',
port : ipc.config.networkPort
},
'message',
{
from : ipc.config.id,
message : 'Hello'
}
);
}
);
ipc.server.define.listen.message='This event type listens for message strings as value of data key.';
ipc.server.start();
FAQs
A nodejs module for local and remote Inter Process Communication (IPC), Neural Networking, and able to facilitate machine learning.
The npm package node-ipc receives a total of 327,183 weekly downloads. As such, node-ipc popularity was classified as popular.
We found that node-ipc demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 12 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.