Take the 2-minute tour ×
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

Reading a book on Single Page Web Applications I came to a paragraph that got me thinking:

Node.js is non-blocking and event-driven. In a nutshell, this means a single Node.js instance on modest hardware can handle tens or hundreds of thousands of concurrent open connections, such as those used in real-time messaging, which is often a highly desired feature of modern SPAs.

I noticed the case of using the Raspberry Pi as a Rails server, so how about for Node.js?

How do I setup my Raspberry Pi to serve a Node.js application?
Did anyone try, are there tips & tricks, maybe gotchas or limitation to consider?


Edit: To avoid misunderstandings or off-topics, let's please keep the focus on the Raspberry Pi, in the Node.js context:

  1. How suited is the Raspberry Pi to serve Node applications?
  2. If that is the case, how can one fine tune the Raspberry Pi for best results?
share|improve this question
    
Because the book is all about Single page application, node.js had to make an appearance there. Yes it is possible to serve everything up with node. But I doubt it will ever be done in any production environment as it can get very complex and unfriendly –  ppumkin Jan 23 '13 at 16:35
    
It is weird how you updated you answer to ask 2 specific questions to avoid confusion and then mark an answer for installing node.js- which was not the question? You original question was, how to set it up and any advice. Why did I even bother. LOL :) –  ppumkin Jan 24 '13 at 8:39
1  
Looks like when the choice was made you were still editing; choosing the answer can be edited just like the answers, so thanks for pointing that out. :) –  Marius Butuc Jan 24 '13 at 13:35
add comment

2 Answers 2

up vote 11 down vote accepted

Getting Node.js on a Rasperry Pi

You can either:

  1. Compile Node.js yourself (as ppumkin already pointed out)—takes about 2 hours on a Raspberry Pi.
  2. Or you can download the binary v0.8.17

Performance

I did a quick performance test (to give a rough first impression):

  1. My Raspberry Pi is overclocked (Turbo) with default memory_split (64)

  2. Tests were performed over my local network (802.11g Wifi).

  3. I used the standard "Hello World" example from the Node.js website:

    var http = require('http');
    http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello World\n');
    }).listen(1337, '127.0.0.1');
    console.log('Server running at http://127.0.0.1:1337/');
    
  4. Apache Bench settings: ab -r -n 10000 -c 100 http://192.168.0.116:1337/

So these tests are not representative for a normal web application (both concerning the network connection and the length/complexity of the transferred content).

Results

Server Software:        node.js/0.8.17
Server Hostname:        192.168.0.116
Server Port:            1337

Document Path:          /
Document Length:        12 bytes

