# local-machine-network

> Create a machine local network with leader election

Latest version **1.0.0** (published 2021-07-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install local-machine-network
pnpm add local-machine-network
yarn add local-machine-network
bun add local-machine-network
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2021-07-16 |
| First published | 2018-02-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=12.0.0 |
| Dependencies | 3 |
| Unpacked size | 73 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Maintainers | aholstenson |
| Keywords | ipc, socket, leader-election |

## Links

- npm: https://www.npmjs.com/package/local-machine-network
- Repository: https://github.com/aholstenson/local-machine-network
- Homepage: https://github.com/aholstenson/local-machine-network#readme
- Issues: https://github.com/aholstenson/local-machine-network/issues
- npm.io page: https://npm.io/package/local-machine-network

## Dependencies (3)

- [atvik](https://npm.io/package/atvik.md) ^3.1.0
- [debug](https://npm.io/package/debug.md) ^4.3.2
- [proper-lockfile](https://npm.io/package/proper-lockfile.md) ^4.1.2

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2021-07-16
- 0.3.0 — 2019-05-28
- 0.2.1 — 2018-11-25
- 0.2.0 — 2018-11-25
- 0.1.0 — 2018-02-04

## README

# local-machine-network

[![npm version](https://badge.fury.io/js/local-machine-network.svg)](https://badge.fury.io/js/local-machine-network)
[![Dependencies](https://david-dm.org/aholstenson/local-machine-network.svg)](https://david-dm.org/aholstenson/local-machine-network)

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

```javascript
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

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

---
_Source: https://npm.io/package/local-machine-network · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
