conn-wrapper v1.1.0
conn-wrapper
A simple wrapper for connections which only allows passing string.
Usecase
There are a lots of times communication through connections and processes require dynamic Event Emitter rather than only listening on message
event. This module allows dev to create events dynamicly through those connections.
Installation
npm install --save conn-wrapper
Usage
Vanila
const cp = require('child_process');
const child = cp.fork(path.join('./child.js'));
child.on('message', function(message) {
m = JSON.parse(message);
if (m.event === 'event1') {
// Do something
} else if (m.event === 'event2') {
// Do something
} else if (m.event === 'event2') {
...
}
...
})
With conn-wrapper
const ConnWrapper = require('conn-wrapper');
const cp = require('child_process');
const child = cp.fork(path.join('./child.js'));
const wrapper = ConnWrapper(child);
// Send event
wrapper.send('event', payload)
// Listen for an event
wrapper.on('event1', function(payload) {
// Do something
});
To achieve this, the message
sent by conn.send()
must be a json or json format string. All other type will be ignored.
The message has 2 fields: event
(name of the event to be listened on) and body
(optinal, main content).
Getting To Know Yeoman
Yeoman has a heart of gold. He's a person with feelings and opinions, but he's very easy to work with. If you think he's too opinionated, he can be easily convinced. Feel free to learn more about him.
Created with
Yeoman and Generator-simple-package
License
MIT © nghiattran