Why JavaScript and the Internet of Things?

Share this article

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.

Patrick CatanzaritiPatrick Catanzariti
View Author

PatCat is the founder of Dev Diner, a site that explores developing for emerging tech such as virtual and augmented reality, the Internet of Things, artificial intelligence and wearables. He is a SitePoint contributing editor for emerging tech, an instructor at SitePoint Premium and O'Reilly, a Meta Pioneer and freelance developer who loves every opportunity to tinker with something new in a tech demo.

ColinIEmerging TechInternet-of-Thingsiot
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week