node-ipc/index.html

1092 lines
55 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="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">
<link rel="stylesheet" type="text/css" media="screen" href="stylesheets/stylesheet.css">
<title>Node-ipc</title>
</head>
<body>
<!-- HEADER -->
<div id="header_wrap" class="outer">
<header class="inner">
<a id="forkme_banner" href="https://github.com/RIAEvangelist/node-ipc">View on GitHub</a>
<h1 id="project_title">Node-ipc</h1>
<h2 id="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>
<section id="downloads">
<a class="zip_download_link" href="https://github.com/RIAEvangelist/node-ipc/zipball/master">Download this project as a .zip file</a>
<a class="tar_download_link" href="https://github.com/RIAEvangelist/node-ipc/tarball/master">Download this project as a tar.gz file</a>
</section>
</header>
</div>
<!-- MAIN CONTENT -->
<div id="main_content_wrap" class="outer">
<section id="main_content" class="inner">
<h1>
<a id="node-ipc" class="anchor" href="#node-ipc" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>node-ipc</h1>
<p><em>a nodejs module for local and remote Inter Process Communication</em> with full support for Linux, Mac and Windows. It also supports all forms of socket communication from low level unix and windows sockets to UDP and secure TLS and TCP sockets.</p>
<p>A great solution for complex multiprocess <strong>Neural Networking</strong> in Node.JS</p>
<p><strong>npm install node-ipc</strong></p>
<h4>
<a id="npm-stats" class="anchor" href="#npm-stats" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>NPM Stats</h4>
<p>npm info : <a href="http://npm-stat.com/charts.html?package=node-ipc&amp;author=&amp;from=&amp;to=">See npm trends and stats for node-ipc</a><br>
<a href="https://nodei.co/npm/node-ipc/"><img src="https://nodei.co/npm/node-ipc.png?downloads=true&amp;downloadRank=true&amp;stars=true" alt="NPM"></a>
<a href="http://packagequality.com/#?package=node-ipc"><img src="http://npm.packagequality.com/badge/node-ipc.png" alt="Package Quality"></a><br>
<img src="https://img.shields.io/npm/v/node-ipc.svg" alt="node-ipc npm version"> <img src="https://img.shields.io/node/v/node-ipc.svg" alt="supported node version for node-ipc"> <img src="https://img.shields.io/npm/dt/node-ipc.svg" alt="total npm downloads for node-ipc"> <img src="https://img.shields.io/npm/dm/node-ipc.svg" alt="monthly npm downloads for node-ipc"> <img src="https://img.shields.io/npm/l/node-ipc.svg" alt="npm licence for node-ipc"></p>
<p><a href="https://github.com/RIAEvangelist"><img src="https://avatars3.githubusercontent.com/u/369041?v=3&amp;s=100" alt="RIAEvangelist"></a></p>
<p>GitHub info :<br>
<img src="https://img.shields.io/github/release/RIAEvangelist/node-ipc.svg" alt="node-ipc GitHub Release"> <img src="https://img.shields.io/github/license/RIAEvangelist/node-ipc.svg" alt="GitHub license node-ipc license"> <img src="https://img.shields.io/github/issues/RIAEvangelist/node-ipc.svg" alt="open issues for node-ipc on GitHub"></p>
<p>Codacy info :<br>
<a href="https://www.codacy.com/app/RIAEvangelist/node-ipc"><img src="https://api.codacy.com/project/badge/grade/8e0294dff55f4ac1985c07b16f39d0a9" alt="Codacy Badge"></a> <a href="https://www.codacy.com/app/RIAEvangelist/node-ipc"><img src="https://api.codacy.com/project/badge/coverage/8e0294dff55f4ac1985c07b16f39d0a9" alt="Codacy Badge"></a></p>
<p>Package details websites :</p>
<ul>
<li>
<a href="http://riaevangelist.github.io/node-ipc/" title="node-ipc documentation">GitHub.io site</a>. A prettier version of this site.</li>
<li>
<a href="https://www.npmjs.org/package/node-ipc" title="node-ipc npm module">NPM Module</a>. The npm page for the node-ipc module.</li>
</ul>
<p>This work is licenced via the <a href="http://www.dbad-license.org/">DBAD Public Licence</a>.</p>
<h4>
<a id="testing" class="anchor" href="#testing" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Testing</h4>
<p><code>npm test</code> will run the jasmine tests with istanbul for node-ipc and generate a coverage report in the spec folder.</p>
<p>You may want to install jasmine and istanbul globally with <code>sudo npm install -g jasmine istanbul</code></p>
<hr>
<h4>
<a id="contents" class="anchor" href="#contents" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Contents</h4>
<ol>
<li><a href="#types-of-ipc-sockets">Types of IPC Sockets and Supporting OS</a></li>
<li><a href="#ipc-config">IPC Config</a></li>
<li>
<a href="#ipc-methods">IPC Methods</a>
<ol>
<li><a href="#log">log</a></li>
<li><a href="#connectto">connectTo</a></li>
<li><a href="#connecttonet">connectToNet</a></li>
<li><a href="#disconnect">disconnect</a></li>
<li><a href="#serve">serve</a></li>
<li><a href="#servenet">serveNet</a></li>
</ol>
</li>
<li><a href="#ipc-stores-and-default-variables">IPC Stores and Default Variables</a></li>
<li><a href="#ipc-events">IPC Events</a></li>
<li><a href="#multiple-ipc-instances">Multiple IPC instances</a></li>
<li>
<a href="#basic-examples">Basic Examples</a>
<ol>
<li><a href="#server-for-unix-sockets--tcp-sockets">Server for Unix||Windows Sockets &amp; TCP Sockets</a></li>
<li><a href="#client-for-unix-sockets--tcp-sockets">Client for Unix||Windows Sockets &amp; TCP Sockets</a></li>
<li><a href="#server--client-for-udp-sockets">Server &amp; Client for UDP Sockets</a></li>
<li><a href="#raw-buffer-or-binary-sockets">Raw Buffers, Real Time and / or Binary Sockets</a></li>
</ol>
</li>
<li><a href="https://github.com/RIAEvangelist/node-ipc/tree/master/example/TLSSocket">Working with TLS/SSL Socket Servers &amp; Clients</a></li>
<li><a href="https://github.com/RIAEvangelist/node-ipc/tree/master/example">Node Code Examples</a></li>
</ol>
<hr>
<h4>
<a id="types-of-ipc-sockets" class="anchor" href="#types-of-ipc-sockets" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Types of IPC Sockets</h4>
<table>
<thead>
<tr>
<th>Type</th>
<th>Stability</th>
<th>Definition</th>
</tr>
</thead>
<tbody>
<tr>
<td>Unix Socket or Windows Socket</td>
<td>Stable</td>
<td>Gives Linux, Mac, and Windows lightning fast communication and avoids the network card to reduce overhead and latency. <a href="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>
</tr>
<tr>
<td>TCP Socket</td>
<td>Stable</td>
<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. <a href="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>
</tr>
<tr>
<td>TLS Socket</td>
<td>Stable</td>
<td>Configurable and secure network socket over SSL. Equivalent to https. <a href="https://github.com/RIAEvangelist/node-ipc/tree/master/example/TLSSocket">TLS/SSL documentation</a>
</td>
</tr>
<tr>
<td>UDP Sockets</td>
<td>Stable</td>
<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. <a href="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>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>OS</th>
<th>Supported Sockets</th>
</tr>
</thead>
<tbody>
<tr>
<td>Linux</td>
<td>Unix, Posix, TCP, TLS, UDP</td>
</tr>
<tr>
<td>Mac</td>
<td>Unix, Posix, TCP, TLS, UDP</td>
</tr>
<tr>
<td>Win</td>
<td>Windows, TCP, TLS, UDP</td>
</tr>
</tbody>
</table>
<hr>
<h4>
<a id="ipc-config" class="anchor" href="#ipc-config" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>IPC Config</h4>
<p><code>ipc.config</code> </p>
<p>Set these variables in the <code>ipc.config</code> scope to overwrite or set default values.</p>
<div class="highlight highlight-source-js"><pre>
{
appspace <span class="pl-k">:</span> <span class="pl-s"><span class="pl-pds">'</span>app.<span class="pl-pds">'</span></span>,
socketRoot <span class="pl-k">:</span> <span class="pl-s"><span class="pl-pds">'</span>/tmp/<span class="pl-pds">'</span></span>,
id <span class="pl-k">:</span> <span class="pl-smi">os</span>.<span class="pl-c1">hostname</span>(),
networkHost <span class="pl-k">:</span> <span class="pl-s"><span class="pl-pds">'</span>localhost<span class="pl-pds">'</span></span>, <span class="pl-c">//should resolve to 127.0.0.1 or ::1 see the table below related to this</span>
networkPort <span class="pl-k">:</span> <span class="pl-c1">8000</span>,
encoding <span class="pl-k">:</span> <span class="pl-s"><span class="pl-pds">'</span>utf8<span class="pl-pds">'</span></span>,
rawBuffer <span class="pl-k">:</span> <span class="pl-c1">false</span>,
sync <span class="pl-k">:</span> <span class="pl-c1">false</span>,
silent <span class="pl-k">:</span> <span class="pl-c1">false</span>,
logInColor <span class="pl-k">:</span> <span class="pl-c1">true</span>,
logDepth <span class="pl-k">:</span> <span class="pl-c1">5</span>,
maxConnections <span class="pl-k">:</span> <span class="pl-c1">100</span>,
retry <span class="pl-k">:</span> <span class="pl-c1">500</span>,
maxRetries <span class="pl-k">:</span> <span class="pl-c1">false</span>,
stopRetrying <span class="pl-k">:</span> <span class="pl-c1">false</span>
}
</pre></div>
<table>
<thead>
<tr>
<th>variable</th>
<th>documentation</th>
</tr>
</thead>
<tbody>
<tr>
<td>appspace</td>
<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. 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>
</tr>
<tr>
<td>sync</td>
<td>synchronous requests. Clients will not send new requests until the server answers.</td>
</tr>
<tr>
<td>silent</td>
<td>turn on/off logging default is false which means logging is on</td>
</tr>
<tr>
<td>logInColor</td>
<td>turn on/off util.inspect colors for ipc.log</td>
</tr>
<tr>
<td>logDepth</td>
<td>set the depth for util.inspect during ipc.log</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>
</tr>
<tr>
<td>maxRetries</td>
<td>if set, it represents the maximum number of retries after each disconnect before giving up and completely killing a specific connection</td>
</tr>
<tr>
<td>stopRetrying</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 true in real time it will immediately stop trying to connect regardless of maxRetries. If set to 0, the client will <strong><em>NOT</em></strong> try to reconnect.</td>
</tr>
</tbody>
</table>
<hr>
<h4>
<a id="ipc-methods" class="anchor" href="#ipc-methods" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>IPC Methods</h4>
<p>These methods are available in the IPC Scope. </p>
<hr>
<h5>
<a id="log" class="anchor" href="#log" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>log</h5>
<p><code>ipc.log(a,b,c,d,e...);</code> </p>
<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 single space ' ' between them and then log them to the console. This is fast because it prevents any concatenation from happening if the ipc.config.silent is set <code>true</code>. That way if you leave your logging in place it should have almost no effect on performance.</p>
<p>The log also uses util.inspect You can control if it should log in color as well as the log depth via <code>ipc.config</code></p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-smi">config</span>.<span class="pl-smi">logInColor</span><span class="pl-k">=</span><span class="pl-c1">true</span>; <span class="pl-c">//default</span>
<span class="pl-smi">ipc</span>.<span class="pl-smi">config</span>.<span class="pl-smi">logDepth</span><span class="pl-k">=</span><span class="pl-c1">5</span>; <span class="pl-c">//default </span>
</pre></div>
<hr>
<h5>
<a id="connectto" class="anchor" href="#connectto" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>connectTo</h5>
<p><code>ipc.connectTo(id,path,callback);</code> </p>
<p>Used for connecting as a client to local Unix Sockets and Windows Sockets. <strong><em>This is the fastest 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>
<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 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 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>
</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 so long as they are still in order.</p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-en">connectTo</span>(<span class="pl-s"><span class="pl-pds">'</span>world<span class="pl-pds">'</span></span>);
</pre></div>
<p>or using just an id and a callback</p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-en">connectTo</span>(
<span class="pl-s"><span class="pl-pds">'</span>world<span class="pl-pds">'</span></span>,
<span class="pl-k">function</span>(){
<span class="pl-smi">ipc</span>.<span class="pl-smi">of</span>.<span class="pl-smi">world</span>.<span class="pl-en">on</span>(
<span class="pl-s"><span class="pl-pds">'</span>hello<span class="pl-pds">'</span></span>,
<span class="pl-k">function</span>(<span class="pl-smi">data</span>){
<span class="pl-smi">ipc</span>.<span class="pl-c1">log</span>(<span class="pl-smi">data</span>.<span class="pl-smi">debug</span>);
<span class="pl-c">//if data was a string, it would have the color set to the debug style applied to it</span>
}
)
}
);
</pre></div>
<p>or explicitly setting the path</p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-en">connectTo</span>(
<span class="pl-s"><span class="pl-pds">'</span>world<span class="pl-pds">'</span></span>,
<span class="pl-s"><span class="pl-pds">'</span>myapp.world<span class="pl-pds">'</span></span>
);
</pre></div>
<p>or explicitly setting the path with callback</p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-en">connectTo</span>(
<span class="pl-s"><span class="pl-pds">'</span>world<span class="pl-pds">'</span></span>,
<span class="pl-s"><span class="pl-pds">'</span>myapp.world<span class="pl-pds">'</span></span>,
<span class="pl-k">function</span>(){
<span class="pl-k">...</span>
}
);
</pre></div>
<hr>
<h5>
<a id="connecttonet" class="anchor" href="#connecttonet" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>connectToNet</h5>
<p><code>ipc.connectToNet(id,host,port,callback)</code> </p>
<p>Used to connect as a client to a TCP or <a href="https://github.com/RIAEvangelist/node-ipc/tree/master/example/TLSSocket">TLS socket</a> 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>
<p>For TLS and SSL Sockets see the <a href="https://github.com/RIAEvangelist/node-ipc/tree/master/example/TLSSocket">node-ipc TLS and SSL docs</a>. They have a few additional requirements, and things to know about and so have their own doc.</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 being connected to. For TCP &amp; 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>
</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 so long 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>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-en">connectToNet</span>(<span class="pl-s"><span class="pl-pds">'</span>world<span class="pl-pds">'</span></span>);
</pre></div>
<p>or using just an id and a callback</p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-en">connectToNet</span>(
<span class="pl-s"><span class="pl-pds">'</span>world<span class="pl-pds">'</span></span>,
<span class="pl-k">function</span>(){
<span class="pl-k">...</span>
}
);
</pre></div>
<p>or explicitly setting the host and path</p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-en">connectToNet</span>(
<span class="pl-s"><span class="pl-pds">'</span>world<span class="pl-pds">'</span></span>,
<span class="pl-s"><span class="pl-pds">'</span>myapp.com<span class="pl-pds">'</span></span>,<span class="pl-en">serve</span>(path,callback)
<span class="pl-c1">3435</span>
);
</pre></div>
<p>or only explicitly setting port and callback</p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-en">connectToNet</span>(
<span class="pl-s"><span class="pl-pds">'</span>world<span class="pl-pds">'</span></span>,
<span class="pl-c1">3435</span>,
<span class="pl-k">function</span>(){
<span class="pl-k">...</span>
}
);
</pre></div>
<hr>
<h5>
<a id="disconnect" class="anchor" href="#disconnect" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>disconnect</h5>
<p><code>ipc.disconnect(id)</code> </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>
<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>
</tr>
</tbody>
</table>
<p><strong>examples</strong></p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-en">disconnect</span>(<span class="pl-s"><span class="pl-pds">'</span>world<span class="pl-pds">'</span></span>);
</pre></div>
<hr>
<h5>
<a id="serve" class="anchor" href="#serve" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>serve</h5>
<p><code>ipc.serve(path,callback);</code> </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>
<table>
<thead>
<tr>
<th>variable</th>
<th>required</th>
<th>definition</th>
</tr>
</thead>
<tbody>
<tr>
<td>path</td>
<td>optional</td>
<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>
</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 omitted so long as they are still in order.</p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-en">serve</span>();
</pre></div>
<p>or specifying callback</p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-en">serve</span>(
<span class="pl-k">function</span>(){<span class="pl-k">...</span>}
);
</pre></div>
<p>or specify path</p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-en">serve</span>(
<span class="pl-s"><span class="pl-pds">'</span>/tmp/myapp.myservice<span class="pl-pds">'</span></span>
);
</pre></div>
<p>or specifying everything</p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-en">serve</span>(
<span class="pl-s"><span class="pl-pds">'</span>/tmp/myapp.myservice<span class="pl-pds">'</span></span>,
<span class="pl-k">function</span>(){<span class="pl-k">...</span>}
);
</pre></div>
<hr>
<h5>
<a id="servenet" class="anchor" href="#servenet" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>serveNet</h5>
<p><code>serveNet(host,port,UDPType,callback)</code></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 the first address in os.networkInterfaces(). For TCP, TLS &amp; UDP servers this is most likely going to be 127.0.0.1 or ::1</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. When using udp6 make sure to specify a valid IPv6 host, like <code>::1</code>
</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>
<p>default tcp server</p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-en">serveNet</span>();
</pre></div>
<p>default udp server</p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-en">serveNet</span>(<span class="pl-s"><span class="pl-pds">'</span>udp4<span class="pl-pds">'</span></span>);
</pre></div>
<p>or specifying TCP server with callback</p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-en">serveNet</span>(
<span class="pl-k">function</span>(){<span class="pl-k">...</span>}
);
</pre></div>
<p>or specifying UDP server with callback</p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-en">serveNet</span>(
<span class="pl-s"><span class="pl-pds">'</span>udp4<span class="pl-pds">'</span></span>,
<span class="pl-k">function</span>(){<span class="pl-k">...</span>}
);
</pre></div>
<p>or specify port</p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-en">serveNet</span>(
<span class="pl-c1">3435</span>
);
</pre></div>
<p>or specifying everything TCP</p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-en">serveNet</span>(
<span class="pl-s"><span class="pl-pds">'</span>MyMostAwesomeApp.com<span class="pl-pds">'</span></span>,
<span class="pl-c1">3435</span>,
<span class="pl-k">function</span>(){<span class="pl-k">...</span>}
);
</pre></div>
<p>or specifying everything UDP</p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-en">serveNet</span>(
<span class="pl-s"><span class="pl-pds">'</span>MyMostAwesomeApp.com<span class="pl-pds">'</span></span>,
<span class="pl-c1">3435</span>,
<span class="pl-s"><span class="pl-pds">'</span>udp4<span class="pl-pds">'</span></span>,
<span class="pl-k">function</span>(){<span class="pl-k">...</span>}
);
</pre></div>
<hr>
<h3>
<a id="ipc-stores-and-default-variables" class="anchor" href="#ipc-stores-and-default-variables" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>IPC Stores and Default Variables</h3>
<table>
<thead>
<tr>
<th>variable</th>
<th>definition</th>
</tr>
</thead>
<tbody>
<tr>
<td>ipc.of</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>
</td>
</tr>
</tbody>
</table>
<hr>
<h3>
<a id="ipc-server-methods" class="anchor" href="#ipc-server-methods" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>IPC Server Methods</h3>
<table>
<thead>
<tr>
<th>method</th>
<th>definition</th>
</tr>
</thead>
<tbody>
<tr>
<td>start</td>
<td>start serving need top call <code>serve</code> or <code>serveNet</code> first to set up the server</td>
</tr>
<tr>
<td>stop</td>
<td>close the server and stop serving</td>
</tr>
</tbody>
</table>
<hr>
<h3>
<a id="ipc-events" class="anchor" href="#ipc-events" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>IPC Events</h3>
<table>
<thead>
<tr>
<th>event name</th>
<th>params</th>
<th>definition</th>
</tr>
</thead>
<tbody>
<tr>
<td>error</td>
<td>err obj</td>
<td>triggered when an error has occured</td>
</tr>
<tr>
<td>connect</td>
<td></td>
<td>triggered when socket connected</td>
</tr>
<tr>
<td>disconnect</td>
<td></td>
<td>triggered when socket disconnected</td>
</tr>
<tr>
<td>destroy</td>
<td></td>
<td>triggered when socket has been totally destroyed, no further auto retries will happen and all references are gone.</td>
</tr>
<tr>
<td>data</td>
<td>buffer</td>
<td>triggered when ipc.config.rawBuffer is true and a message is received.</td>
</tr>
<tr>
<td><strong><em>your event type</em></strong></td>
<td><strong><em>your event data</em></strong></td>
<td>triggered when a JSON message is received. The event name will be the type string from your message and the param will be the data object from your message eg : <code>{ type:'myEvent',data:{a:1}}</code>
</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<h3>
<a id="multiple-ipc-instances" class="anchor" href="#multiple-ipc-instances" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Multiple IPC Instances</h3>
<p>Sometimes you might need explicit and independent instances of node-ipc. Just for such scenarios we have exposed the core IPC class on the IPC singleton.</p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-k">const</span> <span class="pl-c1">RawIPC</span><span class="pl-k">=</span><span class="pl-c1">require</span>(<span class="pl-s"><span class="pl-pds">'</span>node-ipc<span class="pl-pds">'</span></span>).<span class="pl-c1">IPC</span>;
<span class="pl-k">const</span> <span class="pl-c1">ipc</span><span class="pl-k">=</span><span class="pl-k">new</span> <span class="pl-en">RawIPC</span>;
<span class="pl-k">const</span> <span class="pl-c1">someOtherExplicitIPC</span><span class="pl-k">=</span><span class="pl-k">new</span> <span class="pl-en">RawIPC</span>;
<span class="pl-c">//OR</span>
<span class="pl-k">const</span> <span class="pl-c1">ipc</span><span class="pl-k">=</span><span class="pl-c1">require</span>(<span class="pl-s"><span class="pl-pds">'</span>node-ipc<span class="pl-pds">'</span></span>);
<span class="pl-k">const</span> <span class="pl-c1">someOtherExplicitIPC</span><span class="pl-k">=</span><span class="pl-k">new</span> <span class="pl-en">ipc.IPC</span>;
<span class="pl-c">//setting explicit configs</span>
<span class="pl-c">//keep one silent and the other verbose</span>
<span class="pl-smi">ipc</span>.<span class="pl-smi">config</span>.<span class="pl-smi">silent</span><span class="pl-k">=</span><span class="pl-c1">true</span>;
<span class="pl-smi">someOtherExplicitIPC</span>.<span class="pl-smi">config</span>.<span class="pl-smi">silent</span><span class="pl-k">=</span><span class="pl-c1">true</span>;
<span class="pl-c">//make one a raw binary and the other json based ipc</span>
<span class="pl-smi">ipc</span>.<span class="pl-smi">config</span>.<span class="pl-smi">rawBuffer</span><span class="pl-k">=</span><span class="pl-c1">false</span>;
<span class="pl-smi">someOtherExplicitIPC</span>.<span class="pl-smi">config</span>.<span class="pl-smi">rawBuffer</span><span class="pl-k">=</span><span class="pl-c1">true</span>;
<span class="pl-smi">someOtherExplicitIPC</span>.<span class="pl-smi">config</span>.<span class="pl-c1">encoding</span><span class="pl-k">=</span><span class="pl-s"><span class="pl-pds">'</span>hex<span class="pl-pds">'</span></span>;
</pre></div>
<hr>
<h3>
<a id="basic-examples" class="anchor" href="#basic-examples" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Basic Examples</h3>
<p>You can find <a href="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>
<h4>
<a id="server-for-unix-sockets-windows-sockets--tcp-sockets" class="anchor" href="#server-for-unix-sockets-windows-sockets--tcp-sockets" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Server for Unix Sockets, Windows Sockets &amp; 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>
<div class="highlight highlight-source-js"><pre>
<span class="pl-k">var</span> ipc<span class="pl-k">=</span><span class="pl-c1">require</span>(<span class="pl-s"><span class="pl-pds">'</span>node-ipc<span class="pl-pds">'</span></span>);
<span class="pl-smi">ipc</span>.<span class="pl-smi">config</span>.<span class="pl-c1">id</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">'</span>world<span class="pl-pds">'</span></span>;
<span class="pl-smi">ipc</span>.<span class="pl-smi">config</span>.<span class="pl-smi">retry</span><span class="pl-k">=</span> <span class="pl-c1">1500</span>;
<span class="pl-smi">ipc</span>.<span class="pl-en">serve</span>(
<span class="pl-k">function</span>(){
<span class="pl-smi">ipc</span>.<span class="pl-smi">server</span>.<span class="pl-en">on</span>(
<span class="pl-s"><span class="pl-pds">'</span>message<span class="pl-pds">'</span></span>,
<span class="pl-k">function</span>(<span class="pl-smi">data</span>,<span class="pl-smi">socket</span>){
<span class="pl-smi">ipc</span>.<span class="pl-c1">log</span>(<span class="pl-s"><span class="pl-pds">'</span>got a message : <span class="pl-pds">'</span></span>.<span class="pl-smi">debug</span>, data);
<span class="pl-smi">ipc</span>.<span class="pl-smi">server</span>.<span class="pl-en">emit</span>(
socket,
<span class="pl-s"><span class="pl-pds">'</span>message<span class="pl-pds">'</span></span>, <span class="pl-c">//this can be anything you want so long as</span>
<span class="pl-c">//your client knows.</span>
data<span class="pl-k">+</span><span class="pl-s"><span class="pl-pds">'</span> world!<span class="pl-pds">'</span></span>
);
}
);
}
);
<span class="pl-smi">ipc</span>.<span class="pl-smi">server</span>.<span class="pl-c1">start</span>();
</pre></div>
<h4>
<a id="client-for-unix-sockets--tcp-sockets" class="anchor" href="#client-for-unix-sockets--tcp-sockets" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Client for Unix Sockets &amp; 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>
<div class="highlight highlight-source-js"><pre>
<span class="pl-k">var</span> ipc<span class="pl-k">=</span><span class="pl-c1">require</span>(<span class="pl-s"><span class="pl-pds">'</span>node-ipc<span class="pl-pds">'</span></span>);
<span class="pl-smi">ipc</span>.<span class="pl-smi">config</span>.<span class="pl-c1">id</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">'</span>hello<span class="pl-pds">'</span></span>;
<span class="pl-smi">ipc</span>.<span class="pl-smi">config</span>.<span class="pl-smi">retry</span><span class="pl-k">=</span> <span class="pl-c1">1500</span>;
<span class="pl-smi">ipc</span>.<span class="pl-en">connectTo</span>(
<span class="pl-s"><span class="pl-pds">'</span>world<span class="pl-pds">'</span></span>,
<span class="pl-k">function</span>(){
<span class="pl-smi">ipc</span>.<span class="pl-smi">of</span>.<span class="pl-smi">world</span>.<span class="pl-en">on</span>(
<span class="pl-s"><span class="pl-pds">'</span>connect<span class="pl-pds">'</span></span>,
<span class="pl-k">function</span>(){
<span class="pl-smi">ipc</span>.<span class="pl-c1">log</span>(<span class="pl-s"><span class="pl-pds">'</span>## connected to world ##<span class="pl-pds">'</span></span>.<span class="pl-smi">rainbow</span>, <span class="pl-smi">ipc</span>.<span class="pl-smi">config</span>.<span class="pl-smi">delay</span>);
<span class="pl-smi">ipc</span>.<span class="pl-smi">of</span>.<span class="pl-smi">world</span>.<span class="pl-en">emit</span>(
<span class="pl-s"><span class="pl-pds">'</span>message<span class="pl-pds">'</span></span>, <span class="pl-c">//any event or message type your server listens for</span>
<span class="pl-s"><span class="pl-pds">'</span>hello<span class="pl-pds">'</span></span>
)
}
);
<span class="pl-smi">ipc</span>.<span class="pl-smi">of</span>.<span class="pl-smi">world</span>.<span class="pl-en">on</span>(
<span class="pl-s"><span class="pl-pds">'</span>disconnect<span class="pl-pds">'</span></span>,
<span class="pl-k">function</span>(){
<span class="pl-smi">ipc</span>.<span class="pl-c1">log</span>(<span class="pl-s"><span class="pl-pds">'</span>disconnected from world<span class="pl-pds">'</span></span>.<span class="pl-smi">notice</span>);
}
);
<span class="pl-smi">ipc</span>.<span class="pl-smi">of</span>.<span class="pl-smi">world</span>.<span class="pl-en">on</span>(
<span class="pl-s"><span class="pl-pds">'</span>message<span class="pl-pds">'</span></span>, <span class="pl-c">//any event or message type your server listens for</span>
<span class="pl-k">function</span>(<span class="pl-smi">data</span>){
<span class="pl-smi">ipc</span>.<span class="pl-c1">log</span>(<span class="pl-s"><span class="pl-pds">'</span>got a message from world : <span class="pl-pds">'</span></span>.<span class="pl-smi">debug</span>, data);
}
);
}
);
</pre></div>
<h4>
<a id="server--client-for-udp-sockets" class="anchor" href="#server--client-for-udp-sockets" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Server &amp; Client for UDP Sockets</h4>
<p>UDP Sockets are different than Unix, Windows &amp; 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>This is the most basic example which will work for both local and remote UDP Sockets.</p>
<h5>
<a id="udp-server-1---world" class="anchor" href="#udp-server-1---world" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>UDP Server 1 - "World"</h5>
<div class="highlight highlight-source-js"><pre>
<span class="pl-k">var</span> ipc<span class="pl-k">=</span><span class="pl-c1">require</span>(<span class="pl-s"><span class="pl-pds">'</span>../../../node-ipc<span class="pl-pds">'</span></span>);
<span class="pl-smi">ipc</span>.<span class="pl-smi">config</span>.<span class="pl-c1">id</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">'</span>world<span class="pl-pds">'</span></span>;
<span class="pl-smi">ipc</span>.<span class="pl-smi">config</span>.<span class="pl-smi">retry</span><span class="pl-k">=</span> <span class="pl-c1">1500</span>;
<span class="pl-smi">ipc</span>.<span class="pl-en">serveNet</span>(
<span class="pl-s"><span class="pl-pds">'</span>udp4<span class="pl-pds">'</span></span>,
<span class="pl-k">function</span>(){
<span class="pl-en">console</span>.<span class="pl-c1">log</span>(<span class="pl-c1">123</span>);
<span class="pl-smi">ipc</span>.<span class="pl-smi">server</span>.<span class="pl-en">on</span>(
<span class="pl-s"><span class="pl-pds">'</span>message<span class="pl-pds">'</span></span>,
<span class="pl-k">function</span>(<span class="pl-smi">data</span>,<span class="pl-smi">socket</span>){
<span class="pl-smi">ipc</span>.<span class="pl-c1">log</span>(<span class="pl-s"><span class="pl-pds">'</span>got a message from <span class="pl-pds">'</span></span>.<span class="pl-smi">debug</span>, <span class="pl-smi">data</span>.<span class="pl-smi">from</span>.<span class="pl-smi">variable</span> ,<span class="pl-s"><span class="pl-pds">'</span> : <span class="pl-pds">'</span></span>.<span class="pl-smi">debug</span>, <span class="pl-smi">data</span>.<span class="pl-smi">message</span>.<span class="pl-smi">variable</span>);
<span class="pl-smi">ipc</span>.<span class="pl-smi">server</span>.<span class="pl-en">emit</span>(
socket,
<span class="pl-s"><span class="pl-pds">'</span>message<span class="pl-pds">'</span></span>,
{
from <span class="pl-k">:</span> <span class="pl-smi">ipc</span>.<span class="pl-smi">config</span>.<span class="pl-c1">id</span>,
message <span class="pl-k">:</span> <span class="pl-smi">data</span>.<span class="pl-smi">message</span><span class="pl-k">+</span><span class="pl-s"><span class="pl-pds">'</span> world!<span class="pl-pds">'</span></span>
}
);
}
);
<span class="pl-en">console</span>.<span class="pl-c1">log</span>(<span class="pl-smi">ipc</span>.<span class="pl-smi">server</span>);
}
);
<span class="pl-smi">ipc</span>.<span class="pl-smi">server</span>.<span class="pl-c1">start</span>();
</pre></div>
<h5>
<a id="udp-server-2---hello" class="anchor" href="#udp-server-2---hello" aria-hidden="true"><span aria-hidden="true" class="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>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-smi">config</span>.<span class="pl-c1">id</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">'</span>hello<span class="pl-pds">'</span></span>;
<span class="pl-smi">ipc</span>.<span class="pl-smi">config</span>.<span class="pl-smi">retry</span><span class="pl-k">=</span> <span class="pl-c1">1500</span>;
<span class="pl-smi">ipc</span>.<span class="pl-en">serveNet</span>(
<span class="pl-c1">8001</span>,
<span class="pl-s"><span class="pl-pds">'</span>udp4<span class="pl-pds">'</span></span>,
<span class="pl-k">function</span>(){
<span class="pl-smi">ipc</span>.<span class="pl-smi">server</span>.<span class="pl-en">on</span>(
<span class="pl-s"><span class="pl-pds">'</span>message<span class="pl-pds">'</span></span>,
<span class="pl-k">function</span>(<span class="pl-smi">data</span>){
<span class="pl-smi">ipc</span>.<span class="pl-c1">log</span>(<span class="pl-s"><span class="pl-pds">'</span>got Data<span class="pl-pds">'</span></span>);
<span class="pl-smi">ipc</span>.<span class="pl-c1">log</span>(<span class="pl-s"><span class="pl-pds">'</span>got a message from <span class="pl-pds">'</span></span>.<span class="pl-smi">debug</span>, <span class="pl-smi">data</span>.<span class="pl-smi">from</span>.<span class="pl-smi">variable</span> ,<span class="pl-s"><span class="pl-pds">'</span> : <span class="pl-pds">'</span></span>.<span class="pl-smi">debug</span>, <span class="pl-smi">data</span>.<span class="pl-smi">message</span>.<span class="pl-smi">variable</span>);
}
);
<span class="pl-smi">ipc</span>.<span class="pl-smi">server</span>.<span class="pl-en">emit</span>(
{
address <span class="pl-k">:</span> <span class="pl-s"><span class="pl-pds">'</span>127.0.0.1<span class="pl-pds">'</span></span>, <span class="pl-c">//any hostname will work</span>
port <span class="pl-k">:</span> <span class="pl-smi">ipc</span>.<span class="pl-smi">config</span>.<span class="pl-smi">networkPort</span>
},
<span class="pl-s"><span class="pl-pds">'</span>message<span class="pl-pds">'</span></span>,
{
from <span class="pl-k">:</span> <span class="pl-smi">ipc</span>.<span class="pl-smi">config</span>.<span class="pl-c1">id</span>,
message <span class="pl-k">:</span> <span class="pl-s"><span class="pl-pds">'</span>Hello<span class="pl-pds">'</span></span>
}
);
}
);
<span class="pl-smi">ipc</span>.<span class="pl-smi">server</span>.<span class="pl-c1">start</span>();
</pre></div>
<h4>
<a id="raw-buffer-or-binary-sockets" class="anchor" href="#raw-buffer-or-binary-sockets" aria-hidden="true"><span aria-hidden="true" class="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. These may come in handy if working with embedded systems or C / C++ processes. You can even make sure to match C or C++ string typing.</p>
<p>When setting up a rawBuffer socket you must specify it as such :</p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-smi">config</span>.<span class="pl-smi">rawBuffer</span><span class="pl-k">=</span><span class="pl-c1">true</span>;
</pre></div>
<p>You can also specify its encoding type. The default is <code>utf8</code></p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-smi">config</span>.<span class="pl-c1">encoding</span><span class="pl-k">=</span><span class="pl-s"><span class="pl-pds">'</span>utf8<span class="pl-pds">'</span></span>;
</pre></div>
<p>emit string buffer :</p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-c">//server</span>
<span class="pl-smi">ipc</span>.<span class="pl-smi">server</span>.<span class="pl-en">emit</span>(
socket,
<span class="pl-s"><span class="pl-pds">'</span>hello<span class="pl-pds">'</span></span>
);
<span class="pl-c">//client</span>
<span class="pl-smi">ipc</span>.<span class="pl-smi">of</span>.<span class="pl-smi">world</span>.<span class="pl-en">emit</span>(
<span class="pl-s"><span class="pl-pds">'</span>hello<span class="pl-pds">'</span></span>
)
</pre></div>
<p>emit byte array buffer :</p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-c">//hex encoding may work best for this.</span>
<span class="pl-smi">ipc</span>.<span class="pl-smi">config</span>.<span class="pl-c1">encoding</span><span class="pl-k">=</span><span class="pl-s"><span class="pl-pds">'</span>hex<span class="pl-pds">'</span></span>;
<span class="pl-c">//server</span>
<span class="pl-smi">ipc</span>.<span class="pl-smi">server</span>.<span class="pl-en">emit</span>(
socket,
[<span class="pl-c1">10</span>,<span class="pl-c1">20</span>,<span class="pl-c1">30</span>]
);
<span class="pl-c">//client</span>
<span class="pl-smi">ipc</span>.<span class="pl-smi">server</span>.<span class="pl-en">emit</span>(
[<span class="pl-c1">10</span>,<span class="pl-c1">20</span>,<span class="pl-c1">30</span>]
);
</pre></div>
<p>emit binary or hex array buffer, this is best for real time data transfer, especially whan connecting to C or C++ processes, or embedded systems :</p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-smi">config</span>.<span class="pl-c1">encoding</span><span class="pl-k">=</span><span class="pl-s"><span class="pl-pds">'</span>hex<span class="pl-pds">'</span></span>;
<span class="pl-c">//server</span>
<span class="pl-smi">ipc</span>.<span class="pl-smi">server</span>.<span class="pl-en">emit</span>(
socket,
[<span class="pl-c1">0x05</span>,<span class="pl-c1">0x6d</span>,<span class="pl-c1">0x5c</span>]
);
<span class="pl-c">//client</span>
<span class="pl-smi">ipc</span>.<span class="pl-smi">server</span>.<span class="pl-en">emit</span>(
[<span class="pl-c1">0x05</span>,<span class="pl-c1">0x6d</span>,<span class="pl-c1">0x5c</span>]
);
</pre></div>
<p>Writing explicit buffers, int types, doubles, floats etc. as well as big endian and little endian data to raw buffer nostly valuable when connecting to C or C++ processes, or embedded systems (see more detailed info on buffers as well as UInt, Int, double etc. here)[<a href="https://nodejs.org/api/buffer.html">https://nodejs.org/api/buffer.html</a>]:</p>
<div class="highlight highlight-source-js"><pre>
<span class="pl-smi">ipc</span>.<span class="pl-smi">config</span>.<span class="pl-c1">encoding</span><span class="pl-k">=</span><span class="pl-s"><span class="pl-pds">'</span>hex<span class="pl-pds">'</span></span>;
<span class="pl-c">//make a 6 byte buffer for example</span>
<span class="pl-k">const</span> <span class="pl-c1">myBuffer</span><span class="pl-k">=</span><span class="pl-k">new</span> <span class="pl-en">Buffer</span>(<span class="pl-c1">6</span>).<span class="pl-en">fill</span>(<span class="pl-c1">0</span>);
<span class="pl-c">//fill the first 2 bytes with a 16 bit (2 byte) short unsigned int</span>
<span class="pl-c">//write a UInt16 (2 byte or short) as Big Endian</span>
<span class="pl-smi">myBuffer</span>.<span class="pl-en">writeUInt16BE</span>(
<span class="pl-c1">2</span>, <span class="pl-c">//value to write</span>
<span class="pl-c1">0</span> <span class="pl-c">//offset in bytes</span>
);
<span class="pl-c">//OR</span>
<span class="pl-smi">myBuffer</span>.<span class="pl-en">writeUInt16LE</span>(<span class="pl-c1">0x2</span>,<span class="pl-c1">0</span>);
<span class="pl-c">//OR</span>
<span class="pl-smi">myBuffer</span>.<span class="pl-en">writeUInt16LE</span>(<span class="pl-c1">0x02</span>,<span class="pl-c1">0</span>);
<span class="pl-c">//fill the remaining 4 bytes with a 32 bit (4 byte) long unsigned int</span>
<span class="pl-c">//write a UInt32 (4 byte or long) as Big Endian</span>
<span class="pl-smi">myBuffer</span>.<span class="pl-en">writeUInt32BE</span>(
<span class="pl-c1">16772812</span>, <span class="pl-c">//value to write</span>
<span class="pl-c1">2</span> <span class="pl-c">//offset in bytes</span>
);
<span class="pl-c">//OR</span>
<span class="pl-smi">myBuffer</span>.<span class="pl-en">writeUInt32BE</span>(<span class="pl-c1">0xffeecc</span>,<span class="pl-c1">0</span>)
<span class="pl-c">//server</span>
<span class="pl-smi">ipc</span>.<span class="pl-smi">server</span>.<span class="pl-en">emit</span>(
socket,
myBuffer
);
<span class="pl-c">//client</span>
<span class="pl-smi">ipc</span>.<span class="pl-smi">server</span>.<span class="pl-en">emit</span>(
myBuffer
);
</pre></div>
<h4>
<a id="licensed-under-dbad-license" class="anchor" href="#licensed-under-dbad-license" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Licensed under DBAD license</h4>
<p>See the <a href="https://github.com/philsturgeon/dbad">DBAD license</a> in your language or our <a href="https://github.com/RIAEvangelist/node-phidget-API/blob/master/license.md">licence.md</a> file.</p>
</section>
</div>
<!-- FOOTER -->
<div id="footer_wrap" class="outer">
<footer class="inner">
<p class="copyright">Node-ipc maintained by <a href="https://github.com/RIAEvangelist">RIAEvangelist</a></p>
<p>Published with <a href="https://pages.github.com">GitHub Pages</a></p>
</footer>
</div>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-48524110-1");
pageTracker._trackPageview();
} catch(err) {}
</script>
</body>
</html>