Node.js for Embedded Systems

This post is an excerpt of the Chapter 3 of “Node.js for the Web of Things” in “Building the Web of Things“, a Manning book by Dominique Guinard and Vlad Trifa.

Long gone are the days when JavaScript was solely used to animate banners on a web page! Thanks to its widespread support by virtually all web browsers, relative ease of use, and flexibility, JavaScript has become the de-facto solution for writing dynamic, client-side applications.

Developers usually love or hate JavaScript for various good (or less good) reasons but overall it is safe to say that it isn’t safe anymore to ignore JavaScript! According to the number of public repositories on GitHub, it has also become one of the most popular programming languages ever, with a community of developers growing faster than any other.
This popularity in parts linked to the fact that a few years back (in 2009) JavaScript entered a new space: the space of server side applications in the form of Node.js or aka Node. Node provides an event-driven architecture and a non-blocking I/O API that optimizes an application’s throughput and scalability. This model is commonly used to design high-performance Data-Intensive Real-Time (DIRT) applications. Node basically took the Web by a storm with a simple core principle moving out of the one-request-one-thread model of most Web servers back them (Apache, older Tomcat, etc.):


to a system where all requests are served by the same thread but parked until the I/O operations the request depends on have returned:


Okay well what does it have to do with the Internet of Things or the Web of Things?

Well it turns out that Node did not stop at the server. Over the past few years it also sneaked into the very core of Things and devices! In a world massively dominated by devices running low-level C programs, JavaScript and Node.js have managed to stand out as a viable and easy-to-use alternative for powering all kinds of things from robots to wireless sensor nodes. Indeed, a number of embedded device platforms today directly support JavaScript and Node.js to write embedded code. This is the case for most Linux-based platforms such as the Raspberry Pi, the Intel Edison, or BeagleBoards, but also for some low-power platforms such as the Tessel or Espruino.

“When you have a hammer, everything looks like a nail!” we hear you say! Not exactly: we don’t advocate using JavaScript and Node.js for every IoT implementation. We’d rather compare JavaScript and Node.js to the Swiss army knife of modern IoT and WoT development than to a hammer. It certainly isn’t the optimal solution for every IoT project we can think of, but it’s quite good at a number of things!

An embedded application requiring absolutely predictable and real-time performance (for example, the code running in a high-speed train) is better off being written in a low-level language such as C. Moreover, JavaScript as a language is often criticized by its detractors for its lack of static typing and a plethora of different programming patterns and styles leading to code that’s sometimes harder to maintain, such as for very big projects involving a large number of people. Nevertheless, its ubiquity, portability, asynchronous event-driven model, along with a large and vibrant online community make it a solid candidate to consider seriously, especially when using Node.js to build scalable and real-time web systems, but also increasingly more frequently for the blazing-fast development of hardware projects.

You can see this in a number of recent Node.js projects from programming robots with Johny-Five and Cylon.js, to creating physical mashups with Node-red and EVRYTHNG or elegantly and efficiently manage GPIOs with onoff.

The recent domination of Node for the IoT does not stop at libraries: new embedded systems are now built with Node and JavaScript as their primary development platform. This is the case of the Samsung Artik platform, the Intel Edison, the BeagleBone black, the Tessel or the Espurino. These great developments show that Node for embedded systems isn’t simply a short living trend: like it or not it is here to stay and makes embedded devices more accessible by bringing them one step closer to the Web!

This is why we decided to adopt Node and JavaScript as the main language for our book. You can already check all the Node and JavaScript examples of the book on our GitHub repository and should this be not enough to convince you getting the book then check webofthings.js, a sample Web of Things server for the Pi, Beaglebone or Edison that you’ll learn how to build step by step in Building the Web of Things.