Common Questions
What is PeerJS?
PeerJS is a JavaScript library that simplifies WebRTC peer-to-peer connections. It provides a clean API for data channels and media streams so you don't have to deal with the complexity of WebRTC directly.
Do I need a server?
PeerJS needs a signaling server to help peers find each other (the "handshake"). After that, data flows directly between browsers. The default PeerJS Cloud server handles signaling for free. Data itself never touches a server (unless a TURN relay is needed).
What kind of data can I send?
PeerJS has the BinaryPack serialization format built-in. This means you can send any JSON type (like String, numbers, null, ...) as well as Blob and ArrayBuffers. Simply send arbitrary data and you'll get it out the other side:
conn.send({
strings: "hi!",
numbers: 150,
arrays: [1, 2, 3],
evenBinary: new Blob([1, 2, 3]),
andMore: { bool: true },
});
Are there any caveats?
A small percentage of users are behind symmetric NATs. When two symmetric NAT users try to connect to each other, NAT traversal is impossible and no connection can be made. A workaround is to proxy through the connection through a TURN server.
How do I use a TURN server?
When creating your Peer object, pass in the ICE servers as the config key of the options hash.
var peer = new Peer({
config: {
iceServers: [
{ url: "stun:stun.l.google.com:19302" },
{ url: "turn:homeo@turn.example.com:80", credential: "homeo" },
],
} /* Sample servers, please use appropriate ones */,
});
What if my peer has not yet connected to the server when I attempt to connect to it?
When you try to connect to a peer, PeerServer may hold a connection offer for up to 5 seconds before rejecting it. This is useful if you want to reconnect to a peer as it disconnects and reconnects rapidly between web pages.
Why am I unable to connect?
You could be behind a symmetric NAT, in which case you'll need to set up a TURN server.
Another possible issue is your network blocking port 443, which the PeerServer cloud runs on. In this case you must use your own PeerServer running on an appropriate port instead of the cloud service.
How many peers can connect?
There's no hard limit in PeerJS itself. In practice, a single peer can maintain a handful of simultaneous WebRTC connections (roughly 5-10) before browser performance degrades. For larger groups, consider an SFU (Selective Forwarding Unit).
What about latency/bandwidth?
Data sent between the two peers do not touch any other servers, so the connection speed is limited only by the upload and download rates of the two peers. This also means you don't have the additional latency of an intermediary server.
Where can I get help?
- GitHub Discussions
- Stack Overflow (tag:
peerjs) - Discord