ipcee

IPC combined with EventEmitter

Usage no npm install needed!

<script type="module">
  import ipcee from 'https://cdn.skypack.dev/ipcee';
</script>

README

IPC EE Build Status

IPC combined with EventEmitter2

What for?

First, RTFM child.send(message[, sendHandle]).

The sendHandle option to child.send() is for sending a TCP server or socket object to another process. The child will receive the object as its second argument to the message event.

This means that you won't be able to do things like:

child.send('message', {some: 'data'}, [data])

This library still works with the list of accepted instances. Also, if you look a but further in the code, internal messages are handled first. As stated in the docs:

There is a special case when sending a {cmd: 'NODE_foo'} message.

Things apart, this is a fancier api to communicate with child processes!

Note that I consider this as a low-level module, if you want a higher communication api, take a look at relieve. I also made a module with the same api using TCP instead of IPC: TCPEE.

Usage

Test for child start:

Child

ipc.send('started')

Master

const child = fork('child')
child.once('started', dosomething)

Play ping pong:

Child

  const ipc = IPCEE(process)

  ipc.send('started')

  ipc.on('ping', function() {
    ipc.send('pong')
  })

Master

  const server = fork('some/node/app.js')
  const client = IPCEE(server)

  client.once('started', () => {
    client.send('ping')
  })

  client.once('pong', () => {
    console.log('\o/')
  })

Or play with namespaces:

Child

  const ipc = IPCEE(process, {wildcard: true, delimiter: ':'})

  ipc.send('started')

  ipc.on('ping:me', () => {
    ipc.send('me:pong')
  })

Master

  const server = fork('some/node/app.js')
  const client = IPCEE(server, {wildcard: true, delimiter: ':'})

  client.once('started', () => {
    client.send('ping:*')
  })

  client.once('*:pong', () => {
    console.log('\o/')
  })

Caveat

Using the first argument of child_process.send(), Nodejs IPC will transport strings. Javascript objects are encoded with json internally. That said, You won't be able to pass instances.

Example:

process.on('uncaughtException', err => {
  ipc.send('error', err.toString(), err.stack)

  process.nextTick(() => {
    process.exit(1)
  })
})

Here, Temptation would be to send the full Error object but JSON.stringify(new Error('test')) will return '{}'.

Note, I made a js to string proof of concept here, it could work in some cases.

Native IPC features

IPCEE does not override any of the internals methods. This means that you'll still be able to get messages from the standard way:

process.on('message', (m, handle) => {
  if(m === 'server') {
    //do something with handle
  }
})

But it will handle accepted instances in an easy way too. For example, sending a Socket:

//server.js
ipc.send('socket', sock)
//child.js
ipc.on('socket', (sock) => {
  assert(sock instanceof Socket)
})

API

/**
 * Constructor
 * @param socket - the process/child_process to write/read to/from
 * @param options - eventemitter2 options
 */
const ipcee = new IPCEE(process, {wildcard: true})

/**
 * @param key - the key you'll listen on
 * @param ...args
 */
ipcee.send('key', arg1, arg2)

/**
 * @param key
 * @param ...args data received
 */
ipcee.on('key', function(arg1, arg2) {
})

Apart from the send method, the api inherits the one of EventEmitter2.

Licence

The MIT License (MIT)

Copyright (c) 2015 Antoine Bluchet

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.