# @hyperswarm/network

> The networking guts of Hyperswarm

Latest version **2.1.0** (published 2020-09-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install @hyperswarm/network
pnpm add @hyperswarm/network
yarn add @hyperswarm/network
bun add @hyperswarm/network
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2020-09-08 |
| First published | 2018-09-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 38.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 35 |
| Author | Mathias Buus |
| Maintainers | davidmarkclements, pfrazee, mafintosh |
| Keywords | hyperswarm, networking, p2p, network |

## Links

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

## Dependencies (3)

- [utp-native](https://npm.io/package/utp-native.md) ^2.2.1
- [nanoresource](https://npm.io/package/nanoresource.md) ^1.3.0
- [@hyperswarm/discovery](https://npm.io/package/@hyperswarm/discovery.md) ^2.0.0

## Recent versions

- 2.1.0 (latest) — 2020-09-08
- 2.0.0 — 2020-07-09
- 1.5.1 — 2020-07-07
- 1.5.0 — 2020-04-20
- 1.4.0 — 2020-04-18
- 1.3.2 — 2020-04-07
- 1.3.1 — 2020-04-06
- 1.3.0 — 2020-04-02
- 1.2.0 — 2020-02-28
- 1.1.3 — 2020-02-25
- 1.1.2 — 2019-10-24
- 1.1.1 — 2019-10-20
- 1.1.0 — 2019-10-04
- 1.0.3 — 2019-07-29
- 1.0.2 — 2019-07-25
- … 7 more at https://npm.io/package/@hyperswarm/network/versions

## README

# @hyperswarm/network

The low level networking guts of the Hyperswarm stack.

```
npm install @hyperswarm/network
```

## Usage

``` js
const network = require('@hyperswarm/network')()

const nw = network()

nw.bind(function () {
  // topic should be a 32 byte buffer
  nw.lookupOne(topic, function (err, peer) {
    if (err) throw err
    nw.connect(peer, function (err, socket) {
      if (err) throw err
      socket.write('Hello World!')
    })
  })
})
```

## API

#### `net = network([options])`

Create a new network instance.

Options include:

``` js
{
  bind () {
    // called when the network is bound
  },
  close () {
    // called when the network is fully closed
  },
  socket (socket) {
    // called when an incoming socket is received
  },
  // Optionally overwrite the default set of bootstrap servers
  bootstrap: [addresses],
  // Set to false if this is a long running instance on a server
  // When running in ephemeral mode you don't join the DHT but just 
  // query it instead. If unset, or set to a non-boolean (default undefined)
  // then the node will start in short-lived (ephemeral) mode and switch 
  // to long-lived (non-ephemeral) mode after a certain period of uptime
  ephemeral: undefined
}
```

#### `nw.bind([preferredPort], [callback])`

Bind to a preferred port. Must be called before connecting.

Safe to call multiple times. If already bound or binding it will call
the callback when fully bound.

#### `nw.close([callback])`

Fully close the network.

Safe to call multiple times.

#### `nw.connect(peer, callback)`

Connect to a peer. Will do UDP holepunching.

Callback is called with `(err, socket, isTCP)`. If the underlying socket is a TCP socket `isTCP` will be true, if it is a UTP socket it will be false.

#### `announcer = nw.announce(topic)`

Start announcing the network on the Hyperswarm discovery network.

#### `lookup = nw.lookup(topic)`

Start doing a lookup on the Hyperswarm discovery network.

#### `nw.lookupOne(topic, callback)`

Lookup a single peer on the Hyperswarm discovery network.

## License

MIT

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