node-ipc/index.html

882 lines
28 KiB
HTML
Raw Normal View History

2014-02-28 08:32:34 +11:00
<!DOCTYPE html>
<html>
<head>
2014-08-28 14:15:30 +10:00
<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">
2014-02-28 08:32:34 +11:00
<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>
2014-03-01 07:30:01 +11:00
<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>
2014-02-28 08:32:34 +11:00
<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>
2015-04-16 14:39:03 +10:00
<a id="node-ipc" class="anchor" href="#node-ipc" aria-hidden="true"><span class="octicon octicon-link"></span></a>node-ipc</h1>
2014-02-28 08:32:34 +11:00
2015-09-27 22:06:15 +10:00
<p><em>a nodejs module for local and remote Inter Process Communication</em> with full support for Linux, Mac and Windows.<br>
2014-03-01 07:30:01 +11:00
A great solution for <strong>Neural Networking</strong> in Node.JS</p>
2014-02-28 08:32:34 +11:00
2015-09-27 22:06:15 +10:00
<p><strong>npm install node-ipc</strong></p>
<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>
<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>
2014-02-28 08:32:34 +11:00
2014-02-28 11:04:14 +11:00
<p>Package details websites :</p>
2014-02-28 08:32:34 +11:00
2014-02-28 11:04:14 +11:00
<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>
2014-10-28 13:26:05 +11:00
</ul>
2014-08-28 14:15:30 +10:00
2015-09-27 22:06:15 +10:00
<p>This work is licenced via the <a href="http://www.dbad-license.org/">DBAD Public Licence</a>.</p>
2014-10-28 13:26:05 +11:00
<hr>
<h4>
2015-04-16 14:39:03 +10:00
<a id="contents" class="anchor" href="#contents" aria-hidden="true"><span class="octicon octicon-link"></span></a>Contents</h4>
2014-08-28 14:15:30 +10:00
<ol>
<li><a href="#types-of-ipc-sockets">Types of IPC Sockets and Supporting OS</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="#basic-examples">Basic Examples</a>
<ol>
2015-09-27 22:06:15 +10:00
<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>
2014-08-28 14:15:30 +10:00
<li><a href="#server--client-for-udp-sockets">Server &amp; Client for UDP Sockets</a></li>
</ol>
</li>
<li><a href="https://github.com/RIAEvangelist/node-ipc/tree/master/example">Advanced Examples</a></li>
2014-10-28 13:26:05 +11:00
</ol>
<hr>
<h4>
2015-04-16 14:39:03 +10:00
<a id="types-of-ipc-sockets" class="anchor" href="#types-of-ipc-sockets" aria-hidden="true"><span class="octicon octicon-link"></span></a>Types of IPC Sockets</h4>
2014-02-28 08:32:34 +11:00
<table>
2014-10-28 13:26:05 +11:00
<thead>
<tr>
2014-02-28 08:32:34 +11:00
<th>Type</th>
2014-02-28 11:04:14 +11:00
<th>Stability</th>
2014-02-28 08:32:34 +11:00
<th>Definition</th>
2014-10-28 13:26:05 +11:00
</tr>
</thead>
2014-02-28 08:32:34 +11:00
<tbody>
<tr>
2015-09-27 22:06:15 +10:00
<td>Unix Socket or Windows Socket</td>
2014-02-28 11:04:14 +11:00
<td>Stable</td>
2015-09-27 22:06:15 +10:00
<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>
2014-02-28 08:32:34 +11:00
</td>
</tr>
<tr>
<td>TCP Socket</td>
2014-02-28 11:04:14 +11:00
<td>Stable</td>
2015-09-27 22:06:15 +10:00
<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>
2014-02-28 08:32:34 +11:00
</td>
</tr>
<tr>
<td>TLS Socket</td>
2014-02-28 11:04:14 +11:00
<td>Alpha</td>
2014-02-28 08:32:34 +11:00
<td><strong><em>coming soon...</em></strong></td>
</tr>
<tr>
<td>UDP Sockets</td>
2014-02-28 11:04:14 +11:00
<td>Stable</td>
2015-09-27 22:06:15 +10:00
<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>
2014-02-28 08:32:34 +11:00
</td>
</tr>
</tbody>
2014-10-28 13:26:05 +11:00
</table>
<table>
<thead>
<tr>
2014-02-28 11:04:14 +11:00
<th>OS</th>
<th>Supported Sockets</th>
2014-10-28 13:26:05 +11:00
</tr>
</thead>
2014-02-28 08:32:34 +11:00
<tbody>
<tr>
2014-02-28 11:04:14 +11:00
<td>Linux</td>
2015-09-27 22:06:15 +10:00
<td>Unix, Posix, TCP, TLS, UDP</td>
2014-02-28 08:32:34 +11:00
</tr>
<tr>
2014-02-28 11:04:14 +11:00
<td>Mac</td>
2015-09-27 22:06:15 +10:00
<td>Unix, Posix, TCP, TLS, UDP</td>
2014-02-28 08:32:34 +11:00
</tr>
<tr>
2014-02-28 11:04:14 +11:00
<td>Win</td>
2015-09-27 22:06:15 +10:00
<td>Windows, TCP, TLS, UDP</td>
2014-02-28 08:32:34 +11:00
</tr>
</tbody>
2014-10-28 13:26:05 +11:00
</table>
2014-02-28 11:04:14 +11:00
2014-10-28 13:26:05 +11:00
<hr>
<p><code>ipc.config</code> </p>
2014-03-02 11:42:20 +11:00
<p>Set these variables in the <code>ipc.config</code> scope to overwrite or set default values.</p>
<pre><code>{
appspace : 'app.',
socketRoot : '/tmp/',
id : os.hostname(),
2015-09-27 22:06:15 +10:00
networkHost : 'localhost', //should resolve to 127.0.0.1 or ::1 see the table below related to this
2014-03-02 11:42:20 +11:00
networkPort : 8000,
encoding : 'utf8',
2015-09-27 22:06:15 +10:00
rawBuffer : false,
2014-03-02 11:42:20 +11:00
silent : false,
maxConnections : 100,
retry : 500,
2014-09-02 04:18:39 +10:00
maxRetries : false,
2014-03-02 11:42:20 +11:00
stopRetrying : false
}
</code></pre>
<table>
2014-10-28 13:26:05 +11:00
<thead>
<tr>
2014-03-02 11:42:20 +11:00
<th>variable</th>
<th>documentation</th>
2014-10-28 13:26:05 +11:00
</tr>
</thead>
2014-03-02 11:42:20 +11:00
<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>
2015-09-27 22:06:15 +10:00
<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>
2014-03-02 11:42:20 +11:00
</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>
</tr>
<tr>
2014-08-28 14:15:30 +10:00
<td>maxRetries</td>
2014-09-02 04:18:39 +10:00
<td>if set, it represents the maximum number of retries after each disconnect before giving up and completely killing a specific connection</td>
2014-08-28 14:15:30 +10:00
</tr>
<tr>
2014-03-02 11:42:20 +11:00
<td>stopRetrying</td>
2015-09-27 22:06:15 +10:00
<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>
2014-03-02 11:42:20 +11:00
</tr>
</tbody>
2014-10-28 13:26:05 +11:00
</table>
<hr>
<h4>
2015-04-16 14:39:03 +10:00
<a id="ipc-methods" class="anchor" href="#ipc-methods" aria-hidden="true"><span class="octicon octicon-link"></span></a>IPC Methods</h4>
2014-02-28 08:32:34 +11:00
<p>These methods are available in the IPC Scope. </p>
2014-10-28 13:26:05 +11:00
<hr>
<h5>
2015-04-16 14:39:03 +10:00
<a id="log" class="anchor" href="#log" aria-hidden="true"><span class="octicon octicon-link"></span></a>log</h5>
2014-02-28 08:32:34 +11:00
<p><code>ipc.log(a,b,c,d,e...);</code> </p>
2015-09-27 22:06:15 +10:00
<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>
2014-02-28 08:32:34 +11:00
2015-09-27 22:06:15 +10:00
<p>The log also supports <a href="https://github.com/Marak/colors.js">colors</a> implementation. All of the available styles are supported and the theme styles are as follows :</p>
2014-02-28 08:32:34 +11:00
<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>
<pre><code>var colors=require('colors');
colors.setTheme(
{
good : 'zebra',
notice : 'redBG',
...
}
);
</code></pre>
2014-10-28 13:26:05 +11:00
<hr>
<h5>
2015-04-16 14:39:03 +10:00
<a id="connectto" class="anchor" href="#connectto" aria-hidden="true"><span class="octicon octicon-link"></span></a>connectTo</h5>
2014-02-28 08:32:34 +11:00
<p><code>ipc.connectTo(id,path,callback);</code> </p>
2015-09-27 22:06:15 +10:00
<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>
2014-02-28 08:32:34 +11:00
2014-02-28 11:04:14 +11:00
<table>
2014-10-28 13:26:05 +11:00
<thead>
<tr>
2014-02-28 11:04:14 +11:00
<th>variable</th>
<th>required</th>
<th>definition</th>
2014-10-28 13:26:05 +11:00
</tr>
</thead>
2014-02-28 11:04:14 +11:00
<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>
2015-09-27 22:06:15 +10:00
<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>
2014-02-28 11:04:14 +11:00
</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>
2014-10-28 13:26:05 +11:00
</table>
2015-09-27 22:06:15 +10:00
<p><strong>examples</strong> arguments can be ommitted so long as they are still in order.</p>
2014-02-28 08:32:34 +11:00
<pre><code>ipc.connectTo('world');
</code></pre>
<p>or using just an id and a callback</p>
<pre><code>ipc.connectTo(
'world',
function(){
ipc.of.world.on(
'hello',
function(data){
2015-09-27 22:06:15 +10:00
ipc.log(data.debug);
2014-02-28 08:32:34 +11:00
//if data was a string, it would have the color set to the debug style applied to it
}
)
}
);
</code></pre>
<p>or explicitly setting the path</p>
<pre><code>ipc.connectTo(
'world',
'myapp.world'
);
</code></pre>
<p>or explicitly setting the path with callback</p>
<pre><code>ipc.connectTo(
'world',
'myapp.world',
function(){
...
}
);
</code></pre>
2014-10-28 13:26:05 +11:00
<hr>
<h5>
2015-04-16 14:39:03 +10:00
<a id="connecttonet" class="anchor" href="#connecttonet" aria-hidden="true"><span class="octicon octicon-link"></span></a>connectToNet</h5>
2014-02-28 08:32:34 +11:00
<p><code>ipc.connectToNet(id,host,port,callback)</code> </p>
2015-09-27 22:06:15 +10:00
<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>
2014-02-28 08:32:34 +11:00
2014-02-28 11:04:14 +11:00
<table>
2014-10-28 13:26:05 +11:00
<thead>
<tr>
2014-02-28 11:04:14 +11:00
<th>variable</th>
<th>required</th>
<th>definition</th>
2014-10-28 13:26:05 +11:00
</tr>
</thead>
2014-02-28 11:04:14 +11:00
<tbody>
<tr>
<td>id</td>
<td>required</td>
2015-09-27 22:06:15 +10:00
<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>
2014-02-28 11:04:14 +11:00
</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>
2014-10-28 13:26:05 +11:00
</table>
2015-09-27 22:06:15 +10:00
<p><strong>examples</strong> arguments can be ommitted so long as they are still in order.<br>
2014-02-28 08:32:34 +11:00
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>
<pre><code>ipc.connectToNet(
'world',
3435,
function(){
...
}
);
</code></pre>
2014-10-28 13:26:05 +11:00
<hr>
<h5>
2015-04-16 14:39:03 +10:00
<a id="disconnect" class="anchor" href="#disconnect" aria-hidden="true"><span class="octicon octicon-link"></span></a>disconnect</h5>
2014-03-02 04:58:52 +11:00
<p><code>ipc.disconnect(id)</code> </p>
2015-09-27 22:06:15 +10:00
<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>
2014-03-02 04:58:52 +11:00
<table>
2014-10-28 13:26:05 +11:00
<thead>
<tr>
2014-03-02 04:58:52 +11:00
<th>variable</th>
<th>required</th>
<th>definition</th>
2014-10-28 13:26:05 +11:00
</tr>
</thead>
<tbody>
<tr>
2014-03-02 04:58:52 +11:00
<td>id</td>
<td>required</td>
<td>is the string id of the socket from which to disconnect.</td>
2014-10-28 13:26:05 +11:00
</tr>
</tbody>
</table>
<p><strong>examples</strong></p>
2014-03-02 04:58:52 +11:00
<pre><code>ipc.disconnect('world');
</code></pre>
2014-10-28 13:26:05 +11:00
<hr>
<h5>
2015-04-16 14:39:03 +10:00
<a id="serve" class="anchor" href="#serve" aria-hidden="true"><span class="octicon octicon-link"></span></a>serve</h5>
2014-02-28 08:32:34 +11:00
<p><code>ipc.serve(path,callback);</code> </p>
2015-09-27 22:06:15 +10:00
<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>
2014-02-28 08:32:34 +11:00
2014-02-28 11:04:14 +11:00
<table>
2014-10-28 13:26:05 +11:00
<thead>
<tr>
2014-02-28 11:04:14 +11:00
<th>variable</th>
<th>required</th>
<th>definition</th>
2014-10-28 13:26:05 +11:00
</tr>
</thead>
2014-02-28 11:04:14 +11:00
<tbody>
<tr>
<td>path</td>
<td>optional</td>
2015-09-27 22:06:15 +10:00
<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>
2014-02-28 11:04:14 +11:00
</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>
2014-10-28 13:26:05 +11:00
</table>
2015-09-27 22:06:15 +10:00
<p><strong><em>examples</em></strong> arguments can be omitted so long as they are still in order.</p>
2014-02-28 08:32:34 +11:00
<pre><code>ipc.serve();
</code></pre>
<p>or specifying callback</p>
<pre><code>ipc.serve(
function(){...}
);
</code></pre>
<p>or specify path</p>
<pre><code>ipc.serve(
'/tmp/myapp.myservice'
);
</code></pre>
<p>or specifying everything</p>
<pre><code>ipc.serve(
'/tmp/myapp.myservice',
function(){...}
);
</code></pre>
2014-10-28 13:26:05 +11:00
<hr>
<h5>
2015-04-16 14:39:03 +10:00
<a id="servenet" class="anchor" href="#servenet" aria-hidden="true"><span class="octicon octicon-link"></span></a>serveNet</h5>
2014-02-28 08:32:34 +11:00
2014-02-28 11:04:14 +11:00
<p><code>serveNet(host,port,UDPType,callback)</code></p>
2015-09-27 22:06:15 +10:00
<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>
2014-02-28 11:04:14 +11:00
<table>
2014-10-28 13:26:05 +11:00
<thead>
<tr>
2014-02-28 11:04:14 +11:00
<th>variable</th>
<th>required</th>
<th>definition</th>
2014-10-28 13:26:05 +11:00
</tr>
</thead>
2014-02-28 11:04:14 +11:00
<tbody>
<tr>
<td>host</td>
<td>optional</td>
2015-09-27 22:06:15 +10:00
<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>
2014-02-28 11:04:14 +11:00
</tr>
<tr>
<td>port</td>
<td>optional</td>
2015-09-27 22:06:15 +10:00
<td>The port on wunich the TCP, UDP, or TLS Socket server will be bound, this defaults to 8000 if not specified</td>
2014-02-28 11:04:14 +11:00
</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>
2014-10-28 13:26:05 +11:00
</table>
<p><strong><em>examples</em></strong> arguments can be ommitted solong as they are still in order.</p>
2014-02-28 11:04:14 +11:00
<p>default tcp server</p>
<pre><code>ipc.serveNet();
</code></pre>
<p>default udp server</p>
<pre><code>ipc.serveNet('udp4');
</code></pre>
<p>or specifying TCP server with callback</p>
<pre><code>ipc.serveNet(
function(){...}
);
</code></pre>
<p>or specifying UDP server with callback</p>
<pre><code>ipc.serveNet(
'udp4',
function(){...}
);
</code></pre>
<p>or specify port</p>
<pre><code>ipc.serveNet(
3435
);
</code></pre>
<p>or specifying everything TCP</p>
<pre><code>ipc.serveNet(
'MyMostAwesomeApp.com',
3435,
function(){...}
);
</code></pre>
<p>or specifying everything UDP</p>
<pre><code>ipc.serveNet(
'MyMostAwesomeApp.com',
3435,
'udp4',
function(){...}
);
</code></pre>
2014-02-28 08:32:34 +11:00
2014-10-28 13:26:05 +11:00
<hr>
<h3>
2015-04-16 14:39:03 +10:00
<a id="ipc-stores-and-default-variables" class="anchor" href="#ipc-stores-and-default-variables" aria-hidden="true"><span class="octicon octicon-link"></span></a>IPC Stores and Default Variables</h3>
2014-02-28 11:04:14 +11:00
2014-08-28 14:15:30 +10:00
<table>
2014-10-28 13:26:05 +11:00
<thead>
<tr>
2014-08-28 14:15:30 +10:00
<th>variable</th>
<th>definition</th>
2014-10-28 13:26:05 +11:00
</tr>
</thead>
2014-08-28 14:15:30 +10:00
<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>
2014-10-28 13:26:05 +11:00
</table>
<hr>
<h3>
2015-04-16 14:39:03 +10:00
<a id="basic-examples" class="anchor" href="#basic-examples" aria-hidden="true"><span class="octicon octicon-link"></span></a>Basic Examples</h3>
2014-02-28 08:32:34 +11:00
2014-08-28 14:15:30 +10:00
<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>
2014-02-28 08:32:34 +11:00
<h4>
2015-09-27 22:06:15 +10:00
<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 class="octicon octicon-link"></span></a>Server for Unix Sockets, Windows Sockets &amp; TCP Sockets</h4>
2014-02-28 08:32:34 +11:00
2015-09-27 22:06:15 +10:00
<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>
2014-02-28 08:32:34 +11:00
<pre><code>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);
2014-02-28 11:04:14 +11:00
ipc.server.emit(
2015-09-27 22:06:15 +10:00
socket,
2014-02-28 08:32:34 +11:00
'message',
data+' world!'
);
}
);
}
);
ipc.server.start();
</code></pre>
<h4>
2015-04-16 14:39:03 +10:00
<a id="client-for-unix-sockets--tcp-sockets" class="anchor" href="#client-for-unix-sockets--tcp-sockets" aria-hidden="true"><span class="octicon octicon-link"></span></a>Client for Unix Sockets &amp; TCP Sockets</h4>
2014-02-28 08:32:34 +11:00
2015-09-27 22:06:15 +10:00
<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>
2014-02-28 08:32:34 +11:00
2015-09-27 22:06:15 +10:00
<pre><code>var ipc=require('node-ipc');
2014-02-28 08:32:34 +11:00
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>
2015-04-16 14:39:03 +10:00
<a id="server--client-for-udp-sockets" class="anchor" href="#server--client-for-udp-sockets" aria-hidden="true"><span class="octicon octicon-link"></span></a>Server &amp; Client for UDP Sockets</h4>
2014-02-28 08:32:34 +11:00
2015-09-27 22:06:15 +10:00
<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>
2014-02-28 08:32:34 +11:00
2015-09-27 22:06:15 +10:00
<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>
2014-02-28 08:32:34 +11:00
2015-09-27 22:06:15 +10:00
<p>This is the most basic example which will work for both local and remote UDP Sockets.</p>
2014-02-28 08:32:34 +11:00
<h5>
2015-04-16 14:39:03 +10:00
<a id="udp-server-1---world" class="anchor" href="#udp-server-1---world" aria-hidden="true"><span class="octicon octicon-link"></span></a>UDP Server 1 - "World"</h5>
2014-02-28 08:32:34 +11:00
<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>
2015-04-16 14:39:03 +10:00
<a id="udp-server-2---hello" class="anchor" href="#udp-server-2---hello" aria-hidden="true"><span class="octicon octicon-link"></span></a>UDP Server 2 - "Hello"</h5>
2014-02-28 08:32:34 +11:00
<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(
{
2015-09-27 22:06:15 +10:00
address : '127.0.0.1', //any hostname will work
2014-02-28 08:32:34 +11:00
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();
2015-09-27 22:06:15 +10:00
</code></pre>
<h4>
<a id="raw-buffer-or-binary-sockets" class="anchor" href="#raw-buffer-or-binary-sockets" aria-hidden="true"><span 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.</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>
<pre><code>ipc.config.encoding='utf8';
</code></pre>
<p>emit string buffer :</p>
<pre><code>//server
ipc.server.emit(
socket,
'hello'
);
//client
ipc.of.world.emit(
'hello'
)
</code></pre>
<p>emit byte array buffer :</p>
<pre><code>//server
ipc.server.emit(
socket,
[10,20,30]
);
//client
ipc.server.emit(
[10,20,30]
);
</code></pre>
<p>emit hex array buffer :</p>
<pre><code>//server
ipc.server.emit(
socket,
[0x05,0x6d,0x5c]
);
//client
ipc.server.emit(
[0x05,0x6d,0x5c]
);
2014-02-28 08:32:34 +11:00
</code></pre>
</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>
2015-04-16 14:39:03 +10:00
<p>Published with <a href="https://pages.github.com">GitHub Pages</a></p>
2014-02-28 08:32:34 +11:00
</footer>
</div>
2014-02-28 11:04:14 +11:00
<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>
2014-02-28 08:32:34 +11:00
</body>
</html>