# hello-webrtc

> A simple js library to use WebRTC.

Latest version **0.0.3** (published 2018-07-23) · ISC license · 0 weekly downloads

## Install

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

## 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.3 |
| Published | 2018-07-23 |
| First published | 2018-07-23 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 42.8 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | tangshuang |

## Links

- npm: https://www.npmjs.com/package/hello-webrtc
- Repository: https://github.com/tangshuang/hello-webrtc
- Homepage: https://github.com/tangshuang/hello-webrtc#readme
- Issues: https://github.com/tangshuang/hello-webrtc/issues
- npm.io page: https://npm.io/package/hello-webrtc

## Dependencies (6)

- [ws](https://npm.io/package/ws.md) ^5.2.2
- [uuid](https://npm.io/package/uuid.md) ^3.3.2
- [level](https://npm.io/package/level.md) ^4.0.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.10
- [query-string](https://npm.io/package/query-string.md) ^6.1.0
- [babel-polyfill](https://npm.io/package/babel-polyfill.md) ^6.26.0

## Recent versions

- 0.0.3 (latest) — 2018-07-23
- 0.0.2 — 2018-07-23
- 0.0.1 — 2018-07-23

## README

# Hello WebRTC

A simple js library to use WebRTC.

## install

```
npm install --save hello-webrtc
```

## usage

ES6:

```
import { WebRTCPeer, WebRTCClient } from 'hello-webrtc/src/hello-webrtc'
```

CommonJS:

```
const { WebRTCPeer, WebRTCClient } = require('hello-webrtc')
```

With Webpack:

```
import { WebRTCPeer, WebRTCClient } from 'hello-webrtc'
```

In browsers:

```
<script src="node_modules/hello-webrtc/dist/hello-webrtc.js"></script>
<script>
const { WebRTCPeer, WebRTCClient } = window.HelloWebRTC
</script>
```

To use:

```
let peer = new WebRTCPeer(options)
let client = new WebRTCClient(options)
```

## WebRTCPeer

### Methods

#### constructor(options)

**options**
- stun: array of objects, required
- turn: array of objects, required
- websocket: an instance of WebSocket
- user: which user do you want to connect to
- room: in which room do you want to connect to the user 
- immediate: do you want to run setup() immediately, default is true

```
let peer = new WebRTCPeer({ stun, turn, websocket, user, room, immediate: false })
```

All required.

#### setup()

To setup the connection. If you set `options.immediate` to be true, you should not run setup() by yourself again.

#### send(data)

Send data by `RTCDataChannel`, data can be any type of data.

```
await peer.send({ msg: 'Hello, there!' })
```

#### getMedia(options)

Get user device media, return stream.

```
let stream = await peer.getMedia({ video: true, audio: false })
```

#### stream(stream)

Send stream.

```
let stream = await peer.getMedia({ video: true, audio: false })
await peer.stream(stream)
```

#### receive(type, callback)

Replace callback when you receive data message or stream.

```
await peer.receive('message', (data) => {
  // ...
})
await peer.receive('streams', (streams) => {
  // ...
})
```

#### destory()

Unlink peer connection and destory context.

### Events

**setup**

When setup, only once in the life circle of a peer.

```
peer.on('setup', () => { ... })
```

**message**

When receive a message from remote peer.

```
peer.on('message', (data) => {
  // ...
})
```

**streams**

When receive streams from remote peer.

**break**

When called to break connection, before destory.

**error**

When recevie an error message.

```
peer.on('error', (error) => {})
```

## WebRTCClient

### Methods

#### constructor(options)

**options**
- stun: array of objects, required
- turn: array of objects, required
- signaling: signaling websocket server url, required, pend `token` as a query string

```
let signaling = 'ws://localhost:8686?token=xxxx-xxxx-xxx'
let node = new WebRTCClient({ stun, turn, signaling })
```

#### connect({ user, room })

Connect with another user.

```
node.connect({ user: 100202004 }) // if you do not pass room name, it means you want to create a connect not in a room
node.connect({ user: 100202004, room: 291445 })
```

Why you need a room name?
In fact, in a WebRTCClient instance, you can connect to multiple users (WebRTCPeer can only connect to another peer), with each user using one peer. But some times you may in a chat room, so two users may need more than one connection, pass a room name to certain which to connect.

Return an instance of WebRTCPeer.

#### destory()

Destory all connections in this node.

### Events

**offline**

When receive a call to offline, before destory.

**error**

When receive an error message.

## websocket server

You should implement your own signaling websokcet server. However I have provided one package:

```
npm install --save hello-webrtc-server
```

And then:

```
const SignalingServer = require('hello-webrtc-server')
const server = new SignalingServer()
```

And then use `ws://your-host.com:8686` as signaling server address to pass into WebRTCClient or WebRTCPeer.

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