# socket.io-peer

> WebRTC data channel communication with a socket.io-like API

Latest version **2.2.0** (published 2018-01-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install socket.io-peer
pnpm add socket.io-peer
yarn add socket.io-peer
bun add socket.io-peer
```

## Health

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

Positive: no vulnerabilities.

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

Negative: insecure dependencies; abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.0 |
| Published | 2018-01-19 |
| First published | 2018-01-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 10 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Matthew Hall |
| Maintainers | springboardvr |
| Keywords | webrtc, peer-to-peer, peer |

## Links

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

## Dependencies (10)

- [hat](https://npm.io/package/hat.md) ^0.0.3
- [debug](https://npm.io/package/debug.md) ^2.6.3
- [to-array](https://npm.io/package/to-array.md) ^0.1.4
- [extend.js](https://npm.io/package/extend.js.md) 0.0.2
- [has-binary](https://npm.io/package/has-binary.md) 0.1.7
- [simple-peer](https://npm.io/package/simple-peer.md) ^7.0.0
- [webrtcsupport](https://npm.io/package/webrtcsupport.md) ^2.2.0
- [component-bind](https://npm.io/package/component-bind.md) ^1.0.0
- [component-emitter](https://npm.io/package/component-emitter.md) ^1.2.1
- [socket.io-peer-parser](https://npm.io/package/socket.io-peer-parser.md) github:springboardvr/socket.io-peer-parser

## Recent versions

- 2.2.0 (latest) — 2018-01-19

## README

#Socket.io-Peer

This module provides an **easy** and **reliable** way to setup a WebRTC connection between peers and communicate using events (the [socket.io-protocol](https://github.com/Automattic/socket.io-protocol)). It is a fork of the [socket.io-p2p](https://github.com/socketio/socket.io-p2p) package.

Socket.IO is used to transport [signalling data](http://www.html5rocks.com/en/tutorials/webrtc/infrastructure/#what-is-signaling) and as a fallback for clients where WebRTC `PeerConnection` is not supported.

## How to use

Create a socket connection, pass it to `Peer`. On the Client:

```js
var Peer = require('socket.io-peer');
var io = require('socket.io-client');
var socket = io();

var peer = new Peer(socket);

peer.on('ready', function(){
  peer.usePeerConnection = true;
  peer.emit('peer-obj', { peerId: peerId });
})

// this event will be triggered over the socket transport
// until `usePeerConnection` is set to `true`
peer.on('peer-msg', function(data){
  console.log(data);
});
```

If you're not using browserify, then use the included standalone file `socketiopeer.min.js`. This exports a `Peer` constructor on `window`.

On the server, use the [socket.io-peer-server](https://github.com/tomcartwrightuk/socket.io-peer-server) to take care of signalling. All clients who support WebRTC data connections will exchange signalling data via the default `/` namespace.

```js
var server = require('http').createServer();
var io = require('socket.io')(server);
var peer = require('socket.io-peer-server').Server;
io.use(peer);
server.listen(3030);
```

WebRTC Peer connections can also be established by exchanging signalling data within a socket.io room. Do this by calling the `peer` server within the `connection` callback:

```js
var server = require('http').createServer();
var io = require('socket.io')(server);
var peer = require('socket.io-peer-server').Server;
server.listen(3030);

io.on('connection', function(socket){
  clients[socket.id] = socket;
  socket.join(roomName);
  peer(socket, null, room);
});
```

See [chat app](https://github.com/socketio/socket.io-peer/tree/master/examples/chat) for full example.

## API

### `peer = new Peer(socket, opts, cb)`

Create a new socket.io-peer connection.

The `opts` object can include options for setup of the overall socket.io-peer connection as well as options for the individual peers - specified using `peerOpts`.

- `numClients` - max number of peers each client can connect to; `5` by default.
- `autoUpgrade` - upgrade to a peer connection (from a socket.io one) when peers are ready; `true` by default
- `peerOpts` - object of options to be passed to underlying peers. See [here](https://github.com/feross/simple-peer/blob/master/README.md#api) for currently supported options. See [here](examples/streaming) for an example.

`cb` is an optional callback. Called when connection is upgraded to a WebRTC connection.

### `peersocket.on('upgrade', function (data) {})`

Triggered when Peer connection is converted to a WebRTC one.

### `peersocket.on('peer-error', function (data) {})`

Triggered when a `PeerConnection` object errors during signalling.

## Roadmap of development

- Support for packets containing multiple binary blobs - packets can only contain one blob in this version
- Allow a peer to act as a relay between peers that don't support PeerConnection and those that do.

PRs and issue reports are most welcome.

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