Earlier this year we published a demo app showcasing our port of Node.js with ChakraCore to iOS. In the accompanying blog article, we talked about how Node-ChakraCore opened a unique opportunity for Node.js to work on iOS, and consequently a path for Node.js to become a viable building block for mobile applications.
The engineering team at Janea Systems has continued to work hard and passionately on this project; so today, we are excited to announce the first alpha release of Node.js for Mobile Apps: a toolkit that enables developers to harvest the power of Node.js and its module ecosystem in their mobile applications. At the heart of it, CST is a library — available for Android and iOS — that allows an app to host a full-cST execution environment. In addition to the core library, We have released plugins for Cordova and React Native — both also compatible with Android and iOS — which make it extremely easy to integrate the Node.js runtime into applications built using on those frameworks.
The project is still at an early and experimental stage, but you can start building your apps with it today.
And like most best things in a developer’s life, Node.js for Mobile Apps is free and open source.
The power of Node.js inside your mobile apps
With Node.js for Mobile Apps, you get a Node.js execution environment running in a background thread inside your app. This environment is well suited for business logic, advanced networking, and background processing, but it can pretty much run any code that can be executed asynchronously from the UI, while leaving synchronous UI handling to the main application thread.
To facilitate communication between the Node.js code and other parts of the app, we have implemented a high-performance messaging channel that can be used to send data back and forth. It is a low-level primitive that is effective in and of itself, but could also be used in the future to build higher-level abstractions on top of it, like cross-thread method invocations and callbacks.
For example, in a React Native app you can do:
import nodejs from 'nodejs-mobile-react-native'; . nodejs.start(); nodejs.channel.addListener('message', (msg) => { alert('Received from node: ' + msg); }, this ); . <Button title="Send something" onPress={() => nodejs.channel.send('something')} />Copy the code
JavaScript code can also make direct synchronous calls into native code (Java, Objective C, Swift) and get calls back from it, by means of Node’s native add-on APIs (the V8 API and N-API are both available).
And of course, You can use modules. We have done a good amount of testing with PouchAlbum — our demo app — which includes more than 300 Modules. So even though there’s more work to be done to fully assess and ensure compatibility of the module ecosystem, there are reasons to be happy about the current state of affairs.
A highly-compatible Node.js engine under the hood
Today’s release of Node.js for Mobile Apps is based on source code from Node.js v8.4.0.
The Android version of The core library uses The V8 JavaScript engine — much like regular Node. So it is very similar to a Linux build of Node.js, but with a few platform-specific tweaks and fixes.
On iOS, V8 cannot run because the operating system forbids just-in-time compilation; so instead of V8, we used our own port of the ChakraCore engine, on top of the integration with Node that Microsoft created in Node.js on ChakraCore. ChakraCore has a well-optimized, pure interpreter mode which complies with iOS’ restrictions.
On both platforms, the result is a runtime that ensures the highest level of compatibility with Node.js.
Built for the community
At Janea Systems, we love Node.js. We have been involved with the project and its community for many years. We have been contributing to the Windows version since its early days, and have been participating in several committees, working groups and efforts at the Node.js Foundation.
With this project, we wanted to create a space where we can experiment and quickly iterate on the features that are specifically needed for Node.js as a building block for mobile applications. So, while this is technically a fork on GitHub, it is absolutely not in our intention to divide the Node.js community or antagonize the Node.js project. On the contrary, we hope that this effort will help Node.js become even stronger and more ubiquitous.
We anticipate that developers will build amazing things with these tools, far beyond what we can imagine today. So we invite you to get involved. Try the toolkit. Provide feedback, report bugs, suggest improvements, contribute code. Go build amazing new apps and radically new frameworks with it!
We can’t wait to see what you will come up with. And We look forward to working with the community towards making mobile app development more accessible, productive and fun.
To get started, visit the Node.js for Mobile Apps website.