<metaname="description"content="Node-ipc : Inter Process Communication Module for node supporting Unix sockets, TCP, TLS, and UDP. Giving lightning speed on Linux, Mac, and Windows. Neural Networking in Node.JS">
<h2id="project_tagline">Inter Process Communication Module for node supporting Unix sockets, TCP, TLS, and UDP. Giving lightning speed on Linux, Mac, and Windows. Neural Networking in Node.JS</h2>
<p>npm info : <ahref="http://npm-stat.com/charts.html?package=node-ipc&author=&from=&to=">See npm trends and stats for node-ipc</a><br>
<imgsrc="https://img.shields.io/npm/v/node-ipc.svg"alt="node-ipc npm version"><imgsrc="https://img.shields.io/node/v/node-ipc.svg"alt="supported node version for node-ipc"><imgsrc="https://img.shields.io/npm/dt/node-ipc.svg"alt="total npm downloads for node-ipc"><imgsrc="https://img.shields.io/npm/dm/node-ipc.svg"alt="monthly npm downloads for node-ipc"><imgsrc="https://img.shields.io/npm/l/node-ipc.svg"alt="npm licence for node-ipc"></p>
<aid="types-of-ipc-sockets"class="anchor"href="#types-of-ipc-sockets"aria-hidden="true"><spanclass="octicon octicon-link"></span></a>Types of IPC Sockets</h4>
<td>Gives Linux, Mac, and Windows lightning fast communication and avoids the network card to reduce overhead and latency. <ahref="https://github.com/RIAEvangelist/node-ipc/tree/master/example/unixWindowsSocket/"title="Unix and Windows Socket Node IPC examples">Local Unix and Windows Socket examples </a>
<td>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 and Windows Sockets do not. <ahref="https://github.com/RIAEvangelist/node-ipc/tree/master/example/TCPSocket/"title="TCP Socket Node IPC examples">Local or remote network TCP Socket examples </a>
<td>Gives the <strong>fastest network communication</strong>. 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 or Windows Socket Implementation because UDP sockets go through the network card while Unix and Windows Sockets do not. <ahref="https://github.com/RIAEvangelist/node-ipc/tree/master/example/UDPSocket/"title="UDP Socket Node IPC examples">Local or remote network UDP Socket examples </a>
<td>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.</td>
</tr>
<tr>
<td>socketRoot</td>
<td>the directory in which to create or bind to a Unix Socket</td>
</tr>
<tr>
<td>id</td>
<td>the id of this socket or service</td>
</tr>
<tr>
<td>networkHost</td>
<td>the local or remote host on which TCP, TLS or UDP Sockets should connect</td>
</tr>
<tr>
<td>networkPort</td>
<td>the default port on which TCP, TLS, or UDP sockets should connect</td>
<td>the default encoding for data sent on sockets. Mostly used if rawBuffer is set to true. Valid values are : <code>ascii</code><code>utf8</code><code>utf16le</code><code>ucs2</code><code>base64</code><code>hex</code> .</td>
</tr>
<tr>
<td>rawBuffer</td>
<td>if true, data will be sent and received as a raw node <code>Buffer</code><strong>NOT</strong> an <code>Object</code> as JSON. This is great for Binary or hex IPC, and communicating with other processes in languages like C and C++</td>
<td>turn on/off logging default is false which means logging is on</td>
</tr>
<tr>
<td>maxConnections</td>
<td>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.</td>
</tr>
<tr>
<td>retry</td>
<td>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.</td>
<td>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 <strong><em>NOT</em></strong> try to reconnect.</td>
<p>ipc.log will accept any number of arguments and if <code>ipc.config.silent</code> 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.</p>
<p>The log also supports <ahref="https://github.com/Marak/colors.js">colors</a> implementation. All of the available styles are supported and the theme styles are as follows :</p>
<p>Used for connecting as a client to local Unix Sockets and Windows Sockets. <strong><em>This is the fastst way for processes on the same machine to communicate</em></strong> because it bypasses the network card which TCP and UDP must both use.</p>
<td>is the path of the Unix Domain Socket File, if the System is Windows, this will automatically be converted to an appropriate pipe with the same information as the Unix Domain Socket File. If not set this will default to <code>ipc.config.socketRoot</code>+<code>ipc.config.appspace</code>+<code>id</code>
<p>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 and Windows Socket Implementaion of <code>connectTo</code> instead as it is much faster since it avoids the network card altogether.</p>
<td>is the string id of the socket being connected to. For TCP & TLS sockets, this id is added to the <code>ipc.of</code> object when the socket is created with a reference to the socket.</td>
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.</p>
<pre><code>ipc.connectToNet('world');
</code></pre>
<p>or using just an id and a callback</p>
<pre><code>ipc.connectToNet(
'world',
function(){
...
}
);
</code></pre>
<p>or explicitly setting the host and path</p>
<pre><code>ipc.connectToNet(
'world',
'myapp.com',serve(path,callback)
3435
);
</code></pre>
<p>or only explicitly setting port and callback</p>
<p>Used to disconnect a client from a Unix, Windows, TCP or TLS socket. The socket and its refrence will be removed from memory and the <code>ipc.of</code> 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.</p>
<p>Used to create local Unix Socket Server or Windows Socket Server to which Clients can bind. The server can <code>emit</code> events to specific Client Sockets, or <code>broadcast</code> events to all known Client Sockets. </p>
<td>This is the path of the Unix Domain Socket File, if the System is Windows, this will automatically be converted to an appropriate pipe with the same information as the Unix Domain Socket File. If not set this will default to <code>ipc.config.socketRoot</code>+<code>ipc.config.appspace</code>+<code>id</code>
<td>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 <code>ipc.server.on('start',function(){});</code>
<p>Used to create TCP, TLS or UDP Socket Server to which Clients can bind or other servers can send data to. The server can <code>emit</code> events to specific Client Sockets, or <code>broadcast</code> events to all known Client Sockets.</p>
<td>If not specified this defaults to the first address in os.networkInterfaces(). For TCP, TLS & UDP servers this is most likely going to be 127.0.0.1 or ::1</td>
<td>This is where socket connection refrences will be stored when connecting to them as a client via the <code>ipc.connectTo</code> or <code>iupc.connectToNet</code>. They will be stored based on the ID used to create them, eg : ipc.of.mySocket</td>
</tr>
<tr>
<td>ipc.server</td>
<td>This is a refrence to the server created by <code>ipc.serve</code> or <code>ipc.serveNet</code>
<p>You can find <ahref="https://github.com/RIAEvangelist/node-ipc/tree/master/example">Advanced Examples</a> in the examples folder. In the examples you will find more complex demos including multi client examples.</p>
<aid="server-for-unix-sockets-windows-sockets--tcp-sockets"class="anchor"href="#server-for-unix-sockets-windows-sockets--tcp-sockets"aria-hidden="true"><spanclass="octicon octicon-link"></span></a>Server for Unix Sockets, Windows Sockets & TCP Sockets</h4>
<p>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 local Unix and Windows Sockets as well as local or remote network TCP Sockets.</p>
<aid="client-for-unix-sockets--tcp-sockets"class="anchor"href="#client-for-unix-sockets--tcp-sockets"aria-hidden="true"><spanclass="octicon octicon-link"></span></a>Client for Unix Sockets & TCP Sockets</h4>
<p>The client connects to the servers socket for Inter Process Communication. The socket will receive 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.</p>
<aid="server--client-for-udp-sockets"class="anchor"href="#server--client-for-udp-sockets"aria-hidden="true"><spanclass="octicon octicon-link"></span></a>Server & Client for UDP Sockets</h4>
<p>UDP Sockets are different than Unix, Windows & TCP Sockets because they must be bound to a unique port on their machine to receive messages. For example, A TCP, Unix, or Windows Socket client could just connect to a separate TCP, Unix, or Windows Socket sever. That client could then exchange, both send and receive, data on the servers port or location. UDP Sockets can not do this. They must bind to a port to receive or send data. </p>
<p>This means a UDP Client and Server are the same thing because in order to receive data, a UDP Socket must have its own port to receive data on, and only one process can use this port at a time. It also means that in order to <code>emit</code> or <code>broadcast</code> data the UDP server will need to know the host and port of the Socket it intends to broadcast the data to.</p>
<p><em>note</em> 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.</p>
<pre><code>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);
<aid="raw-buffer-or-binary-sockets"class="anchor"href="#raw-buffer-or-binary-sockets"aria-hidden="true"><spanclass="octicon octicon-link"></span></a>Raw Buffer or Binary Sockets</h4>
<p>Binary or Buffer sockets can be used with any of the above socket types, however the way data events are emit is <strong><em>slightly</em></strong> different.</p>
<p>When setting up a rawBuffer socket you must specify it as such :</p>
<pre><code>ipc.config.rawBuffer=true;
</code></pre>
<p>You can also specify its encoding type. The default is <code>utf8</code></p>