Skip to content

afska/quickp2p

Repository files navigation

quickp2p

A simple WebRTC Data Channel library. It allows the communication between two nodes.

Install

npm install --save quickp2p

Configuration

  • Deploy the server folder (a node.js server for temporary storage) somewhere.
  • Configure the store:
import quickp2p, { SimpleStore } from "quickp2p";

quickp2p.setStore(new SimpleStore(YOUR_STORE_URL));

Usage

First, you have to create a channel:

const channel = await quickp2p.createChannel();

channel
  .on("connected", () => { /* channel connected */ })
  .on("data", (message) => { /* message received */ })
  .on("timeout", () => { /* connection timeout */ })
  .on("disconnected", () => { /* channel disconnected */ });

Then, you can send channel.token to the other side, which should be running the following code:

const channel = await quickp2p.joinChannel(token);

channel
  .on("connected", () => { /* channel connected */ })
  .on("data", (message) => { /* message received */ })
  .on("timeout", () => { /* connection timeout */ })
  .on("disconnected", () => { /* channel disconnected */ });

All channels have these methods:

Method Parameter Description
send String or Buffer Sends the data or throws an error if the connection was lost.
disconnect - Ends the connection.

If a channel emitted "timeout" or "disconnected", it should be discarded.

Demo

See https://afska.github.io/quickp2p for a live demo!

Signalling server

You can use your own key-value store:

quickp2p.setStore({
  save(key, data) {
    // save `data` under a certain `key`
    // return a promise
  },

  get(key) {
    // retrieve the data from `key`
    // return a promise of the data
  }
});

// `data` is always a base64-encoded String

ICE Servers

By default, it uses the following ICE servers:

[{ urls: "stun:stun.l.google.com:19302" }]

You can set another list of servers by using quickp2p.setIceServers([ ... ]).

Timeout

You can change connection timeout (in milliseconds) by using quickp2p.setTimeout(15 * 1000).

⚠️ Symmetric NATs

This default server list won't work if both peers are under Symmetric NATs. To address that problem, you'll need to use a TURN server.

A quick workaround could be enabling a DMZ on one side.

Development

Install

nvm use
npm install

Run the app

npm start

Build

Demo

npm run build

Library

npm run build-lib

Deploy

Demo

./deploy-demo.sh

Library

./deploy-lib.sh