As an Indian, I benefit from living 9h 30m ahead of you US readers. That’s a full 34,200 seconds of sweet access to free services that would otherwise require a long queue or wait time of some sort.
One such example is PeerJS: a library for direct peer-to-peer communication between two devices over the internet. It’s a neat library that allows you to send any data over WebRTC, not just video or audio.
I won’t go into much of the details, but for WebRTC to work, the two clients need to exchange some basic information: IP addresses, port information, media capability (what codecs are supported/unsupported), etc. The WebRTC protocol defines this exchange as Signalling.
Signalling can be done in any way: HTTPS, Bluetooth, email, IR transmitters; the specification doesn’t care (or mention) how it’s done. Developers are given the freedom to choose any transport layer for signalling.
PeerJS does this using PeerServer: a NodeJS server that allows two devices to perform signalling without having a direct connection, by serving as an information relay. After the signalling process, the devices do not need PeerServer anymore; but without PeerServer, they would never be able to connect in the first place. PeerServer can be self-hosted, but there is a free public instance as well.
PeerServer assigns random IDs to each client that connects. That way, all you need is the ID of the client you want to connect to, and PeerJS will handle the rest.
Enjoying the post so far? Share it with your friends (or to random people on PeerServer)!
One of my recent open source projects was QuickPaperScissors, a peer-to-peer rock-paper-scissors web app. It’s really simple, and uses PeerJS under the hood to power it. I started and finished it in maybe a couple of hours spread over a couple of days. I tested it during the day, and it worked at around 3:00 PM IST/5:30 AM ET.
You see, at 5:30 AM ET, there’s probably very few lunatics awake and using PeerServer. As a result, QuickPaperScissors worked flawlessly whenever I tested it — every connection was quick and elegant.
But then I showed this app to a couple of friends. At nighttime (10 PM IST/12:30 PM ET) when I tried playing with them, the darn thing just wouldn’t connect at all.
Can you guess why? I couldn’t, until today.
By 11PM IST/1:30 PM ET, most of you US readers must have woken up and started beating the shit out of the poor public PeerServer instance with your P2P rock-paper-scissors apps, overloading it and causing connection errors for my fledgling app. It (thankfully) wasn’t a problem with my code, and I’m happy that at least for once, my programming wasn’t the root cause of the issue.
And all this headbutting was a great excuse to learn a little more about how P2P works on the web, what goes on in the mysterious land of WebRTC.
.png)


