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

27/03/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 & chocolateWho 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.

Taste the Node.JS and WebRTC experience now by giving Twelephone a spin. You’ll be glad that you did!

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

Responses

Santosh says:
March 30, 2013

I developed webrtc using Tomcat7 webscokets and its a lot easier and we can exchange http sessions. Node JS is not yet used on call center world

Reply
    Mohsun says:
    February 29, 2016

    I prefere to use asterisk and node js with ari-client package.

    Reply
Sam Dutton says:
June 5, 2013

Great post!

Shameless self promotion, but I ran a codelab at Google I/O that shows how to build a video chat app using Node and socket.io for signalling:

http://bitbucket.org/webrtc/codelab.

Reply
Doug Pelton says:
June 16, 2013

There’s a new release of easyRTC open source with some great improvements including support for Firefox at https://github.com/priologic/easyrtc

Also check out our free video chat service tawk.com built on top of our easyRTC Enterprise platform.

Reply
daslicht says:
July 2, 2013

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 :/

Reply
    Chris Matthieu says:
    July 3, 2013

    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.

    Reply
Yogendera says:
August 7, 2013

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…

Reply
Héctor says:
November 1, 2013

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

— Undefined method. —

What client API are you using?

Thanks

Reply
Chris Matthieu says:
November 1, 2013

Twelephone is no longer using Holla. We are now using Phono (http://phono.com). You should give it a try. Their docs are also better.

Reply
hanumesh says:
December 17, 2013

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 .

Reply
    Tsahi Levent-Levi says:
    December 17, 2013

    Hanumesh,

    Node.js is a server side technology – as such it has no limitation on the web browser side.
    What you should be “concerned” about is probably the Socket.io side of the story, and its support is detailed here: http://socket.io/#browser-support

    Reply
      hanumesh says:
      December 17, 2013

      Thanks for the reply. When i goggle i get to know that scalability(clustering) is an issue wrt to node.js when compared to openfire or ejabberd . Aslo , using the following link , http://stackoverflow.com/questions/9924822/scalability-issues-relating-to-socket-io . I found that websocket.io can handle max of 10,000 requests , what should we do if requests >10,000

      Reply
        Chris Matthieu says:
        December 17, 2013

        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

        Reply
          hanumesh says:
          March 10, 2014

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

          Reply
the technologies - node.js & getting live - kerf_.BLOG() says:
September 2, 2014

[…]  tight integration with webRTC […]

Reply
Brian Pulito says:
January 21, 2016

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

Reply

Comment