New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More

local-machine-network

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

local-machine-network

Create a machine local network with leader election


Version published
Maintainers
1
Created

local-machine-network

npm version Dependencies

Provides a local machine network for simple inter-process communication (IPC). Comes in two variants, one for exchanging objects between the leader and client and one for getting the raw sockets.

Installation

npm install local-machine-network

Object based messaging

const { ObjectNetwork, JSONCodec } = require('local-machine-network');

const net = new ObjectNetwork({
  path: 'path-to-socket',

  codec: JSONCodec
});

net.onLeader(() => {
  console.log('This instance is the leader!');
});

net.onConnection(other => {
  console.log('Incoming connection from someone else:', other);
});

net.onMessage({ returnPath, data } => {
  console.log('Got a message:', data);

  if(data.type === 'request') {
    // If type is request send something back
    returnPath.send({
      type: 'response',
    });
  }
});

// Start the network
net.start()
  .then(() => {
    // Send a message to the leader - with any valid JSON
    net.send({
      type: 'request',
    });
  })
  .catch(handleConnectionError);

Low-level networks

const { LowLevelNetwork } = require('local-machine-network');

const net = new LowLevelNetwork({
  path: 'path-to-socket'
});

net.onLeader(serverSocket => {
  console.log('This instance is the leader!');
});

net.onConnect(socket => {
  console.log('Connected to the leader:', socket);
});

net.onConnection(socket => {
  console.log('Incoming connection from someone else:', socket);
});

// Start the network
net.start()
  .then(...)
  .catch(handleConnectionError);

FAQs

Package last updated on 16 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts