node.js library for synchronous communication between node processes
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib v3.0.0 Aug 11, 2018
src fix windows build error Aug 12, 2018
test/server-client fix windows build error Aug 12, 2018
.gitignore playground Oct 11, 2017
.travis.yml
LICENSE update readme and license Oct 17, 2017
README.md update readme Aug 12, 2018
binding.gyp
index.d.ts
index.js
package.json

README.md

node-sync-ipc

Build Status

node-sync-ipc is a tiny library making it possible for node processes to send synchronous message to other processes. It can block the client side process until the server make a response.

Install

npm install node-sync-ipc

Usage

Server side:

// server.js

const SyncIPCServer = require("node-sync-ipc").SyncIPCServer;

// pipe File
// on Unix based systems, pipe file should be a sock file path
// on Windows, pipe should be named pipes
// const pipeFile = "\\\\.\\pipe\\somePipeName"; // <- windows
const pipeFile = path.join(require('os').tmpDir(), 'tmp.sock');

const server = new SyncIPCServer(pipeFile);

server.startListen();

server.onMessage("foo",function(res,bar){
    bar = bar + " " +bar;
    // block the child process for one second
    setTimeout(function(){
        // the first argument will be passed to child process as the result
        res(bar);
    },1000)
});

//stop server when not needed
//server.stop()

Client Side:

// client.js

const SyncIPCClient = require("node-sync-ipc").SyncIPCClient;

// pipe File to connect to
const serverHandle = path.join(require('os').tmpDir(), 'tmp.sock');

const client = new SyncIPCClient(serverHandle);

// will log "echo content echo content" in console

console.log(client.sendSync("foo","echo content"));

Attention

Data should be serializable

Data to be transferred will be serialized and deserialized in the format of JSON. Error will be thrown if data is not serializable by JSON.stringify.

Also class information will be lost during the communication.

Electron && NW.js

This module has c++ add-ons, so you have to rebuild it to use it in Electron.

Copyright

Copyright (c) 2018 Hang Ma. See LICENSE for details.