Node.js and WebRTC go Together Like Peanut Butter and Chocolate

March 27, 2013

Time for a look at the technologies surrounding WebRTC. This time – Node.js

UPDATE: There's a walkthrough on installing and running a WebRTC sample on RaspberryPi with Node,js.

[I've noticed that a lot of programmers are focusing on a specific set of technologies when they go about implementing a WebRTC service. One such technology is Node.js. I asked Chris Matthieu, founder of Twelephone, to offer his opinion on why Node.js is so popular among WebRTC developers]

Who doesn't love peanut butter and chocolate? By themselves, they are equally delicious but together, they are irresistible!

The same can now be said about Node.JS and WebRTC. Node.JS is an asynchronous, server-side JavaScript engine powered by Chrome's V8 JS engine. Asynchronous is key for the nature of WebRTC (or telephony in general) because everything is an asynchronous event i.e. signaling, incoming/outgoing calls, presence, chat, etc. In addition to asynchronous, these events need to be handled in real-time.

Want to learn more about WebRTC server requirements and specifications? Enroll now to my 3-part video mini-course for free:

Modern web applications use WebSockets to manage bi-directional real-time requests/responses between clients and servers. The days of AJAX are *long* gone because it's are slower than WebSockets and it polls for data. Websockets allow servers to push data to the browser as events occur allowing for realtime notifications, chats, calls, presence changes, etc. With the leading WebSocket libraries (Socket.io, SockJS, and WS) written for Node.JS, Node is the go to platform for leveraging WebSockets. Here's some sample Socket.io code:

Server:

var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

Client:

<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
  console.log(data);
  socket.emit('my other event', { my: 'data' });
});
</script>

In addition to great WebSocket support, Node.JS is starting to see many new WebRTC libraries/modules! The first WebRTC Node.JS module to hit the community a little over 6 months ago was WebRTC.io. At the time of this article, it has 274 star gazers and 55 forks on GitHub. It supports GetUserMedia and PeerConnections on Chrome browsers and it has a great conferencing demo.

Newcomers to the developer race include: Holla, PeerJS, EasyRTC, and SimpleWebRTC. At the time of this article, none of these frameworks support Firefox; however, my bet is on Holla to have it integrated first. All four of these WebRTC engines support GetUserMedia and PeerConnections on Chrome and come with simple demos to get developers started building voice/video apps today. In addition to GetUserMedia and PeerConnections, Holla also supports P2P calls for both placing and receiving calls as well as handling chat and presence. Note: Holla is the WebRTC module used in Twelephone.

Adding Holla to your Node.JS app is simple. On the server, there are only 3 lines of code:

var holla = require('holla');
var server = http.createServer().listen(8080);
var rtc = holla.createServer(server);

On the client (browser), you basically connect to Holla, register your user, and either send or receive a call as follows:

Sending a call:

var rtc = holla.connect();
rtc.register("chris", function(worked) {
  holla.createFullStream(function(err, stream) {
    var call = rtc.call("tsahi");
    call.addStream(stream);
    holla.pipe(stream, $("#myVideo"));

    call.on("answered", function() {
      console.log("Remote user answered the call");
    });

    console.log("Calling ", call.user);
  });
});

Receiving a call:

var rtc = holla.connect();
rtc.register("tsahi", function(worked) {
  rtc.on("call", function(call) {
    console.log("Inbound call from ", call.user);
    holla.createFullStream(function(err, stream) {
      call.addStream(stream);
      call.answer();
      holla.pipe(stream, $("#myVideo"));

      call.ready(function(stream) {
        holla.pipe(stream, $("#theirVideo"));
      });
    });
  });
});

If all of these reasons aren't *yummy* enough to convince you to use Node.JS for your next WebRTC project, consider using Node because it is also perfect for SPAs (single-page applications). SPAs are becoming more popular these days. Page loads are expensive. Users want immediate results. SPAs are designed to behave like desktop or mobile applications using JavaScript on the client while passing small packets of JSON data via WebSockets to/from the servers as needed. Node.JS is a nice fit for several reasons:

  1. HTTP and JSON are first-class citizens in Node.JS.
  2. Developers get to write JavscScript for both the client and the server. One language to rule them all.
  3. Since Node.JS includes a web server in it's HTTP library, developers have more control over handling lower-level requests and responses.
  4. Node.JS also supports streaming natively which is perfect for WebRTC too.

Do note that WebRTC signaling is but one of the several types of WebRTC servers needed for a production WebRTC application.

Want to learn how to build your own Node.js signaling server for WebRTC? Check out our WebRTC Codelab


You may also like