# discovery-swarm-web

> Abstracts away discovery-swarm interaction with WebRTC and a websocket gateway.

Latest version **2.0.0** (published 2019-10-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install discovery-swarm-web
pnpm add discovery-swarm-web
yarn add discovery-swarm-web
bun add discovery-swarm-web
```

Provides the command `discovery-swarm-web`.

## 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.0.0 |
| Published | 2019-10-29 |
| First published | 2019-03-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 13.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 42 |
| Author | rangermauve |
| Maintainers | rangermauve |
| Keywords | discovery, swarm, web, browser, dat |

## Links

- npm: https://www.npmjs.com/package/discovery-swarm-web
- Repository: https://github.com/RangerMauve/discovery-swarm-web
- Homepage: https://github.com/RangerMauve/discovery-swarm-web#readme
- Issues: https://github.com/RangerMauve/discovery-swarm-web/issues
- npm.io page: https://npm.io/package/discovery-swarm-web

## Dependencies (8)

- [debug](https://npm.io/package/debug.md) ^4.1.1
- [yargs](https://npm.io/package/yargs.md) ^13.2.2
- [randombytes](https://npm.io/package/randombytes.md) ^2.1.0
- [hyperdiscovery](https://npm.io/package/hyperdiscovery.md) ^9.0.2
- [websocket-stream](https://npm.io/package/websocket-stream.md) ^5.1.2
- [discovery-swarm-stream](https://npm.io/package/discovery-swarm-stream.md) ^2.1.1
- [length-prefixed-message](https://npm.io/package/length-prefixed-message.md) ^3.0.3
- [@geut/discovery-swarm-webrtc](https://npm.io/package/@geut/discovery-swarm-webrtc.md) ^2.2.4

## Alternatives

- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads
- [@oridune/validator](https://npm.io/package/@oridune/validator.md) — 16 weekly downloads

## Recent versions

- 2.0.0 (latest) — 2019-10-29
- 1.4.1 — 2019-07-02
- 1.4.0 — 2019-06-12
- 1.3.0 — 2019-06-11
- 1.2.2 — 2019-06-11
- 1.2.1 — 2019-06-11
- 1.2.0 — 2019-06-11
- 1.1.0 — 2019-05-27
- 1.0.7 — 2019-04-30
- 1.0.6 — 2019-04-25
- 1.0.5 — 2019-03-25
- 1.0.4 — 2019-03-20
- 1.0.3 — 2019-03-20
- 1.0.2 — 2019-03-17
- 1.0.1 — 2019-03-17
- … 1 more at https://npm.io/package/discovery-swarm-web/versions

## README

# discovery-swarm-web
Abstracts away discovery-swarm interaction with WebRTC and a websocket gateway.

```shell
npm install --save discovery-swarm-web
```

## Example:

```js
const DiscoverySwarmWeb = require('discovery-swarm-web')
const swarm = new DiscoverySwarmWeb({
  stream: () => archive.replicate(),
})

swarm.join(archive.discoveryKey)
```

## How it works

Under the hood it uses [@geut/discovery-swarm-webrtc](https://www.npmjs.com/package/@geut/discovery-swarm-webrtc) to find WebRTC peers for channels, and uses [discovery-swarm-stream](https://www.npmjs.com/package/discovery-swarm-stream) over a websocket to bridge to the rest of the Dat network by using [discovery-swarm](https://www.npmjs.com/package/discovery-swarm) along with the [dat-swarm-defaults](https://www.npmjs.com/package/dat-swarm-defaults) configuration.

A websocket gets created for signaling WebRTC peers, and another one gets created to proxy connections from the P2P network.

## API

### `new DiscoverySwarmWeb(opts)`

`opts` include:
  - `stream`: The only required field. Return a stream to handle an incoming connection
  - `id`: The ID you want to use to show up in the swarm
  - `bootstrap`: An array of [signal server urls](https://github.com/geut/discovery-swarm-webrtc#server) used for WebRTC discovery. Uses [signal.mauve.moe](https://signal.mauve.moe) by default.
  - `discovery`: A `discovery-swarm-web` server URl to connect to. By default it uses [discoveryswarm.mauve.moe](wss://discoveryswarm.mauve.moe), please supply your own if you're deploying to production. All discovery servers reach out to the same P2P network.

### `swarm.join(key)`

Starts looking for peers for the given `key`. This should be either a string or a Buffer. For Dat, you should use the [discoveryKey](https://github.com/mafintosh/hypercore#feeddiscoverykey) of your hypercore or hyperdrive.

### `swarm.leave(key)`

Stops looking for peers for the given `key`.

### `swarm.close()`, `swarm.destroy()`

Close the swarm: closes all connections to peers, to the discovery server.

## CLI

You can start your own gateway for added performance and privacy using the CLI, or if you want to host one for an application you're building. By default it will listen on TCP 3472 (DISC), but that can be changed with the `--port` flag.

```shell
npm install -g discovery-swarm-web

discovery-swarm-web --port 3472
```

## Roadmap:

- [x] Attempt to connect to a local discovery instance before the internet
- [x] Generate ID if one is not provided
- [x] Auto-reconnect logic to discover-swarm-stream websocket
- [x] Emit the `connection` event
- [x] Have limits for the number of connections made for a given channel (auto-close new connections after reaching it)
  - [x] Resolve bugs with close event not firing
- [x] Don't join discovery for a key unless you get no WebRTC peers (then join after a delay)
- [ ] Disconnect peers with known ID
- [ ] Make `stream` optional with a default handshake function

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