β οΈ This project is unmaintained. Please, use peerjs.
A simple WebRTC Data Channel library. It allows the communication between two nodes.
npm install --save quickp2p- Deploy the
serverfolder (a node.js server for temporary storage) somewhere. - Configure the store:
importquickp2p,{SimpleStore}from"quickp2p";quickp2p.setStore(newSimpleStore(YOUR_STORE_URL));First, you have to create a channel:
constchannel=awaitquickp2p.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:
constchannel=awaitquickp2p.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.
See https://afska.github.io/quickp2p for a live demo!
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 StringBy 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([ ... ]).
You can change connection timeout (in milliseconds) by using quickp2p.setTimeout(15 * 1000).
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.
nvm use
npm installnpm startnpm run buildnpm run build-lib./deploy-demo.sh./deploy-lib.sh