Badass JavaScript

A showcase of awesome JavaScript that pushes the boundaries of what's possible on the web, by @devongovett.

PeerJS: A Peer to Peer Networking Library in JavaScript using WebRTC

February 14th 2013

I just caught wind of PeerJS, a project that makes peer to peer networking using the new WebRTC browser APIs easier.  WebRTC is extremely cutting edge and the library currently only works in Chrome Canary and Dev Channel, so take this with a grain of salt but it is exciting to see cutting edge libraries like this.

PeerJS actually consists of two parts: the client side script that communicates with other clients using WebRTC, and a Node.js server component that brokers the connections between the clients.  The server keeps track of each client that is currently online so that the clients can become aware of each other.  Once the clients know about each other, they can connect directly thanks to WebRTC’s RTCPeerConnection API, which allows sending arbitrary data between clients with an API very similar to the WebSocket API.  Both binary and textual data will be supported, but right now only text is working.

PeerJS wraps all of this up in a nice API, and they even provide a free server for you to use with an API key if you don’t want to run your own.  They also handle all of the complexities of working with the WebRTC API including handshaking, temporary binary string encoding until browsers implement sending binary data directly, and of course the actual server brokering of connections.  WebRTC handles the actual networking complexities for you, including NAT traversal, UDP and the actual peer-to-peer connections themselves.

The API looks really easy to use.  You can check out a peer to peer chat demo using PeerJS online, and its source on Github as well.  However, as I mentioned at the top of this post, the library currently only works in the Canary and Dev versions of Chrome 26, and Firefox apparently doesn’t work yet.  It is exciting to see WebRTC coming along, both in terms of live video and audio transmission as well as arbitrary data.  WebRTC is a major undertaking for browser vendors, but I think it will create some great opportunities for browser based apps in the future.

One of the things that might be made possible with WebRTC and a library like PeerJS is a browser based BitTorrent client.  Previously it has been pretty much impossible because the only protocols supported via JS have been WebSockets and HTTP.  I’m not entirely familiar with the details of WebRTC, but it sounds like it will give developers much more networking flexibility.  I’m not sure what the security restriction are. Same domain wouldn’t really apply here, so I guess it would probably just ask the user’s permission, as it should.  Let me know if this I’m mistaken, but I think a BitTorrent client could be an feasible possibility.

You can check out PeerJS on Github, and find the documentation and demos on their website.  I’m looking forward to seeing the future of WebRTC as it is implemented in browsers and demos and libraries start to make use of it!