<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>
<td>Gives Linux and Mac lightning fast communication and avoids the network card to reduce overhead and latency. <ahref="https://github.com/RIAEvangelist/node-ipc/tree/master/example/unixSocket/"title="Unix Socket Node IPC examples">Local Unix 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 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 Implementation because UDP sockets go through the network card while Unix 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>
</table><p><strong>Windows</strong> users may want to use UDP servers for the fastest local IPC. Unix Servers are the fastest oprion on Linux and Mac, but not available for windows. </p>
<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>
</tr>
<tr>
<td>encoding</td>
<td>the default encoding for data sent on sockets</td>
</tr>
<tr>
<td>silent</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 mwaning clients will continue to retryt to connect to servers indefinately 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 concation 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>
<pre><code>{
good : 'green',
notice : 'yellow',
warn : 'red',
error : 'redBG',
debug : 'magenta',
variable: 'cyan',
data : 'blue'
}
</code></pre>
<p>You can override any of these settings by requireing colors and setting the theme as follows :</p>
<p>Used for connecting as a client to local Unix 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 string id of the socket being connected to. The socket with this id is added to the ipc.of object when created.</td>
</tr>
<tr>
<td>path</td>
<td>optional</td>
<td>is the path of the Unix Domain Socket File, if not set this will be defaylted to <code>ipc.config.socketRoot</code>+<code>ipc.config.appspace</code>+<code>id</code>
</td>
</tr>
<tr>
<td>callback</td>
<td>optional</td>
<td>this is the function to execute when the socket has been created.</td>
</tr>
</tbody>
</table><p><strong>examples</strong> arguments can be ommitted solong as they are still in order.</p>
<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 Socket Implementaion of <code>connectTo</code> instead as it is much faster since it avoids the network card alltogether.</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 refrence to the socket.</td>
</tr>
<tr>
<td>host</td>
<td>optional</td>
<td>is the host on which the TCP or TLS socket resides. This will default to <code>ipc.config.networkHost</code> if not specified.</td>
</tr>
<tr>
<td>port</td>
<td>optional</td>
<td>the port on which the TCP or TLS socket resides.</td>
</tr>
<tr>
<td>callback</td>
<td>optional</td>
<td>this is the function to execute when the socket has been created.</td>
</tr>
</tbody>
</table><p><strong>examples</strong> arguments can be ommitted solong as they are still in order.<br>
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, 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>
<table>
<thead><tr>
<th>variable</th>
<th>required</th>
<th>definition</th>
</tr></thead>
<tbody><tr>
<td>id</td>
<td>required</td>
<td>is the string id of the socket from which to disconnect.</td>
<p>Used to create local Unix 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 Unix Domain Socket path to bind to. If not supplied, it will default to : ipc.config.socketRoot + ipc.config.appspace + ipc.config.id;</td>
</tr>
<tr>
<td>callback</td>
<td>optional</td>
<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>
</td>
</tr>
</tbody>
</table><p><strong><em>examples</em></strong> arguments can be ommitted solong as they are still in order.</p>
<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>
<table>
<thead><tr>
<th>variable</th>
<th>required</th>
<th>definition</th>
</tr></thead>
<tbody>
<tr>
<td>host</td>
<td>optional</td>
<td>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 <ahref="https://github.com/RIAEvangelist/node-http-server">node-http-server</a> installed to run subdomains for you.</td>
</tr>
<tr>
<td>port</td>
<td>optional</td>
<td>The port on which the TCP, UDP, or TLS Socket server will be bound, this defaults to 8000 if not specified</td>
</tr>
<tr>
<td>UDPType</td>
<td>optional</td>
<td>If set this will create the server as a UDP socket. 'udp4' or 'udp6' are valid values. This defaults to not being set.</td>
</tr>
<tr>
<td>callback</td>
<td>optional</td>
<td>Function to be called when the server is created</td>
</tr>
</tbody>
</table><p><strong><em>examples</em></strong> arguments can be ommitted solong as they are still in order.</p>
<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>
<aname="server-for-unix-sockets--tcp-sockets"class="anchor"href="#server-for-unix-sockets--tcp-sockets"><spanclass="octicon octicon-link"></span></a>Server for Unix 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 both local Unix Sockets and local or remote network TCP Sockets.</p>
<aname="client-for-unix-sockets--tcp-sockets"class="anchor"href="#client-for-unix-sockets--tcp-sockets"><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 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.</p>
<pre><code>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);
}
);
}
);
</code></pre>
<h4>
<aname="server--client-for-udp-sockets"class="anchor"href="#server--client-for-udp-sockets"><spanclass="octicon octicon-link"></span></a>Server & Client for UDP Sockets</h4>
<p>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. </p>
<p>This means a UDP Client and Server are the same thing because inorder 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 <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>This is the most basic example which will work for both local Unix Sockets and local or remote network TCP Sockets.</p>
<h5>
<aname="udp-server-1---world"class="anchor"href="#udp-server-1---world"><spanclass="octicon octicon-link"></span></a>UDP Server 1 - "World"</h5>
<pre><code>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();
</code></pre>
<h5>
<aname="udp-server-2---hello"class="anchor"href="#udp-server-2---hello"><spanclass="octicon octicon-link"></span></a>UDP Server 2 - "Hello"</h5>
<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);
}
);
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();
</code></pre>
</section>
</div>
<!-- FOOTER -->
<divid="footer_wrap"class="outer">
<footerclass="inner">
<pclass="copyright">Node-ipc maintained by <ahref="https://github.com/RIAEvangelist">RIAEvangelist</a></p>
<p>Published with <ahref="http://pages.github.com">GitHub Pages</a></p>