JavaScript
Article

Why JavaScript and the Internet of Things?

By Patrick Catanzariti

JavaScript has proven itself worthy on both the client and server side world of web applications, but why does it have potential in the ever expanding Internet of Things (IoT)?

Over the past two years, I’ve covered the growing amount of technology that JavaScript developers can get involved with in my JavaScript Beyond the Web and JavaScript Beyond the Web in 2014 articles here at SitePoint, as well as in various presentations at conferences and meetups. There is a huge level of enthusiasm for JavaScript and the IoT which is great (nice to know I’m not alone in this passion), but many developers ask one very reasonable question – why JavaScript? What are the advantages of using JavaScript with the Internet of Things? I decided it’d be useful to explore this topic in a bit more detail and put a lot of my thoughts down in writing.

Much of the Internet already speaks JavaScript

The whole idea of the Internet of Things is a simple one. We want to connect a range of new devices to the Internet so they can speak to servers and devices around the world. While the next big IoT device that changes the world is likely to be something we don’t see coming (that’s just the nature of technology right?), we know the technology pillars that it is likely to stand upon – connecting devices to web APIs in new and unexpected ways to (hopefully) benefit society. What language can our web pages and web apps speak right now? JavaScript. It makes sense to extend the same standard platform to the Internet of Things, communicating to a larger range of devices using that same language.

We can share functionality

JavaScript’s ability to be everywhere is huge. When connecting up a network of devices to your server, having them all speak the same language makes life easier. If you’ve got a large number of different connected devices that all understand JavaScript and you’ve got a server running Node.js, you’ve reduced complexity and can share functions that do the similar functions across different devices. Say you have business logic in your app which you need to reuse for different devices and server responses. With JavaScript, you can reuse the same functions for calls to different devices with ease – build once and reuse.

Existing libraries, plugins, and APIs

JavaScript has a range of existing libraries, plugins, and APIs, many of which can be utilized in the Internet of Things. While the jQuery plugins of old aren’t the most reusable for non-client side code, there is an evolving world of npm modules in JavaScript that are reusable for a multitude of solutions on the client, server, and beyond.

JavaScript utility libraries like Underscore.js, lodash, traverse and Async can be perfect to use in a range of common situations across different devices. There are some very powerful functions in the above modules that would be useful to IoT projects.

Another example of a great JavaScript module that is perfect for IoT is Socket.io – a module providing real time event based communication across multiple devices.

Rather than needing to build a range of new libraries and plugins for some of this basic stuff, we can reuse and further develop existing solutions in use around the web today for entirely new implementations.

JavaScript is great at event driven apps

JavaScript is quite good at event driven applications. These are the sorts of applications in which each device listens for various events and responds when events occur that it cares about. Socket.io is great at this.

Node has the “event loop.” While it’s not really in the scope of this article to explain this in detail, it allows us to execute a number of tasks without needing to wait for each task to be finished before we move to the next one. We can receive and respond to events, then wait for a callback from each event that notifies us once it is complete. This means we can respond to events as they happen, juggling many tasks simultaneously as they come in. Multiple devices can also respond to the same events. This works nicely in the Internet of Things.

Let’s imagine you’ve got an Internet enabled home with a smart hot tub that’s filling up with water as you walk home. It’s a rather random example, but lets go with it! Your phone signals that you’re about five minutes away from arriving home. The tub sees this event and starts filling with water automatically. You feel like catching up on the latest episodes of Gotham too, so you open up a mobile app and select to download and play the latest episodes from the season on a big display above your bathroom’s hot tub when you arrive. Yes – this is a pretty fancy hot tub. The tub receives this event and starts to look up what episodes of the show you’ve watched.

Suddenly, you realize you forgot to pick up some milk on the way home. As with all joyous technological breakthroughs, your to-do app is still prone to user error. You had set the reminder to get milk for tomorrow by accident and have had to rely on that primitive human brain at the last minute! You turn back around to head to the nearest supermarket.

Your phone notices the change in direction and signals that you are no longer on your way home. The tub sees this event and stops filling up with water for now. Rather than waiting until it has retrieved the latest Gotham episode list for you before responding to this change, the tub can instantly deal with this request and stop filling up with water. Once the list of unseen episodes has been found, the tub then sees that we’ve got that list and downloads the next episode as you journey for some milk. Once you’re on your way back home again, your phone signals this once more. The tub sees this event and turns the water back on while still finishing its download. You get home with a hot tub ready and Gotham playing the moment you step into the bathroom.

JavaScript has matured as a language

There is a common misconception that JavaScript is still a messy language to work with. I disagree. It is entirely possible to build a very stable and maintainable codebase using modern day JavaScript practices. JavaScript is a language mature enough to be used for much more than simple client facing apps. We’ve already got JavaScript linters like JSHint to ensure code meets a certain standard and have JavaScript test frameworks like Mocha which have the potential to be extended to work with IoT systems too.

Data

