Node JS Raspberry Pi: Creating a WebRTC WOW Moment

April 10, 2014

Want to WOW your friends? Show a video calling service running off your Raspberry Pi.

Want to run WebRTC on anything? Check out my free WebRTC Device Cheat Sheet.

I’ve been running some trainings for product managers and developers here in Israel for the past several months. All of them are focused on WebRTC and how it transforms the market.

When I first did my developer training, asking for feedback at the end of the workshop, one thing that was raised is that going over sample code and understanding it isn’t enough. What is missing is to show the simplicity. To show how easy it is to get up and running from scratch.

Call it the 0 to 100 kph of doing a hello world video calling.

With WebRTC it can be achieved just before lunch break and in record time.

Raspberry Pi

What I decided doing is purchase a Raspberry Pi Starter Kit. For those who don’t know, a Raspberry Pi is a small machine (credit card size) that can run Linux. It has an ARM chip, HDMI output, 2 USB inputs, SD card input and Ethernet cable input. You can buy one to create your own home video streamer… or you can use it to teach WebRTC.

The idea was simple:

  1. Get a vinyl distribution of a Linux operating system for Raspberry Pi
  2. Install it on the SD card (which is used to boot a Raspberry Pi)
  3. In the training itself, get and install Node, get and install a sample, run the sample

For the courtesy of those who came to the training, and those who just want to understand how simple this is, here are the step-by-step instructions I came up with for myself. I decided to do this on a Windows machine (just because it is what I have during the trainings to show the presentation).

1. Things to download and install

My assumption is that your machine has an SD card reader/writer. Most laptops these days do (mine has one). You will have to have a few tools on your Windows machine to get things going:

  1. SD Formatter – to format the SD card when needed and before first use
  2. Win32 Disk Imager – to write the OS image to the SD card
  3. Nmap and Zenmap – to locate your Raspberry Pi on the network
  4. Bitvise SSH Client – or any other SSH client for Windows (not needed on Linux)

Install all of the above.

2. Get a Raspberry Pi operating system

There are several options for operating systems on a Raspberry Pi. I ended up selecting the “default” one. Go to http://www.raspberrypi.org/downloads/ and select the one you want. I chose to go with Raspbian (Debian ported to Raspberry Pi). Also called on that size Debian Wheezy. The version I used was January 2014 with the release date 2014-01-07.

3. Prepare the SD card

This step is long, so I do it prior to the training, and I do it before each and every training – just to show that I am starting from a vinyl system. Format the SD card To prepare the card, we need to format it and install the image on it: Run SDFormatter, and select Quick Format with no size adjustments.

SDFormatter

Once done, it is time to write the image into it. Write the OS image Unzip the Raspbian image file you downloaded. Mine was called 2014-01-07-wheezy-raspbian.img with a size of ~2.8Mb. Run Win32 Disk Imager, select the image file and write it to the SD card. This is going to take a few minutes.

SDWrite

4. Find your Raspberry Pi on the network

Plug the SD card into your Raspberry Pi, connect it to the power and the network with the Ethernet cable. Since I don’t usually have extra display and keyboard in these sessions, my only option to login to the Raspberry Pi is over the network. For that, I am using Zenmap in order to find out the IP address the Raspberry Pi got. Run Zenmap. Write in the target your subnet. For me, that’s 10.0.0.* Switch the profile to Quick scan Count to 10. Look at the results and search for a (Raspberry Pi Foundation) MAC address. That’s going to have the IP address of your Raspberry Pi. Here’s how it looks for me:

Zenmap in action

Now use your SSH client to login to this IP address. The default username is pi and the default password to go along with it is raspberry.

5. Install Node.js on Raspberry Pi

Logged in? Good. Node.js doesn’t come preinstalled on a Raspberry Pi, so we need it there. Googling a bit, got me to this set of instructions. What I need is:

  1. Node.js
  2. Websocket support for Node.js

Run the following from the command line of your Raspberry Pi:

wget http://node-arm.herokuapp.com/node_latest_armhf.deb
sudo dpkg -i node_latest_armhf.deb
npm install websocket

The last one installs Websocket support.

6. Get your preferred WebRTC sample

For me, that would be Rob Manson’s Getting Started With WebRTC. This code is explained in detail in his book under the same name which I reviewed here already. Since this is on Github, we follow these instructions:

git clone git://github.com/buildar/getting_started_with_webrtc
cd getting_started_with_webrtc

 7. Prepare the sample

Rob’s sample requires one additional step. The Node.js code sends a .html file to the browser, and in it, there’s an indication where the Websocket signaling need to go to. In the HTML file, that’s set to “localhost”. I had to change that value to the IP address of my Raspberry Pi. For that, just use:

Looking for a WebRTC sample? There’s a GitHub repo for that(Opens in a new browser tab)

nano video_call_with_chat_and_file_sharing.html

Change in line 149 from “ws://localhost:1234” to “ws//<ip-address>:1234”

8. Showoff

Start the server by calling:

node webrtc_signal_server.js