Concurrency Level:      100
Time taken for tests:   53.824 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      1130791 bytes
HTML transferred:       120084 bytes
Requests per second:    185.79 [#/sec] (mean)
Time per request:       538.238 [ms] (mean)
Time per request:       5.382 [ms] (mean, across all concurrent requests)
Transfer rate:          20.52 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        2  178 405.2     40    4975
Processing:     7  342 1136.4     50   31533
Waiting:        6  274 1047.6     48   31533
Total:         11  520 1238.7     94   31581

Percentage of the requests served within a certain time (ms)
  50%     94
  66%    112
  75%    303
  80%    714
  90%   1491
  95%   2499
  98%   3722
  99%   5040
 100%  31581 (longest request)

For a comparison, I also installed nginx on my Raspberry Pi and ran the same test with the default "Welcome to nginx!" HTML file:

Server Software:        nginx/1.2.1
Server Hostname:        192.168.0.116
Server Port:            80

Document Path:          /
Document Length:        151 bytes

Concurrency Level:      100
Time taken for tests:   46.959 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      3610361 bytes
HTML transferred:       1510151 bytes
Requests per second:    212.95 [#/sec] (mean)
Time per request:       469.590 [ms] (mean)
Time per request:       4.696 [ms] (mean, across all concurrent requests)
Transfer rate:          75.08 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        2  162 407.8     40    4999
Processing:     5  256 979.8     45   29130
Waiting:        5  256 979.8     45   29130
Total:         32  418 1078.6     88   30477

Percentage of the requests served within a certain time (ms)
  50%     88
  66%     97
  75%    105
  80%    258
  90%   1064
  95%   2382
  98%   3412
  99%   4145
 100%  30477 (longest request)

Optimizing Raspberry Pi settings

Use raspi-config to change the following settings:

  1. Set the memory_split for the GPU to 16 (lowest value)
  2. Set overclocking mode to "Turbo" for fastest RAM/CPU settings
share|improve this answer
    
Yes, finally! Thanks for the link. –  Steven Devijver Jan 23 '13 at 13:59
add comment

Web Servers

Node.JS can be used as a web server replacement on the Pi and you can create stand alone or Single Page web applications with ease.

But just for your information, in most real world applications it is recommended to use servers like the modern nginx , light weight lighttpd or the chunky but fully featured apache2! And then script node.js to complement the site.

Obviously the possibilities are endless and everything depends on what you would like to achieve.

Raspberry Pi?

The Raspberry Pi can run any of those web servers. It can also run Node without any serious complications and is really fast without any complicated tweaking.

The Raspberry Pi is very capable but best would be to turn the memory split to least graphics and most RAM. Forget about using an IDE and just do everything via SSH. If you really need some more juice then put a heat sink on the BCM chip and overclock it as you feel safe. Another option would be to use multiple Pi's as a cluster to help with load balancing. You can start digging around here about clustering.

But do you really need to use node.js?

Node.JS was intended to be used when you start (or anticipate) to get hundreds and thousands of request that require small chunks of data to be stored to DB, cached or read back with minimal server overhead. So you drive it using JS on the client but Node.JS is actually driven by C/C++. So what happens if you need custom module or a specific change in the base code?

In a application that serves webpages node.js does not usually outperform apache for example, on single requests. The non-blocking feature of node.js is great if you have thousands of request per second for most of the day, this is where apache would block and crash.

A real world example

Ebay - During an auction when you have a count down of the last 30 seconds. You can have several people that used to refresh the page vigorously and increasing bids. This is where node.js shines, because today you need NOT refresh any more. That is because the JS ajaxes to node.js very often (300ms~600ms) from all clients and is able to provide a "real life auction" experience. Ebay does not run solely off node.js but on very complex load balanced server farms.

To build and install Node.js on the Pi*:

Obviously there is nothing wrong with using node.js instead of others and how best to learn node if not on a neat little device like the Pi. So you can compile the code yourself like this.

$ sudo apt-get install git-core build-essential libssl-dev 
$ mkdir ~/nodeDL && cd ~/nodeDL 
$ git clone https://github.com/joyent/node.git
$ git checkout v0.6.15 (to checkout the most recent stable version at time of writing)

update: later versions of node (current version is v0.8.18) can be built without the special steps below

next we need to tell the compiler to use the armv6 architecture for the compilation:

$ export CCFLAGS='-march=armv6'
$ export CXXFLAGS='-march=armv6'
and then edit deps/v8/SConstruct around the line 82 mark, to add “-march=armv6”:
'all': {
   'CCFLAGS':      ['$DIALECTFLAGS', '$WARNINGFLAGS', '-march=armv6'],
   'CXXFLAGS':     ['-fno-rtti', '-fno-exceptions', '-march=armv6'],
 },

Then comment out lines starting around the 157 mark, to remove the vfp3 and simulator parts. Since this is a JSON-like object, remember to remove the comma on the CPPDEFINES line!

'armeabi:softfp' : {
   'CPPDEFINES' : ['USE_EABI_HARDFLOAT=0']
  # 'vfp3:on': {
  #   'CPPDEFINES' : ['CAN_USE_VFP_INSTRUCTIONS']
  # },
  # 'simulator:none': {
  #   'CCFLAGS':     ['-mfloat-abi=softfp'],
  # }
 },

Then the usual configure, make, make install process, NB I had to manually specify the location of the OpenSSL libpath:

$ ./configure --openssl-libpath=/usr/lib/ssl 
$ make (to compile node (This took 103 minutes!))
$ sudo make install 

Thats it, you should now have a working Node.JS install!

$ node -v should show you the version number
$ npm -v should show you the version of the Node Package Manager

* References and original article

But as pointed out in other answers you can simply download the a pre compiled binary that will just work.

Conclusions

A good piece of Pi isn't bad. You can run just about anything on the Pi- Just don't expect production level performance.

share|improve this answer
2  
Well ... you are right in that you most probably want to combine Node.js with an additional "front-end" web server such as Nginx. But what do you mean by "you NEED a C/C++ developer"? As long as you do not want to work on Node.js core or write platform-dependent modules, you do not need C/C++ at all. JavaScript is enough for the common Node.js app developer. Where did I get you wrong? –  Golo Roden Jan 23 '13 at 19:42
    
All I meant by that was that node.js is written in C/C++ - When I was doing research on node.js I came across many sites that demonstrated how to expand on the library. But that required pure knowledge in C/C++ - For most purposes you won't need to - But if you ever land up in a situation like that then node.js is the wrong solution. As it happened to be in my case. –  ppumkin Jan 23 '13 at 19:48
    
My question is Raspberry Pi-focused --- How suited is Raspberry Pi to serve Node applications? If that is the case, how can I fine tune the RPi for best results? --- and not Node-focused --- How good or bad is Node? But thanks for your opinion; I'll edit the initial question to make it more clear. –  Marius Butuc Jan 23 '13 at 20:16
    
Yea I answered question 1- The Pi can handle node.js plus a full LAMP stack too! How to fine tune it? That is open to discussion. Please be more specific what parameters you want to fine tune? I also expanded on what I feel can help with performance. –  ppumkin Jan 23 '13 at 21:01
1  
I will upvote if you merge your two answers into this one. –  Jivings Jan 23 '13 at 21:05
show 7 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.