ipc-evo
v1.0.30
Published
IPC simple use
Downloads
5
Maintainers
Readme
IPC Evolution
Node's module for local Inter Process Communication with full support for Linux, Mac and Windows. Simple use. Buffer enqueued and sync stream.
Install
npm i ipc-evo
Usage
const Ipc = require('ipc-evo');
const ipc = new Ipc('myAppName');
ipc.event('otherAppName').on('anyEvent', data => {
// ... listen event from `otherAppName`
});
// ... send event to specific app
ipc.event('otherAppName').send('anyoneEvent', { hello: 'world' });
API
Use Events
const ipc = new Ipc('app1');
// send ways :
// ... send event to `app2`
ipc.event('app2').send('anyoneEvent', data);
// ... send event to `app2`
ipc.send('app2', 'anyoneEvent', data);
// ... send event to all connected apps
ipc.broadcast('anyoneEvent', data);
// listen ways :
ipc.event('app2').on('anyEvent', data => {
// ... listen event from `app2`
});
ipc.on('app2', (event, data) => {
// ... listen any event from `app2`
});
ipc.on([ 'app2', 'app3' ], (event, data) => {
// ... listen any event from `app2` and `app3`
});
ipc.on('event', (appId, event, data) => {
// ... listen all events from all apps
});
Use Services
// `app1` :
const ipc = new Ipc('app1');
// `app1` set services :
ipc.setServices({
getItem(tableName, itemId){
this.appId; // appId who calls
return getItemFromDb(tableName, itemId);
},
saveItem(tableName, newItem){ // function many arguments dynamic
return saveItemFromDb(newItem);
}
});
/**************/
// `app2` :
const ipc = new Ipc('app2');
// return services avaiables and params names
const servicesTypes = await ipc.appServices('app1');
// `app2` accessing `app1` services
const item = await ipc.app('app1').getItem('user', '123123');
console.log(item); // prints result from other application
const anyValue = {
// The object will remain almost intact in another application
// Functions will be removed
// Circular references are maintained
};
const result = await ipc.app('app1').saveItem('product', anyValue);
// ... broadcast function in many apps
const results = await ipc.app().functionInManyApps();
// Array with the results of all the applications that had this function
// In case of no function or error the default result is null
Events
ipc.apps(); // return appIds connecteds
ipc.on('start', () => { /**/ }); // When server starts
ipc.on('ready', appId => { /**/ }); // When ready connection from other application
ipc.on('close', appId => { /**/ }); // When close connection from other application
ipc.on('error', error => { /**/ }); // When any error in communication
ipc.on('event', (appId, event, data) => { /**/ }); // Events from all applications
ipc.on('reply', (appId, result, error) => { /**/ }); // Response from unreachable promises
Configs
const defaultOptionalConfigs = {
timeoutService: Infinity, // Timeout in milliseconds to any service call throws error
rootPath: '/tmp/app.', // Root path to socket IPC
retryTime: 1000, // Time in milliseconds to retry connection other application
retryLimit: Infinity // Number of max tries to connection other application
};
const ipc = new Ipc('myAppName', defaultOptionalConfigs);