The smartest way to program smart things: Node.js
The reasons to use Node.js for hardware are simple: it’s standardized, event driven, and has very high productivity.
![](https://www.oreilly.com/content/wp-content/uploads/sites/2/2019/06/nodejs-1440x900-3dbf6cb348d5de2954455ad428061072.jpg)
Node.js is on the rise for programming hardware. The full Google V8 version helps run Intel’s Edison chip. The IoT community has already embraced Node.js for embedded devices and robotics, with notable examples including Nodebots and Cylon. And now, even smaller devices like Tessel 2 — a development platform for prototyping hardware — are using JavaScript.
Why is this a big deal? It makes programming hardware much simpler — college students can learn Node.js in a weekend. And it makes it possible to build and program an entire IoT device, from start to finish, in less than four hours. This may very well be the future of hardware programming.
Intel principal engineer Michael McCool will be at O’Reilly’s Solid Conference, June 23-25, 2015, to lead a workshop on using Node.js and HTML5 to program the Internet of Things. “In only three and a half hours, we’re going to walk people through building a complete and sophisticated IoT system,” McCool told me in an interview. That includes building a hardware prototype, hardware interfacing, streaming telemetry, building a UI on the phone, and creating an app. “The Web server part is just five lines of code. The rest of it is similarly simple,” he said. “The complete code is only about 200 lines on the embedded device, plus a little bit more…when you add in graphs of things for streaming data.”
The reasons to use Node.js for hardware are simple: it’s standardized, event driven, and has a very high productivity. And as importantly, said McCool, it’s dynamically typed, which makes it faster to write — perfectly suited for getting a hardware prototype out the door. “With JavaScript, you can build a prototype rapidly, get it in front of users to get feedback, and iterate. It’s also robust enough that you can take it all the way to product,” he explained. “And if you’re concerned about time to market and you want to get a lot of features implemented in a short period of time, then go with Node.js.”
One of the other strengths of Node.js is its huge library of packages, more than 132,000 and growing. Last September, when McCool last taught his course, it was 74,000. Not all of them work for small devices, but they are easy to install with one command. And Node.js also supports a number of other IoT-specific communication systems, such as MQTT, which is designed for fast status updates from small devices.
That said, building in Node.js does require specialized knowledge about how to connect to the Web and other IoT devices. But there are some simple tools that can help. McCool pointed to StackOverflow to help you get started. John Resig’s Stream Playground blog also is a valuable resource for finding well-documented streaming node modules you (mostly) chain together plug-and-play for programming. And two of McCool’s favorite tricks are Browserify and Ecstatic — simple Web server packages that allow you to build in Node.js and turn it into Web content. Browserify can convert many Node.js programs and tools so they run inside the browser’s version of JavaScript. The Ecstatic package makes it possible to set up a simple Web server quickly, using only a few lines of code. “Using any of these packages, you can build a Web server and/or a Web service,” said McCool. “And of course, you can also use Node.js for server-side code.”
While some experts point out that Java is still a viable option for IoT programming, McCool said that JavaScript makes programming way simpler. The reasons have to do with both politics and portability. “Apple discourages use of standard Java implementations in iOS for various reasons, so relatively complex pre-compilation approaches are needed to use it,” he said. “But the fact is, you don’t really need Java.” The set of available libraries in JavaScript is equally rich. JavaScript programs run on embedded devices, servers, mobile devices, and desktops. And they are under Windows, Linux, Android, and iOS.
In short: for building a complete end-to-end IoT system, JavaScript is the most portable programming system available.
“Suffice it to say that the IoT is a huge mashup of Web services, browser technology, and embedded technology,” said McCool. “JavaScript is pretty useful in all those places.”
Cropped image by PublicDomainImages on Pixabay, used under a CC0 Public Domain license.