# webrtc-swarm

> Create a swarm of p2p connections using webrtc and a signalhub

Latest version **2.9.0** (published 2017-10-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install webrtc-swarm
pnpm add webrtc-swarm
yarn add webrtc-swarm
bun add webrtc-swarm
```

## 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.9.0 |
| Published | 2017-10-14 |
| First published | 2015-04-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 377 |
| Author | Mathias Buus |
| Maintainers | mafintosh, maxogden |

## Links

- npm: https://www.npmjs.com/package/webrtc-swarm
- Repository: https://github.com/mafintosh/webrtc-swarm
- Issues: https://github.com/mafintosh/webrtc-swarm/issues
- npm.io page: https://npm.io/package/webrtc-swarm

## Dependencies (6)

- [cuid](https://npm.io/package/cuid.md) ^1.2.4
- [once](https://npm.io/package/once.md) ^1.3.1
- [debug](https://npm.io/package/debug.md) ^2.2.0
- [inherits](https://npm.io/package/inherits.md) ^2.0.1
- [through2](https://npm.io/package/through2.md) ^2.0.0
- [simple-peer](https://npm.io/package/simple-peer.md) ^6.0.1

## Recent versions

- 2.9.0 (latest) — 2017-10-14
- 2.8.0 — 2016-08-28
- 2.7.0 — 2016-08-07
- 2.6.1 — 2016-07-05
- 2.6.0 — 2016-06-24
- 2.5.0 — 2016-06-21
- 2.4.0 — 2016-02-17
- 2.3.0 — 2016-01-01
- 2.2.0 — 2015-11-02
- 2.1.1 — 2015-11-02
- 2.1.0 — 2015-11-02
- 2.0.0 — 2015-09-20
- 1.2.0 — 2015-04-28
- 1.1.1 — 2015-04-27
- 1.1.0 — 2015-04-27
- … 2 more at https://npm.io/package/webrtc-swarm/versions

## README

# webrtc-swarm

> Create a swarm of p2p connections using webrtc and a
[signalhub](https://github.com/mafintosh/signalhub).

```
npm install webrtc-swarm
```

## Usage

``` js
var swarm = require('webrtc-swarm')
var signalhub = require('signalhub')

var hub = signalhub('swarm-example', ['http://yourdomain.com'])

var sw = swarm(hub, {
  wrtc: require('wrtc') // don't need this if used in the browser
})

sw.on('peer', function (peer, id) {
  console.log('connected to a new peer:', id)
  console.log('total peers:', sw.peers.length)
})

sw.on('disconnect', function (peer, id) {
  console.log('disconnected from a peer:', id)
  console.log('total peers:', sw.peers.length)
})
```

## API

```js
var swarm = require('webrtc-swarm')
```

### var sw = swarm(hub, opts)

Creates a new webrtc swarm using
[signalhub](https://github.com/mafintosh/signalhub) `hub` for discovery and
connection brokering.

Valid keys for `opts` include:

- `wrtc` - (optional) a reference to the `wrtc` library, if using Node.
- `uuid` - (optional) a unique identifier for this peer. One is generated for
you if not supplied.
- `maxPeers` - (optional) the maximum number of peers you wish to connect to.
Defaults to unlimited.
- `wrap` - (optional) a function that can modify the WebRTC signaling data
before it gets send out. It's called  with `wrap(outgoingSignalingData,
destinationSignalhubChannel)` and must return the wrapped signaling data.
- `unwrap` - (optional) a function that can modify the WebRTC signaling data
before it gets processed. It's called  with `unwrap(incomingData,
sourceSignalhubChannel)` and must return the raw signaling data.

Additional optional keys can be passed through to the underlying
[simple-peer](https://www.npmjs.com/package/simple-peer) instances:

- `channelConfig` -  custom webrtc data channel configuration (used by
`createDataChannel`)
- `config` - custom webrtc configuration (used by `RTCPeerConnection`
constructor)
- `stream` - if video/voice is desired, pass stream returned from
`getUserMedia`


### sw.close()

Disconnect from swarm

### sw.on('peer|connect', peer, id)

`peer` and `connect` are interchangeable. Fires when a connection has been
established to a new peer `peer`, with unique id `id`.

### sw.on('disconnect', peer, id)

Fires when an existing peer connection is lost.

`peer` is a [simple-peer](https://www.npmjs.com/package/simple-peer) instance.

### sw.on('close')

Fires when all peer and signalhub connections are closed

### sw.peers

A list of peers that `sw` is currently connected to.

### swarm.WEBRTC_SUPPORT

Detect native WebRTC support in the javascript environment.

```js
var swarm = require('webrtc-swarm')

if (swarm.WEBRTC_SUPPORT) {
  // webrtc support!
} else {
  // fallback
}
```

## License

MIT

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