ipc-portal

node portal, comunication between node apps

Usage no npm install needed!

<script type="module">
  import ipcPortal from 'https://cdn.skypack.dev/ipc-portal';
</script>

README

IPC Portal

Node Inter Process Communication function based

a nodejs module for only local Inter Process Communication with full support for Linux, Mac and Windows. It also supports all forms of socket communication from low level unix and windows sockets to TCP sockets.

inspired by the library: node-ipc

A great solution for complex multiprocess Neural Networking in Node.JS

  • high performance
  • support access concurrency
  • super lightweight with only 1 file and no dependencies

get started:

npm i ipc-portal

using

  • Instance portal object setting application uniq name
  • And put functions sync or async or any values to another app can execute or get

example:

application A


/* Instance */
const Portal = required('ipc-portal')
const portal_in_a = new Portal('myApplicationNameA', {

  my_function_in_a(num1, num2){
    console.log('run function in app A')
    /* ... */
    return num1 + num2
  },

  async my_async_function_in_a(param1, param2, param3){
    console.log('run function async in app A')
    /* ... */
    return [ param1, param2, param3 ]
  },

  any_bool_value: true,
  any_object_value: { k: v },
  any_string_value: 'Lorem Ipsum'

  /* ... */

}, {
  log: true || console.log // Optional
})

/* Calling function in another app */
await portal_in_a.from('myApplicationNameB').my_function_in_b(attr1, attr2)

application B


/* Instance */
const Portal = required('ipc-portal')
const portal_in_b = new Portal('myApplicationNameB', {

  my_function_in_b(attr1, attr2){
    /* ... */
    console.log('run function in app B')
    return 'Ok'
  },
  
  /* ... */

})

/* Connect with app A */
let app_a = await portal_in_b.to('myApplicationNameA')

/* Call any function or get any value from application A */
let result = await app_a.my_function_in_a(10, 30)
let result2 = await app_a.my_async_function_in_a('item', 'col1', 'col2')
let { k } = await app_a.any_object_value

/* Check connection */
app_a.is_connected()

/* Reconnect */
await app_a.reconnect()

/* Connection events */
app_a.on('connect:myApplicationNameA', () => {})
app_a.on('disconnect:myApplicationNameA', () => {})
app_a.on('error:myApplicationNameA', () => {})
app_a.on('destroy:myApplicationNameA', () => {})

broadcasting

/* Instance app A */
const Portal = required('ipc-portal')
const portal_in_a = new Portal('myApplicationNameA', {
  /* ... */
})
/* Instance app B */
const Portal = required('ipc-portal')
const portal_in_b = new Portal('myApplicationNameB', {
  receive_broadcast(arg1, arg2){
    console.log('run function in app B')
    return 'Ok B'
  },
})
/* Connect to A */
portal_in_b.to('myApplicationNameA')
/* Instance app C */
const Portal = required('ipc-portal')
const portal_in_c = new Portal('myApplicationNameC', {
  receive_broadcast(arg1, arg2){
    console.log('run function in app C')
    return 'Ok C'
  },
})
/* Connect to A */
portal_in_c.to('myApplicationNameA')
/* In app A call broadcast for all app connecteds */
let result_broadcast = await portal_in_a.broadcast.receive_broadcast(value1, value2)

/* result is array if exists receive_broadcast in 2 or more */
let [ result_from_b, result_from_c ] = result_broadcast

working with buffer item in multi-level objects


let buffer = Buffer.from('my buffer text or any file...', 'utf-8')
let object_to_send = {
  v1: {
    v2: {
      a1: [
        a2: [ buffer ]
      ]
    }
  }
}

await portal.function_in_another_app(object_to_send)

setting new values or functions after instance

/* Instance app A */
const Portal = required('ipc-portal')
const portal = new Portal('myApplicationName', {
  /* ... */
})

portal.set('value_name', value)
/* Or */
portal.set(function myfunction(arg1, arg2){
  /* ... */
})