npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ipcee

v2.0.0

Published

IPC combined with EventEmitter

Downloads

347

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.