Node.js at Road Rules

January 18, 2016 - by Dave Jensen, Founder, Road Rules

Road Rules is a member of Joyent's Node.js Innovator Program, Class of 2015. Guest blogger Dave Jensen is the founder of Road Rules, which provides the most interactive way to stop texting while driving and is written primarily in Node.js. Dave explains why Road Rules selected Node.js and how they leverage the runtime for their innovative new app.

My first exposure to JavaScript on the server side was in 1997 and 1998, when I worked as an intern at Netscape. I built and maintained an internal project management tool using SSJS on Netscape Enterprise Server. Despite all of the problems with Netscape SSJS, including lack of async support, I discovered the advantage of using JavaScript on both the front end and the back end. In the many years to follow, especially as I became a more experienced JavaScript developer, I would search the internet for SSJS – finding nothing. In the summer of 2012, I started participating in hackathons and discovered Node.js. Even though the first version of Node.js had been released three years prior, I was still very excited to finally have JavaScript on the server-side. For the next few years, all of my hackathon projects were Node.js based. And when it came time to decide on the technology stack for Road Rules, the choice for the back-end was obvious: Node.js.

Benefits of Node.js

It wasn’t just nostalgia that led to the decision to use Node.js. Several benefits factored into the decision to use Node.js.

JavaScript is all over the web. JavaScript beat out a number of browser-side languages and technologies to be the “language of the web” and as Mikeal Rogers puts it, “JavaScript succeeded because it made what those programmers did accessible to people without their background.” Now we see JavaScript on the server-side, in the internet of things, databases, robotics, and more.

Performance. There are two primary reasons why Node.js is fast. First, it uses the Chrome V8 javascript engine. Second, and more importantly, is the event loop. Node.js performs I/O non-blocking and asynchronously. Instead of blocking parallel threads, a task is sent to the event loop with a callback and continues to execute. When is finishes the async task, the callback is invoked. This approach uses less memory, is usually easier to program, and is particularly fast for I/O operations.

NPM. NPM is the package manager for Node.js and is one of the reasons for the popularity and growth of Node.js. NPM makes adding libraries and third party modules super easy, handling all of the dependencies for you. On top of that, as of this writing, there are 225k modules in NPM with 2.5B downloads per month!

Tools. There are Node.js projects that range from small simple libraries to full blown applications. Not only can you run your entire back end on node.js but also you can run other aspects of your engineering operations and online presence with Node.js based software. There are basic libraries like Lodash for various utility functions or Async to help with control flow in your asynchronous code. There are drivers for working with all the SQL and NOSQL databases. There are web frameworks like Express or HapiJS. There are two popular tools for build/task runners, Grunt and Gulp. For testing, there are a few great frameworks such as Mocha, Jasmine, and Lab, which includes code coverage. A great tool for managing your Node.js processes is PM2. You can run a message broker with MQTT using Mosca. If you need a continuous integration/delivery server, then there is StriderCD. You can even use HarpJS for static website generation, Ghost for blogging, and NodeBB for community forums.

A large, active community. The size and usage of NPM gives you an idea of the size of the Node.js community. There are numerous resources to learn Node.js including 105k questions on StackOverfow. Personally I’ve had good experiences working submitting issues and pull requests on Github for Node.js based projects. For whatever reason, most Node.js project owners are quite responsive. There are tons of blogs posts, thousands of open source modules, dozens of books, many jobs, active IRC channels, several Meetup groups, conferences, and even a few consulting firms dedicated to Node.js. There’s even an Arduino hardware project, called Tessel, which runs using JavaScript and Node.js.

How Road Rules uses Node.js

Road Rules is an Android app (iOS coming later) that helps eliminate distracted driving by assisting you while you drive. For instance, we detect when you’re driving (using only your phone) and automatically respond to text messages on your behalf.

So how does an native Android app use Node.js? Node.js runs our entire server-side infrastructure. We’re using Restify, but are transitioning to HapiJS, to power the API used by the Android app. The API server communicates directly to MongoDB using the Mongoose ODM as well as Redis, which we use as a time cache. We will, however, be moving to Couchbase using Ottoman in the near future. We are also planning on introducing a microservice platform such as Seneca. The Android app uses MQTT to stream data, such as data for location sharing, and we use Mosca as our message broker and small modules that do work on the incoming data. The front end web application is a separate service from the REST API service and is built with HapiJS. Finally, we also have another small standalone application that powers web based location sharing, which is used in conjunction with the Android app.

One of the more powerful concepts in Node.js is small, maintainable modules. Even though it seems like a lot to keep track of, it’s working out well with our small engineering team. Each small component is easier to test as an individual part. Changes to a module or service won’t cause regressions in other unrelated modules.

When I first started with Node.js I was just happy to have JavaScript on the server with a powerful package manager. Now it seems like Node.js is everywhere and the possibilities are endless. Feel free to contact me on Twitter @djensen47, if you have any questions about how we're using Node.js at Road Rules. And if you’re on Android and are interested in trying out our anti-distracted driving assistant app, please sign up for our beta.