0.2.0 • Published 10 years ago

jmsg v0.2.0

Weekly downloads
30
License
MIT
Repository
github
Last release
10 years ago

jmsg Build Status

Jmsg is a simple module to communicate with another instance across a stream or other kind of message channel. Communication is two-way, and messages are mixed action / reply and events.

Installing

$ npm install --save jmsg

Usage

const jmsg = require('jmsg');

jmsg.stream(socket, {
    foobar(obj) {
        /* ... */
    }
});

The jmsg.stream(stream, handlers) method takes a duplex stream, like a socket, and an object with message handlers.

There's also jmsg.cluster([worker], handlers), which uses the IPC channel from the cluster module. The worker argument is only used on the master side.

Finally, the main jmsg(writeFn, handlers) export is the low-level interface, where writeFn is a function that takes a JSON object to be transmitted to the dispatch(obj) handle method on the side.

Each of these returns a handle, with the following properties:

  • send(type, [value], [callback]), method to send a message. Whether a callback is specified determines whether the message expects a reply (an action) or not (an event).

  • handlers, the object containing message handlers.

  • timeout, the timeout in milliseconds to wait for a reply to an action, before the callback is called with an error.

Hacking the code

git clone https://github.com/Two-Screen/jmsg.git
cd jmsg
npm install
npm test
0.2.0

10 years ago

0.1.6

11 years ago

0.1.5

11 years ago

0.1.4

11 years ago

0.1.3

11 years ago

0.1.2

11 years ago

0.1.1

11 years ago

0.1.0

11 years ago