local-machine-network
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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);

Package Sidebar

Install

npm i local-machine-network

Weekly Downloads

4

Version

1.0.0

License

MIT

Unpacked Size

74.7 kB

Total Files

35

Last publish

Collaborators

  • aholstenson