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

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.

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:

Client:

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:

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

Sending a call:

Receiving a call:

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!

[UPDATE: Here is some more information on using Node.js to build chat applications]

This post is part of my WebRTC post series.

Tags: , , , , , , , , , , , , , , , ,

Liked this post?

Share it!

Never miss a post!

Or just grab the RSS feed!

Comments

  1. 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

  2. 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.

  3. 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.

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

    • 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.

  5. Yogendera says:

    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…

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

    — Undefined method. —

    What client API are you using?

    Thanks

  7. 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.

  8. 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 .

Trackbacks

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">