Every language has its idiosyncrasies and working with different languages on different devices means dealing with these. Often you’ve got to convert the data into formats that the other languages require, then back again if passing it in the opposite direction. Some languages handle this better than others and there are usually documented ways to pass data across, so it’s not impossible to do. However wouldn’t it be nice if you didn’t need to worry too much about this? Imagine all of your devices understanding data types in the same way.

JavaScript is open

There’s no requirement for a JavaScript developer to use a particular IDE or one single operating system. No need to pay a yearly fee to publish applications in the language. No closed off mega corporation that is guarding the language and its development from within its darkest depths. JavaScript is an open standard that can be implemented using a variety of JavaScript engines. I really hope that the open standard of JavaScript will lead to many open IoT devices that can collaborate and build upon each other, keeping the ideals of the open web alive and well into a whole new era of the Internet.

JavaScript is a nicer choice for new developers

I think when it comes to the various options for getting developers involved with the Internet of Things, JavaScript has the lowest barriers to entry. It is an easier language for a beginner to understand, yet there is a whole realm of advanced ways to structure and grow a project into a truly scalable system.

An incredible community

The JavaScript community is an ever growing and very open one. New and exciting modules and techniques come out all the time, and the development community is continually evolving in very positive ways. I think this reason alone is a great one for why JavaScript is well suited to the constantly evolving technology in the Internet of Things.

There are some pretty darn talented JavaScript developers out there in that community who’ve worked with the language and its nuances for years. They’ve got expertise in setting up test driven, modular JavaScript applications. There are also many JavaScript developers who are very keen to improve and grow their skills too. I would love to see all of this talent try their hand at moving beyond the typical web and onto programming interactive experiences with new devices.

It has already begun

There are already JavaScript enabled IoT devices out there. See my JavaScript Beyond the Web and JavaScript Beyond the Web in 2014 articles for an overview of some of them.

Conclusion

As you can likely tell, I’m a pretty keen advocate for JavaScript’s growth in this area. There are many advantages to using JavaScript in the Internet of Things that I hope to see flourish in coming years.

These are all my own opinions, but I’d love to hear your thoughts too in the comments. Are there advantages I’ve missed? Things I could have said better? Have you had any experience with JS and the IoT so far that you’d like to share?

JavaScript has a large and passionate community that I hope will show that same excitement and passion, bringing their skillset to the Internet of Things in ways nobody will see coming.

Thanks to Phill Farrugia and Jeremy Brown for their thoughts when I put out the question on Twitter.

  • sebastiaan hilbers

    What about CPU heavy apps?

    • Patrick Catanzariti

      To be honest, I haven’t personally had to work on anything too CPU intensive yet. I would imagine that from the Node perspective, there are particular strengths to Node and I’ve heard if things get too CPU intensive, it’s not an ideal use case for it (or at the very least, requires a bit more fine tuning and finesse). I’d be keen to know if anyone else reading the article has had experience with the more CPU intensive apps. It may be a case similar to calebvear’s comment and my response above.

  • calebvear

    For less power constrained systems I can see it being a reasonable option, but I can’t see Javascript being a good fit for extremely low power IoT devices for a long time yet. Memory and performance constraints make fitting everything in difficult enough with native C/C++ code let along handling the extra overhead javascript has (and always will have).

    • Patrick Catanzariti

      Thanks your thoughts! I agree with your points, there are still areas where JavaScript isn’t feasible and native C/C++ is likely to work better for onboard code. Potentially in these situations though, I could still see JavaScript getting involved in supplementing the functionality by communicating to the device via API calls, rather than being installed on the device itself. Just a thought :)

  • Glenn

    You could apply these criteria to a lot of languages but the point I agreed with was a nicer language for beginners. But eventually these devices need to talk to a server which is still a part of the IoT ecosystem. With that said javascript is ill suited for parallel or concurrent processing.

  • seba

    Actually I don’t agree that it’s an easy language for beginners. Javascript allows a lot of different ways to do the same thing (eg. different ways to achieve object-oriented-like behavior). It also has some complex concepts for beginners (eg. different function context based on the invocation method). A beginner might not get his head around what’s the best way to do things and might get confused when ‘this’ refers to different stuff one time from the other. In my experience beginners have more trouble understanding the more advanced (yet important) javascript concepts then they have trouble thinking about references in C. And C has a major advantage on much of the IoT devices with limited hardware.

    Both the fact that it’s open and it has a large community is true for a lot of languages.

    The only thing I really agree upon is the ‘data’ aspect. AFAIK no other language has something like JSON is to Javascript.

    • Patrick Catanzariti

      That’s interesting to hear actually. My personal experience has been different, maybe it depends on the developer learning it. When starting out, I found JavaScript much cleaner and easier to learn and get started with than when I learnt C. I’ve heard similar things from other developers.

      The openness and community bits definitely could be true for other languages too :)

      You have phrased the data bit even better than I did in my article. JSON is wonderful and it gets a bit messier to implement in languages like C (though definitely doable).

Recommended

Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Get the latest in JavaScript, once a week, for free.