Mar 02 2013

Peer.js Enhances WebRTC, Allows For Peer-To-Peer Connections In The Browser

Advertisement

WebRTC is a powerful idea. It aims at establishing real-time communication inside of a browser. Google, Mozilla and Opera carry the project, its API is supported in Chrome and the nightly versions of Firefox. Does this sound limited? It sure does. The JavaScript project Peer.js is even more limited. It is only functional, not even fully, in Chrome Dev and Canary. Still it is an interesting approach to guess on the future of communication.

Well, you could go pier to pier here. But that isn't what we are talking about...

Well, you could go pier to pier here. But that isn’t what we are talking about…

Peer.js – Client-Side with Server-Side-Support

Peer.js wraps WebRTC and allows for establishing peer connections between two clients based on unique IDs. The system needs a server-side installation, the so-called PeerServer, which can be established using node.js. On the client-side Peer.js is integrated as a library. The PeerServer is only needed to connect the peers, any communication that occurs after connect happens directly between the peers.

As WebRTC only sees lukewarm support even in Chrome, Peer.js today is nothing more than an experiment. Its perspectives are good though. I would go as far as proclaiming that its success is a mere matter of time. At least as far as the functionality is concerned. If it will be exactly Peer.js or coming under a different name still has to be seen..

Peer.js Homepage

Screenshot: Peer.js Homepage

Right at this moment the project has not a lot more to offer than a demo of a chat. You’ll need to know and enter the above-mentioned unique ID to connect. Handling of IDs would be a matter of programming of course, so let’s not bemoan this demo as desultory. As the project is a hatchling, a little patience should be expectable.

Peer.js – Basic Mode of Operation

Integration of Peer.js is no big thing. The bigger thing is to constitute the PeerServer. On the side of the client the script is implemented like this:

1
<script src="http://cdn.peerjs.com/0/peer.js"></script>

A peer gets defined this way:

1
2
3
4
5
6
7
8
9
<script>
  var peer = new Peer('someid', {key: 'apikey'});
  peer.on('connection', function(conn) {
    conn.on('data', function(data){
      // Will print 'hi!'
      console.log(data);
    });
  });
</script>

And the peer with the desire to connect shows up like this:

1
2
3
4
5
6
7
<script>
  var peer = new Peer('anotherid', {key: 'apikey'});
  var conn = peer.connect('someid');
  conn.on('open', function(){
    conn.send('hi!');
  }); 
</script>

If you want to approach Peer.js without having to put lots of effort into it, you will want to omit to establish a PeerServer on your own behalf. Thanks to the developers of Peer.js you can. They offer a PeerServer as a cloud service, free of charge for developers.

If you get eager to build your own eMule environment that way, calm down. The size of data to be shared via Peer.js is extremely limited. Only sizes less than 1 kB are working, yet not even reliably. Don’t blame Peer.js though. This is another flaw in the browser implementation of WebRTC.

Peer.js is maintained by the two Berkeley students Michelle Bu and Eric Zhang. Zhang was part of the startup Flotype which now lives on under a different name. Zhang is no longer part of it, still he is a skilled JavaScript developer.

Related Links:

photo credit | the pier picture: Tambako the Jaguar via photopin cc

About the Author

Dieter Petereit is Noupe's Editor-In-Chief and a veteran of the web with over 25 years of experience in the world of IT. As soon as Netscape became available he started to do what already at that time was called webdesign and has carried on ever since. Almost a decade ago he started writing for several online publications, some well, some lesser known. Dieter is a heavy G-Plusser, so why not meet him over there?

Homepage

Comments and Discussions
  • Tim, 04 March 2013

    lack security much?

search form
 
image description image description