Michelle Bu and Eric Zhang announced the release of PeerJS 0.1.7 on March 6th as a wrapper around WebRTC, a W3C initiative meant to facilitate P2P communication in the browser.
The burgeoning role of websockets notwithstanding, PeerJS represents a radical shift away from server-orchestrated data transfer.
"Websockets and WebRTC DataChannels seem the same—both support binary and allow you to send arbitrary data from one client that may eventually reach another client. However, they're fundamentally different. With WebRTC DataChannels, peers can send data to each other without information ever going through some central server. Take Facebook or Google chat, for example. Sending a message to someone sitting right next to you takes a good second. That's because your message has to travel physically from your computer to Facebook’s servers some 50, 60 router hops away. From a networking point of view this is terribly suboptimal. Ideally, this packet should travel just about 20 feet—and this is what WebRTC allows us to do. The entire topology of networking is changing from something that looks like a spider to something that is more like a web," Bu says.
Embracing this new ecosystem, PeerJS is meant to wrap the complexity of the WebRTC spec into a digestible api:
- Use Chrome version 26 or higher. At this time, that means versions of canary or beta are the only browsers that work. Watch the WebRTC status for browser compatibility updates in the coming months. Bu estimates interoperability with the latest versions of Firefox to be 2-3 months out.
- Set up a PeerServer. Either sign up for the hosted variant, grab the source and build your own, or
npm install peer
on node.js. "Once you connect to a peer, you no longer need the server anymore unless you plan to connect to more peers," Bu says. - Spin up the code. Take a look at the hello world example to see how "PeerJS saves you the trouble of having to build and running a brokering server and having to understand all of the intricacies of the WebRTC PeerConnection and DataChannel spec, which includes countless handlers that need to be set, edge cases that need to be considered, and browser differences that need to be handled," Bu says.
Outside of the handful of chat demos, examples of PeerJS in the wild are rare given the fluid nature of the WebRTC spec. An exception is PeerKit, an emerging project created by Zhang as a P2P CDN. “Imagine a world where cat images are served from the guy next to you in a coffee shop who happens to be browsing the same site instead of a central server in Nevada,” Bu says.
A coming generation of apps built atop stateful, bi-directional data transfers between browsers is likely a couple years out, but in the interim, "[WebRTC is] a party for developers," Bu says. "There are definitely lots of technical problems to tackle when considering all state to be held in the clients."