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]

Peanut butter & chocolate

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

Comment

Your email address will not be published. Required fields are marked

  1. Hi,
    Is there somewhere an example hot to stream an audio file to clients using webrtc ?

    And how about seeking in audio files so that the playback can start in the middle of the file ?
    Any help is welcome since I havent found any help on that yet :/

    1. WebRTC getUserMedia allows you to specify whether you want to stream audio from the mic or video from the camera or both. Here’s how you would set Phono (http://phono.com) to only stream audio:

      audio: {
      type: “jsep”,
      media: {
      audio: true,
      video: false
      },

      The pure WebRTC API controls are a little more verbose such as:

      navigator.webkitGetUserMedia({audio:true}, gotStream);

      If you are trying to stream an audio file that has already been recorded, you do not need WebRTC. Just use an HTML5 audio tag.

  2. Hi, can anybody help me out in resolving echo problem in AUDIO.

    I have implemented webRTC using tomcat server, everything is working fine except too much echo while conversation between two clients.

    I have implemented getUserMedia API as:

    {audio:true, video:true}

    can we add any echo canceller code here or…

  3. I get an error when executing: “var rtc = holla.connect();”

    — Undefined method. —

    What client API are you using?

    Thanks

  4. We are in a dilemma whether to use node.js or openfire for signalling. Is node.js supported by all web-browsers ,IE, Chrome, Mozilla, Safari and Opera .

        1. Prior to starting @twelephone, I built (and sold) a Node.JS PaaS called Nodester. We supported web sockets on the platform and could handle approximately 1M web socket connections per AWS server. You can horizontally scale web sockets across multiple servers for the same site if needed by using Redis. In fact, Socket.io supports Redis scaling out of the box! https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO

          Today’s developers are solving the signaling issue with one of these three technologies: websockets, xmpp/jingle, and sip. All of them are good choices. I happen to prefer websockets. If you use a websocket framework like socket.io, they handle fallback technologies for you automatically to run on all web browsers (http://davidwalsh.name/websocket).

          Hope this helps…
          Chris

          1. we are using node.js for signalling . Everyhting works great for a video conferencing solution without plugin.

  5. Hi Tsahi,

    We just released an Rtcomm sample that only requires the Mosca MQTT broker running on Node.js along with angular-rtcomm. Instructions are on the angular-rtcomm page. I’m with you, Node.js and WebRTC are a perfect pair.

    -Brian

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}