PeerJS provides a complete, configurable, and easy-to-use peer-to-peer API built on top of WebRTC.
This fork only supports data connections and json serialization.
Include the library
with npm:
npm install peerjs
and the usage:
import Peer from 'peerjs-esnext';
Create a Peer
const peer = new Peer('pick-an-id');
// You can pick your own id or omit the id if you want to get a random one from the server.
Connect
const conn = peer.connect('another-peers-id');
conn.on('open', () => {
conn.send('hi!');
});
Receive
peer.on('connection', (conn) => {
conn.on('data', (data) => {
// Will print 'hi!'
console.log(data);
});
conn.on('open', () => {
conn.send('hello!');
});
});
npm test
Q. I have a message Critical dependency: the request of a dependency is an expression
in browser's console
A. The message occurs when you use PeerJS with Webpack. It is not critical! It relates to Parcel parcel-bundler/parcel#2883 We'll resolve it when updated to Parcel V2.
PeerJS is licensed under the MIT License.