Connect 2 browsers to the IP address of your Raspberry Pi at port 1234, or 2 tabs from the same browser. For this sample, you will need to connect the first browser, and then copy the URL provided on the screen to the second browser to join the session.

Why does this matter?

Before WebRTC, getting such a thing to work from source to running a demo took a day or a week.

Now it is down to 5 minutes.

If you wanted disruption, then here you have it: WebRTC is all about reducing the barrier of entry for developers.

It can’t get any simpler than this hello world sample.

Need to know where WebRTC is available? Download this free WebRTC Device Cheat Sheet.


You may also like

Comment​

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

  1. I have been running proto-WebRTC app on Raspi-like device (http://blog.enthinnai.com/2011/04/28/enthinnai-running-on-a-home-server/). This app has IM and voice capability by dynamically downloading a Java applet. It does even ICE with a TRN server built in. Once Raspi was released, the app was moved to it with no further modification. Of course recently we replaced the Java applet and are using WebRTC. A Raspi is running at my home and using it for my personal use.

    1. Sure. I make it a point to show this specific “use case” in training sessions I do.

      One thing to note here is that the media itself doesn’t get passed or processed through the Raspberry Pi – that is done solely in the browsers.

      1. what browser do you use on the Raspberry PI?

        another question would this be possible as well to connect to other webRTC site like vline or similar service?

        thanks it is very interesting i am just now getting around playing with my Pi

        1. I think I didn’t make myself clear enough here.

          The Raspberry Pi acts as the server itself. It deals with the signaling path only. The browsers run on a regular PC and connect to the web server that I run on the Raspberry Pi.

  2. Hi ,
    Many thanks for your nice post.I had a video chat within my home network successfully. But how can i have video chat over internet also in the same manner ?
    It would be cool to have video chat with friends over internet. 🙂

    1. Saurav,

      For that you will first need to place your Raspberry Pi itself on the public internet, or just run the same code on AWS or any other cloud service out there.
      You might also need to use a TURN server, depending on the network topology between you and your friend’d equipment.

  3. Hi,

    Quick question…I want to be able to connect to this from the Internet. I have port forwarding on my router already set up and working. Where I specify the IP address for ws//:1234, do I specify the Raspberry Pi’s local private IP? Or do I put my Internet public IP?

      1. Hi,

        Thanks for the quick reply. I tried using my RPi’s local IP and it didn’t seem to work. So, I put my public IP and it worked. It also works using my domain name.

        Also, I was able to modify things a bit to make this work using HTTPS for added security.

        In the webrtc_signal_server.js:

        Change:
        var http = require(“http”);
        to
        var https = require(“https”);

        In the general variables section ADD:
        var options = {
        key: fs.readFileSync(“/path/to/private.key”),
        cert: fs.readFileSync(“/path/to/certificate.crt”)
        };

        This is assuming that you already have a key and cert pair ready. For those who are reading that do not have SSL keys and certs, do a search on how to create self-signed SSL. It’s relatively easy.

        Change:
        var http_server = http.createServer(function(request, response)
        to
        var http_server = https.createServer(options, function(request, response)

        In line 149 of video_call_with_chat_and_file_sharing.html, change the ws:// to wss://

        You will need to run the server under the user that is able to read the private key.

        Hope this helps if anyone is interested.

  4. appreciate the effort made here, even i can install a WebRTC server now, WOW !

    So i got it working allright locally behind the home gateway, but not from the internet. Mmm, portforwarding did not bring the success i had hoped for though. Do i have to change settings somewhere else?

    i would like to name the session to a roomnme of my choice, does anybody have a sample for that?

    thx,
    richard

    1. You’ll need a public IP for your signaling server if you wish to be able to route calls to it. You may also need to connect to an external TURN server.

      The idea of this post was just to show that it is easy to get this up and running. Production is a bit harder 🙂

  5. Thanks for the tutorial.
    It worked first try 🙂

    One hint for step 5:
    `npm install websocket`
    creates a folder “node_modules” in the current folder. Would be better to `cd getting_started_with_webrtc` first.

    When you are in the `getting_started_with_webrtc` folder you can also run `npm install`
    witch will install all dependences for you. So you don’t need the “manualy” install the websocket module.

  6. Thanks for the post. The code works perfectly. Is it possible to add few lines of code in the existing code you can give so that, the raspberry pi also joins the webrtc session. So that someone sitting infront of the raspberry pi with the microphone, speakers and camera attached to raspberry pi can also join the Chat?

  7. I think I follow this very carefully but in the end when I:

    pi@raspberrypi ~ $ node webrtc_signal_server.js

    I get:
    node: /usr/lib/arm-linux-gnueabihf/libstdc++.so.6: version `GLIBCXX_3.4.20′ not found (required by node)
    node: /lib/arm-linux-gnueabihf/libc.so.6: version `GLIBC_2.16′ not found (required by node)

    I did:

    sudo apt-get install libstdc++6

    and got:
    libstdc++6 is already the newest version.

    What to do???
    Thanks

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