Over the last few months, Janea Systems has been working on a port of Node.js with ChakraCore to iOS. Now that things appear to be working well, we have created a demo app that showcases this work, and we’d like the feedback of the developer community to help us shape the future of the project.
UPDATE [Oct 3rd, 2017]: The work described in this article has culminated in the release of Node.js for Mobile Apps: a full-fledged Node.js runtime for Android and iOS. Read the announcement.
One of the many strengths that Node.js can claim under its belt is its ubiquity. From server to desktop to IoT, the Node.js runtime is available and generally well-supported on an impressive list of operating systems and architectures.
Two major platforms, however, are conspicuously absent from the list: iOS and Android.
Why is that?
The reasons are both historical and technical. For one, Node.js was originally designed as a server platform, with assumptions that don’t always fit the app model and the unique challenges of mobile operating systems. But this reason alone would not have prevented developers from at least experimenting with Node.js on mobile. In fact, there have been a few reports already of people running Node.js on Android.
To be fair, somebody did manage to make Node work on iOS. JXcore, a fork of Node based on version 0.10, added features specifically targeting mobile platforms and circumvented the iOS JIT restriction by adding the ability to use engines other than V8. By using SpiderMonkey in its interpreter mode, JXcore was able to run on iOS. JXcore also added other features and tooling to make Node fit well with mobile environments. Unfortunately in March 2016, Nubisa – the company behind JXcore – announced that they would no longer be working on the project. JXcore has made massive changes since the fork and meanwhile, development of mainline Node.js has continued at a dramatic pace; the two codebases have diverged so much that reconciling them is no longer practical and with JXcore still stuck at v0.10, it is clear that the project has reached a dead end.
Fortunately, other relevant efforts have emerged. Microsoft published Node-ChakraCore, a friendly fork of Node which adds support for optionally using the ChakraCore engine instead of V8. Rather than modifying the Node source code to use a different engine, as JXcore did, they wrote a shim on top of ChakraCore which essentially emulates the V8 API. Thanks to this approach, the fork is much easier to keep in sync with the parent project; the two happily coexist under the Node.js Foundation and will likely be merged at some point.
Node-ChakraCore opens an opportunity for Node and iOS because ChakraCore has a well-optimized, 100% JIT-free interpreter mode. Even though it doesn’t officially support iOS at the moment, it was recently ported to macOS, which is not too far off.
The Janea team has been involved with these technologies for a while. We have been supporting JXcore for the Thali project and we have been working closely with Microsoft on both the Windows port of Node.js and Node-ChakraCore. So a few months ago we decided to capitalize on the lessons learned from all those projects and started porting Node.js with ChakraCore to iOS.
We chose to focus on iOS first because we knew that it would be significantly more challenging than Android. With a little bit of effort, you can pretty much already run Node with V8 on Android (even though a lot more work needs to be done to make it an easy-to-use framework for mobile). It’s Node.js on iOS that truly breaks new grounds.
We have now reached a point where things seem to be working pretty well. Even though it’s still too early to publish a release, we wanted to at least share the news and show a demo.
The application allows the user to create photo albums and synchronize them with other users/devices. Synchronization happens between any number of devices and it works peer-to-peer, with no external server involved. Because of that, it only happens when the devices are on the same Wi-Fi network.
Node’s built-in dgram module is used for local network peer discovery, by means of UDP broadcast packets. The React Native portion of the app handles the UI. When a photo is added, it is stored in an in-memory database running in the Node layer, using PouchDB and MemDOWN. Back on the Node side, PouchDB handles the synchronization between devices. The REST API is provided by the express-pouchdb module, which starts a CouchDB compatible REST API on top of PouchDB, using the popular Express module. WebSockets is provided by the express-ws module, also on top of Express.
The demo source code is available on GitHub.
Where do we go from here?
So there are plenty of opportunities for future developments. At Janea, we intend to continue working towards making Node.js a powerful tool for iOS and Android development.
In the meantime, we would love to hear your feedback about all of this. How would you use Node.js on iOS and Android? What scenarios, features, or integrations would be most interesting to you? Please share your thoughts in the comments section.
For any further questions or suggestions, please don’t hesitate to get in touch. Also, don’t forget to subscribe to our social media channels and our newsletter, so that we can keep you posted about future developments.