class IPC{ constructor(){ Object.defineProperties( this, { config : { enumerable:true, writable:true, value:new Defaults }, connectTo : { enumerable:true, writable:false, value:connect }, connectToNet: { enumerable:true, writable:false, value:connectNet }, disconnect : { enumerable:true, writable:false, value:disconnect }, serve : { enumerable:true, writable:false, value:serve }, serveNet : { enumerable:true, writable:false, value:serveNet }, of : { enumerable:true, writable:true, value:{} }, server : { enumerable:true, writable:true, configurable:true, value:false }, log : { enumerable:true, writable:false, value:log } } ); } }
n/a
function connect(id, path, callback){ if(typeof path == 'function'){ callback=path; path=false; } if(!callback){ callback=emptyCallback; } if(!id){ this.log( 'Service id required'.warn, 'Requested service connection without specifying service id. Aborting connection attempt'.notice ); return; } if(!path){ this.log( 'Service path not specified, so defaulting to'.notice, 'ipc.config.socketRoot + ipc.config.appspace + id'.variable, (this.config.socketRoot+this.config.appspace+id).data ); path=this.config.socketRoot+this.config.appspace+id; } if(this.of[id]){ if(!this.of[id].socket.destroyed){ this.log( 'Already Connected to'.notice, id.variable, '- So executing success without connection'.notice ); callback(); return; } this.of[id].socket.destroy(); } this.of[id] = new Client(this.config,this.log); this.of[id].id = id; this.of[id].path = path; this.of[id].connect(); callback(this); }
...
ipc.config.logDepth=5; //default
```
----
##### connectTo
`ipc.connectTo(id,path,callback);`
Used for connecting as a client to local Unix Sockets and Windows Sockets. ***This is the fastest way for processes on the same
machine to communicate*** because it bypasses the network card which TCP and UDP must both use.
| 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 ` |
...
function connectNet(id, host, port, callback){ if(!id){ this.log( 'Service id required'.warn, 'Requested service connection without specifying service id. Aborting connection attempt'.notice ); return; } if(typeof host=='number'){ callback=port; port=host; host=false; } if(typeof host=='function'){ callback=host; host=false; port=false; } if(!host){ this.log( 'Server host not specified, so defaulting to'.notice, 'ipc.config.networkHost'.variable, this.config.networkHost.data ); host=this.config.networkHost; } if(typeof port=='function'){ callback=port; port=false; } if(!port){ this.log( 'Server port not specified, so defaulting to'.notice, 'ipc.config.networkPort'.variable, this.config.networkPort ); port=this.config.networkPort; } if(typeof callback == 'string'){ UDPType=callback; callback=false; } if(!callback){ callback=emptyCallback; } if(this.of[id]){ if(!this.of[id].socket.destroyed){ this.log( 'Already Connected to'.notice, id.variable, '- So executing success without connection'.notice ); callback(); return; } this.of[id].socket.destroy(); } this.of[id] = new Client(this.config,this.log); this.of[id].id = id; this.of[id].path = host; this.of[id].port = port; this.of[id].connect(); callback(this); }
...
);
```
----
##### connectToNet
`ipc.connectToNet(id,host,port,callback)`
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.
| variable | required | definition |
|----------|----------|------------|
...
function disconnect(id){ if(!this.of[id]){ return; } this.of[id].explicitlyDisconnected=true; this.of[id].off('*','*'); if(this.of[id].socket){ if(this.of[id].socket.destroy){ this.of[id].socket.destroy(); } } delete this.of[id]; }
...
);
```
----
##### disconnect
`ipc.disconnect(id)`
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.
| variable | required | definition |
|----------|----------|------------|
| id | required | is the string id of the socket from which to disconnect. |
...
function log(){ if(this.config.silent){ return; } const args=Array.prototype.slice.call(arguments); for(let i=0, count=args.length; i<count; i++){ if(typeof args[i] != 'object'){ continue; } args[i]=util.inspect( args[i], { depth:this.config.logDepth, colors:this.config.logInColor } ); } console.log( args.join(' ') ); }
...
#### 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 single space
x27; ' 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 ` true `. That way if you leave your logging in place it should have almost no effect on performance
.
The log also uses util.inspect You can control if it should log in color as well as the log depth via ` ipc.config `
```javascript
...
function serve(path, callback){ if(typeof path=='function'){ callback=path; path=false; } if(!path){ this.log( 'Server path not specified, so defaulting to'.notice, 'ipc.config.socketRoot + ipc.config.appspace + ipc.config.id'.variable, (this.config.socketRoot+this.config.appspace+this.config.id).data ); path=this.config.socketRoot+this.config.appspace+this.config.id; } if(!callback){ callback=emptyCallback; } this.server=new Server( path, this.config, log ); this.server.on( 'start', callback ); }
...
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.
| 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(){});` |
...
function serveNet(host, port, UDPType, callback){ if(typeof host=='number'){ callback=UDPType; UDPType=port; port=host; host=false; } if(typeof host=='function'){ callback=host; UDPType=false; host=false; port=false; } if(!host){ this.log( 'Server host not specified, so defaulting to'.notice, 'ipc.config.networkHost'.variable, this.config.networkHost.data ); host=this.config.networkHost; } if(host.toLowerCase()=='udp4' || host.toLowerCase()=='udp6'){ callback=port; UDPType=host.toLowerCase(); port=false; host=this.config.networkHost; } if(typeof port=='string'){ callback=UDPType; UDPType=port; port=false; } if(typeof port=='function'){ callback=port; UDPType=false; port=false; } if(!port){ this.log( 'Server port not specified, so defaulting to'.notice, 'ipc.config.networkPort'.variable, this.config.networkPort ); port=this.config.networkPort; } if(typeof UDPType=='function'){ callback=UDPType; UDPType=false; } if(!callback){ callback=emptyCallback; } this.server=new Server( host, this.config, log, port ); if(UDPType){ this.server[UDPType]=true; if(UDPType === "udp4" && host === "::1") { // bind udp4 socket to an ipv4 address this.server.path = "127.0.0.1"; } } this.server.on( 'start', callback ); }
...
***examples*** arguments can be ommitted solong as they are still in order.
default tcp server
```javascript
ipc.serveNet();
```
default udp server
```javascript
...