node-ipc/README.md

669 lines
22 KiB
Markdown
Raw Permalink Normal View History

2014-10-28 12:05:03 +11:00
node-ipc
================
2015-09-27 21:53:08 +10:00
*a nodejs module for local and remote Inter Process Communication* with full support for Linux, Mac and Windows.
2014-03-01 07:28:35 +11:00
A great solution for **Neural Networking** in Node.JS
2014-02-22 20:13:31 +11:00
**npm install node-ipc**
2015-08-27 06:33:55 +10:00
2015-09-21 06:19:43 +10:00
npm info : [See npm trends and stats for node-ipc](http://npm-stat.com/charts.html?package=node-ipc&author=&from=&to=)
![node-ipc npm version](https://img.shields.io/npm/v/node-ipc.svg) ![supported node version for node-ipc](https://img.shields.io/node/v/node-ipc.svg) ![total npm downloads for node-ipc](https://img.shields.io/npm/dt/node-ipc.svg) ![monthly npm downloads for node-ipc](https://img.shields.io/npm/dm/node-ipc.svg) ![npm licence for node-ipc](https://img.shields.io/npm/l/node-ipc.svg)
2014-02-28 11:00:20 +11:00
2015-09-27 20:36:01 +10:00
[![RIAEvangelist](https://avatars3.githubusercontent.com/u/369041?v=3&s=100)](https://github.com/RIAEvangelist)
2015-09-21 06:19:43 +10:00
GitHub info :
![node-ipc GitHub Release](https://img.shields.io/github/release/RIAEvangelist/node-ipc.svg) ![GitHub license node-ipc license](https://img.shields.io/github/license/RIAEvangelist/node-ipc.svg) ![open issues for node-ipc on GitHub](https://img.shields.io/github/issues/RIAEvangelist/node-ipc.svg)
2015-08-27 08:02:42 +10:00
2014-02-28 11:00:20 +11:00
Package details websites :
* [GitHub.io site](http://riaevangelist.github.io/node-ipc/ "node-ipc documentation"). A prettier version of this site.
* [NPM Module](https://www.npmjs.org/package/node-ipc "node-ipc npm module"). The npm page for the node-ipc module.
2015-09-21 06:19:43 +10:00
This work is licenced via the [DBAD Public Licence](http://www.dbad-license.org/).
2014-06-13 16:25:04 +10:00
----
#### Contents
1. [Types of IPC Sockets and Supporting OS](#types-of-ipc-sockets)
2. [IPC Methods](#ipc-methods)
1. [log](#log)
2. [connectTo](#connectto)
3. [connectToNet](#connecttonet)
4. [disconnect](#disconnect)
5. [serve](#serve)
6. [serveNet](#servenet)
3. [IPC Stores and Default Variables](#ipc-stores-and-default-variables)
4. [Basic Examples](#basic-examples)
1. [Server for Unix||Windows Sockets & TCP Sockets](#server-for-unix-sockets--tcp-sockets)
2. [Client for Unix||Windows Sockets & TCP Sockets](#client-for-unix-sockets--tcp-sockets)
2015-09-28 15:59:25 +10:00
4. [Server & Client for UDP Sockets](#server--client-for-udp-sockets)
5. [Raw Buffers or Binary Sockets](#raw-buffer-or-binary-sockets)
2015-09-28 16:11:04 +10:00
5. [Working with TLS/SSL Socket Servers & Clients](https://github.com/RIAEvangelist/node-ipc/tree/master/example/TLSSocket)
6. [Advanced Examples](https://github.com/RIAEvangelist/node-ipc/tree/master/example)
2014-02-22 20:13:31 +11:00
2014-02-22 20:22:04 +11:00
----
#### Types of IPC Sockets
2014-02-22 20:13:31 +11:00
2014-02-28 11:00:20 +11:00
| Type | Stability |Definition |
|-----------|-----------|-----------|
|Unix Socket or Windows Socket| Stable | Gives Linux, Mac, and Windows lightning fast communication and avoids the network card to reduce overhead and latency. [Local Unix and Windows Socket examples ](https://github.com/RIAEvangelist/node-ipc/tree/master/example/unixWindowsSocket/ "Unix and Windows Socket Node IPC examples") |
|TCP Socket | Stable | Gives the most reliable communication across the network. Can be used for local IPC as well, but is slower than #1's Unix Socket Implementation because TCP sockets go through the network card while Unix Sockets and Windows Sockets do not. [Local or remote network TCP Socket examples ](https://github.com/RIAEvangelist/node-ipc/tree/master/example/TCPSocket/ "TCP Socket Node IPC examples") |
|TLS Socket | Stable | Configurable and secure network socket over SSL. Equivalent to https. [TLS/SSL documentation](https://github.com/RIAEvangelist/node-ipc/tree/master/example/TLSSocket) |
|UDP Sockets| Stable | Gives the **fastest network communication**. UDP is less reliable but much faster than TCP. It is best used for streaming non critical data like sound, video, or multiplayer game data as it can drop packets depending on network connectivity and other factors. UDP can be used for local IPC as well, but is slower than #1's Unix Socket or Windows Socket Implementation because UDP sockets go through the network card while Unix and Windows Sockets do not. [Local or remote network UDP Socket examples ](https://github.com/RIAEvangelist/node-ipc/tree/master/example/UDPSocket/ "UDP Socket Node IPC examples") |
2014-02-28 08:41:33 +11:00
| OS | Supported Sockets |
|-----|--------------------|
2015-09-03 16:24:23 +10:00
|Linux| Unix, Posix, TCP, TLS, UDP|
|Mac | Unix, Posix, TCP, TLS, UDP|
|Win | Windows, TCP, TLS, UDP |
----
`ipc.config`
2014-03-02 11:41:27 +11:00
Set these variables in the `ipc.config` scope to overwrite or set default values.
2014-03-02 11:41:27 +11:00
```javascript
2014-03-02 11:41:27 +11:00
{
appspace : 'app.',
socketRoot : '/tmp/',
id : os.hostname(),
networkHost : 'localhost', //should resolve to 127.0.0.1 or ::1 see the table below related to this
2014-03-02 11:41:27 +11:00
networkPort : 8000,
encoding : 'utf8',
2015-08-23 15:46:55 +10:00
rawBuffer : false,
2014-03-02 11:41:27 +11:00
silent : false,
maxConnections : 100,
retry : 500,
2014-09-01 04:05:06 +10:00
maxRetries : false,
2014-03-02 11:41:27 +11:00
stopRetrying : false
}
```
2014-03-02 11:41:27 +11:00
| variable | documentation |
|----------|---------------|
| appspace | used for Unix Socket (Unix Domain Socket) namespacing. If not set specifically, the Unix Domain Socket will combine the socketRoot, appspace, and id to form the Unix Socket Path for creation or binding. This is available incase you have many apps running on your system, you may have several sockets with the same id, but if you change the appspace, you will still have app specic unique sockets.|
| socketRoot| the directory in which to create or bind to a Unix Socket |
| id | the id of this socket or service |
| networkHost| the local or remote host on which TCP, TLS or UDP Sockets should connect |
| networkPort| the default port on which TCP, TLS, or UDP sockets should connect |
2015-08-23 16:51:04 +10:00
| encoding | the default encoding for data sent on sockets. Mostly used if rawBuffer is set to true. Valid values are : ` ascii` ` utf8 ` ` utf16le` ` ucs2` ` base64` ` hex ` .
| rawBuffer| if true, data will be sent and received as a raw node ` Buffer ` __NOT__ an ` Object ` as JSON. This is great for Binary or hex IPC, and communicating with other processes in languages like C and C++ |
2014-03-02 11:41:27 +11:00
| silent | turn on/off logging default is false which means logging is on |
| maxConnections| this is the max number of connections allowed to a socket. It is currently only being set on Unix Sockets. Other Socket types are using the system defaults. |
| retry | this is the time in milliseconds a client will wait before trying to reconnect to a server if the connection is lost. This does not effect UDP sockets since they do not have a client server relationship like Unix Sockets and TCP Sockets. |
2014-09-01 04:05:06 +10:00
| maxRetries | if set, it represents the maximum number of retries after each disconnect before giving up and completely killing a specific connection |
| stopRetrying| Defaults to false meaning clients will continue to retry to connect to servers indefinitely at the retry interval. If set to any number the client will stop retrying when that number is exceeded after each disconnect. If set to true in real time it will immediately stop trying to connect regardless of maxRetries. If set to 0, the client will ***NOT*** try to reconnect. |
2014-03-02 11:41:27 +11:00
----
#### IPC Methods
These methods are available in the IPC Scope.
----
##### log
`ipc.log(a,b,c,d,e...);`
ipc.log will accept any number of arguments and if `ipc.config.silent` is not set, it will concat them all with a sincle space ' ' between them and then log them to the console. This is fast because it prevents any concatenation from happening if the ipc is set to silent. That way if you leave your logging in place it should not effect performance.
The log also supports [colors](https://github.com/Marak/colors.js) implementation. All of the available styles are supported and the theme styles are as follows :
```javascript
{
good : 'green',
notice : 'yellow',
warn : 'red',
error : 'redBG',
debug : 'magenta',
variable: 'cyan',
data : 'blue'
}
```
You can override any of these settings by requireing colors and setting the theme as follows :
```javascript
var colors=require('colors');
2015-09-21 06:19:43 +10:00
colors.setTheme(
{
good : 'zebra',
notice : 'redBG',
...
}
);
```
----
##### connectTo
`ipc.connectTo(id,path,callback);`
Used for connecting as a client to local Unix Sockets and Windows Sockets. ***This is the fastst way for processes on the same machine to communicate*** because it bypasses the network card which TCP and UDP must both use.
2014-02-28 10:45:47 +11:00
| variable | required | definition |
|----------|----------|------------|
| id | required | is the string id of the socket being connected to. The socket with this id is added to the ipc.of object when created. |
| path | optional | is the path of the Unix Domain Socket File, if 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 ` ipc.config.socketRoot `+` ipc.config.appspace `+` id ` |
2014-02-28 10:45:47 +11:00
| callback | optional | this is the function to execute when the socket has been created. |
2014-02-28 06:34:49 +11:00
**examples** arguments can be ommitted so long as they are still in order.
```javascript
ipc.connectTo('world');
2015-09-21 06:19:43 +10:00
```
or using just an id and a callback
2015-09-21 06:19:43 +10:00
```javascript
ipc.connectTo(
'world',
function(){
ipc.of.world.on(
'hello',
function(data){
2015-09-21 06:19:43 +10:00
ipc.log(data.debug);
//if data was a string, it would have the color set to the debug style applied to it
}
)
}
);
```
or explicitly setting the path
```javascript
ipc.connectTo(
'world',
'myapp.world'
);
2015-09-21 06:19:43 +10:00
```
or explicitly setting the path with callback
```javascript
ipc.connectTo(
'world',
'myapp.world',
function(){
...
}
);
```
----
##### connectToNet
`ipc.connectToNet(id,host,port,callback)`
2015-09-28 15:59:25 +10:00
Used to connect as a client to a TCP or [TLS socket](https://github.com/RIAEvangelist/node-ipc/tree/master/example/TLSSocket) 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 `connectTo` instead as it is much faster since it avoids the network card altogether.
For TLS and SSL Sockets see the [node-ipc TLS and SSL docs](https://github.com/RIAEvangelist/node-ipc/tree/master/example/TLSSocket). They have a few additional requirements, and things to know about and so have their own doc.
2014-02-28 10:45:47 +11:00
| variable | required | definition |
|----------|----------|------------|
| id | required | is the string id of the socket being connected to. For TCP & TLS sockets, this id is added to the `ipc.of` object when the socket is created with a reference to the socket. |
| host | optional | is the host on which the TCP or TLS socket resides. This will default to `ipc.config.networkHost` if not specified. |
2014-02-28 10:45:47 +11:00
| port | optional | the port on which the TCP or TLS socket resides. |
| callback | optional | this is the function to execute when the socket has been created. |
**examples** arguments can be ommitted so long as they are still in order.
So while the default is : (id,host,port,callback), the following examples will still work because they are still in order (id,port,callback) or (id,host,callback) or (id,port) etc.
```javascript
ipc.connectToNet('world');
2015-09-21 06:19:43 +10:00
```
or using just an id and a callback
2015-09-21 06:19:43 +10:00
```javascript
ipc.connectToNet(
'world',
function(){
...
}
);
```
or explicitly setting the host and path
```javascript
ipc.connectToNet(
'world',
'myapp.com',serve(path,callback)
3435
);
2015-09-21 06:19:43 +10:00
```
or only explicitly setting port and callback
```javascript
ipc.connectToNet(
'world',
3435,
function(){
...
}
);
```
2014-03-02 04:54:32 +11:00
----
##### disconnect
`ipc.disconnect(id)`
2014-03-02 04:54:32 +11:00
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 `ipc.of` scope. This can be local or remote. UDP clients do not maintain connections and so there are no Clients and this method has no value to them.
2014-03-02 04:54:32 +11:00
| variable | required | definition |
|----------|----------|------------|
| id | required | is the string id of the socket from which to disconnect. |
**examples**
```javascript
2014-03-02 04:54:32 +11:00
ipc.disconnect('world');
```
----
##### serve
`ipc.serve(path,callback);`
Used to create local Unix Socket Server or Windows Socket Server to which Clients can bind. The server can `emit` events to specific Client Sockets, or `broadcast` events to all known Client Sockets.
2014-02-28 10:45:47 +11:00
| variable | required | definition |
|----------|----------|------------|
| path | optional | 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 ` ipc.config.socketRoot `+` ipc.config.appspace `+` id ` |
| callback | optional | This is a function to be called after the Server has started. This can also be done by binding an event to the start event like `ipc.server.on('start',function(){});` |
***examples*** arguments can be omitted so long as they are still in order.
```javascript
ipc.serve();
```
or specifying callback
```javascript
ipc.serve(
function(){...}
);
2015-09-21 06:19:43 +10:00
```
or specify path
```javascript
ipc.serve(
'/tmp/myapp.myservice'
);
2015-09-21 06:19:43 +10:00
```
or specifying everything
```javascript
ipc.serve(
'/tmp/myapp.myservice',
function(){...}
);
```
----
##### serveNet
2014-02-28 10:45:47 +11:00
`serveNet(host,port,UDPType,callback)`
2014-02-28 10:45:47 +11:00
Used to create TCP, TLS or UDP Socket Server to which Clients can bind or other servers can send data to. The server can `emit` events to specific Client Sockets, or `broadcast` events to all known Client Sockets.
2014-02-28 10:45:47 +11:00
| variable | required | definition |
|----------|----------|------------|
| host | optional | If not specified this defaults to the first address in os.networkInterfaces(). For TCP, TLS & UDP servers this is most likely going to be 127.0.0.1 or ::1 |
| port | optional | The port on wunich the TCP, UDP, or TLS Socket server will be bound, this defaults to 8000 if not specified |
2014-02-28 10:45:47 +11:00
| UDPType | optional | If set this will create the server as a UDP socket. 'udp4' or 'udp6' are valid values. This defaults to not being set.
| callback | optional | Function to be called when the server is created |
***examples*** arguments can be ommitted solong as they are still in order.
default tcp server
```javascript
2014-02-28 10:45:47 +11:00
ipc.serveNet();
2015-09-21 06:19:43 +10:00
```
2014-02-28 10:45:47 +11:00
default udp server
```javascript
2014-02-28 10:45:47 +11:00
ipc.serveNet('udp4');
```
2014-02-28 10:45:47 +11:00
or specifying TCP server with callback
```javascript
2014-02-28 10:45:47 +11:00
ipc.serveNet(
function(){...}
);
2015-09-21 06:19:43 +10:00
```
2014-02-28 10:45:47 +11:00
or specifying UDP server with callback
```javascript
2014-02-28 10:45:47 +11:00
ipc.serveNet(
'udp4',
function(){...}
);
2015-09-21 06:19:43 +10:00
```
2014-02-28 10:45:47 +11:00
or specify port
```javascript
2014-02-28 10:45:47 +11:00
ipc.serveNet(
3435
);
2015-09-21 06:19:43 +10:00
```
2014-02-28 10:45:47 +11:00
or specifying everything TCP
```javascript
2014-02-28 10:45:47 +11:00
ipc.serveNet(
'MyMostAwesomeApp.com',
3435,
function(){...}
);
```
2014-02-28 10:45:47 +11:00
or specifying everything UDP
```javascript
2014-02-28 10:45:47 +11:00
ipc.serveNet(
'MyMostAwesomeApp.com',
3435,
'udp4',
function(){...}
);
```
2014-02-28 10:45:47 +11:00
----
### IPC Stores and Default Variables
| variable | definition |
|-----------|------------|
| ipc.of | This is where socket connection refrences will be stored when connecting to them as a client via the `ipc.connectTo` or `iupc.connectToNet`. They will be stored based on the ID used to create them, eg : ipc.of.mySocket|
| ipc.server| This is a refrence to the server created by `ipc.serve` or `ipc.serveNet`|
2014-02-28 10:45:47 +11:00
----
2015-09-21 06:19:43 +10:00
### Basic Examples
You can find [Advanced Examples](https://github.com/RIAEvangelist/node-ipc/tree/master/example) in the examples folder. In the examples you will find more complex demos including multi client examples.
#### Server for Unix Sockets, Windows Sockets & TCP Sockets
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.
2014-02-22 20:22:04 +11:00
```javascript
2014-02-22 20:22:04 +11:00
var ipc=require('node-ipc');
ipc.config.id = 'world';
ipc.config.retry= 1500;
2015-09-21 06:19:43 +10:00
2014-02-22 20:22:04 +11:00
ipc.serve(
function(){
ipc.server.on(
'message',
function(data,socket){
ipc.log('got a message : '.debug, data);
ipc.server.emit(
socket,
2014-02-22 20:22:04 +11:00
'message',
data+' world!'
);
}
);
}
);
2015-09-21 06:19:43 +10:00
2014-02-22 20:22:04 +11:00
ipc.server.start();
```
2015-09-21 06:19:43 +10:00
#### Client for Unix Sockets & TCP Sockets
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.
2014-02-22 20:22:04 +11:00
```javascript
var ipc=require('node-ipc');
2014-02-22 20:22:04 +11:00
ipc.config.id = 'hello';
ipc.config.retry= 1500;
2015-09-21 06:19:43 +10:00
2014-02-22 20:22:04 +11:00
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);
}
);
}
);
2015-09-21 06:19:43 +10:00
```
2015-09-21 06:19:43 +10:00
#### Server & Client for UDP Sockets
UDP Sockets are different than Unix, Windows & TCP Sockets because they must be bound to a unique port on their machine to receive messages. For example, A TCP, Unix, or Windows Socket client could just connect to a separate TCP, Unix, or Windows Socket sever. That client could then exchange, both send and receive, data on the servers port or location. UDP Sockets can not do this. They must bind to a port to receive or send data.
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 `emit` or `broadcast` data the UDP server will need to know the host and port of the Socket it intends to broadcast the data to.
This is the most basic example which will work for both local and remote UDP Sockets.
2015-09-21 06:19:43 +10:00
##### UDP Server 1 - "World"
```javascript
var ipc=require('../../../node-ipc');
ipc.config.id = 'world';
ipc.config.retry= 1500;
2015-09-21 06:19:43 +10:00
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!'
}
);
}
);
2015-09-21 06:19:43 +10:00
console.log(ipc.server);
}
);
2015-09-21 06:19:43 +10:00
ipc.server.define.listen.message='This event type listens for message strings as value of data key.';
2015-09-21 06:19:43 +10:00
ipc.server.start();
2015-09-21 06:19:43 +10:00
```
##### UDP Server 2 - "Hello"
*note* we set the port here to 8001 because the world server is already using the default ipc.config.networkPort of 8000. So we can not bind to 8000 while world is using it.
```javascript
ipc.config.id = 'hello';
ipc.config.retry= 1500;
2015-09-21 06:19:43 +10:00
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 : '127.0.0.1', //any hostname will work
port : ipc.config.networkPort
},
'message',
{
from : ipc.config.id,
message : 'Hello'
}
);
}
);
2015-09-21 06:19:43 +10:00
ipc.server.define.listen.message='This event type listens for message strings as value of data key.';
2015-09-21 06:19:43 +10:00
ipc.server.start();
2015-08-23 16:51:04 +10:00
```
#### Raw Buffer or Binary Sockets
2015-08-23 16:51:04 +10:00
Binary or Buffer sockets can be used with any of the above socket types, however the way data events are emit is ***slightly*** different.
When setting up a rawBuffer socket you must specify it as such :
```javascript
2015-08-23 16:51:04 +10:00
ipc.config.rawBuffer=true;
```
2015-08-23 16:51:04 +10:00
You can also specify its encoding type. The default is ` utf8 `
```javascript
2015-08-23 16:51:04 +10:00
ipc.config.encoding='utf8';
```
2015-08-23 16:51:04 +10:00
emit string buffer :
```javascript
2015-08-23 16:51:04 +10:00
//server
ipc.server.emit(
socket,
'hello'
);
2015-09-21 06:19:43 +10:00
2015-08-23 16:51:04 +10:00
//client
ipc.of.world.emit(
'hello'
)
2015-09-21 06:19:43 +10:00
```
2015-08-23 16:51:04 +10:00
emit byte array buffer :
```javascript
2015-08-23 16:51:04 +10:00
//server
ipc.server.emit(
socket,
[10,20,30]
);
2015-09-21 06:19:43 +10:00
2015-08-23 16:51:04 +10:00
//client
ipc.server.emit(
[10,20,30]
);
2015-09-21 06:19:43 +10:00
```
2015-08-23 16:51:04 +10:00
emit hex array buffer :
2015-09-21 06:19:43 +10:00
```javascript
2015-08-23 16:51:04 +10:00
//server
ipc.server.emit(
socket,
[0x05,0x6d,0x5c]
);
2015-09-21 06:19:43 +10:00
2015-08-23 16:51:04 +10:00
//client
ipc.server.emit(
[0x05,0x6d,0x5c]
2015-09-03 16:24:23 +10:00
);
```
#### Licensed under DBAD license
See the [DBAD license](https://github.com/philsturgeon/dbad) in your language or our [licence.md](https://github.com/RIAEvangelist/node-phidget-API/blob/master/license.md) file.