# peer-node

> Peer Node for constructing p2p networks. Supports RSA keypair encryption of data payload

Latest version **0.0.8** (published 2018-06-02) · GPL-3.0 license · 0 weekly downloads

## Install

```sh
npm install peer-node
pnpm add peer-node
yarn add peer-node
bun add peer-node
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.8 |
| Published | 2018-06-02 |
| First published | 2018-05-23 |
| Weekly downloads | 0 |
| License | GPL-3.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 90.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | yes |
| Author | Nathaniel Thomas |
| Maintainers | nthomas20 |
| Keywords | p2p, peer network, peer, networking, distributed network, network, computer cluster |

## Links

- npm: https://www.npmjs.com/package/peer-node
- Repository: https://github.com/nthomas20/peer-node
- Homepage: https://github.com/nthomas20/peer-node#readme
- Issues: https://github.com/nthomas20/peer-node/issues
- npm.io page: https://npm.io/package/peer-node

## Dependencies (2)

- [keypair](https://npm.io/package/keypair.md) ^1.0.1
- [promise-socket](https://npm.io/package/promise-socket.md) ^3.1.1

## Recent versions

- 0.0.8 (latest) — 2018-06-02
- 0.0.7 — 2018-05-28
- 0.0.6 — 2018-05-27
- 0.0.5 — 2018-05-25
- 0.0.4 — 2018-05-23
- 0.0.3 — 2018-05-23
- 0.0.2 — 2018-05-23
- 0.0.1 — 2018-05-23

## README

# peer-node
Peer Node for constructing p2p networks. Supports RSA keypair encryption of data payload

# Install

## Using yarn
```bash
yarn add peer-node
```

## Using npm
```bash
npm install peer-node
```

# Documentation

Clone the master branch from github

```bash
git clone https://github.com/nthomas20/peer-node.git
```

Navigate to the repository directory and run the following (You may also replace `yarn` with `npm`):

```bash
yarn install; yarn jsdocs
```

Then simply open up a web browser and navigate to: `localhost:8080` to view the documentation


# Launch a Peer Node

If a Peer wishes to accept connections, it must run its own Peer Node Server. Here's a simple server that will accept incoming connections and every 10 seconds broadcast a message to all connected peers.

```js
'use strict'

const p2p = require('peer-node')

let Host = new p2p.Host(`127.0.0.1`, 3000)
let Node = new p2p.Node(Host)

Node.on(
  `peerConnected`,
  data => console.log(`Peer connected: hash = ${data.peerHash}`)
)

setInterval(
  () => {
    console.log(`sending broadcast`)
    Node.broadcast(`HOWDY`, `I wish to bid you welcome! All are welcome here!`)
  },
  10000
)

Node.listen()
```

# Launch an outgoing Peer Connection

Remote systems can connect without hosting their own incoming server. If you launch the previous script in one terminal and run the following script in one or more terminals, you will see the communications

```js
'use strict'

const p2p = require('peer-node')

let Host = new p2p.Host(`127.0.0.1`, 3000)
let Peer = new p2p.Peer(Host)

Peer.generateKeypair() // This will secure communications between nodes

Peer.on(
  `connect`,
  () => console.log(`Connected to the host! Huzzah!!`)
)

Peer.on(
  `message`,
  payload => console.log(payload.data.toString()) // Output the message payload that was broadcast
)

Peer.connect()
```

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