The asynchronous architecture of Node.js I/O operations means that all the operations requiring some type of communication with external systems (e.g. databases, file systems, etc) are non-blocking. Basically, an app that you just logged into continues to run while waiting for its thread or task (authentication in this case) to complete. That is unless it has been programmed to do otherwise, say login users in a synchronous process that deliberately blocks the main thread. Rather than taking one “order” and going off to do its thing, Node.js is able to take multiple “orders” or complete multiple tasks concurrently in an asynchronous fashion.
Here’s another trivial example: Let’s say you have to read a couple of large files, each taking 5 seconds to read. If you use Node.js, reading both files might take less than 10 seconds, as both operations will run concurrently.
Native Node.js for Mobile
Since V8 doesn’t run on iOS due to the OS restricting Just-in-time compilation, Janea Systems substituted their own port of the ChakraCore engine, integrated using what Microsoft created in Node.js on ChakraCore. According to Janea Systems "ChakraCore has a well-optimized, pure interpreter mode which complies with iOS’ restrictions.” On both platforms, the result is a runtime environment that ensures the highest level of compatibility with Node.js. To sweeten the deal, Node.js for Mobile Apps adds a few features specifically aimed at using Node.js in mobile applications.
Currently, Janea Systems provides three implementations for developing Node.js for mobile: A React bridge, natively and as an Apache Cordova plugin.
Note that Node.js for mobile is a bleeding edge framework that is still in its infancy. Keep in mind there are some issues that might affect your build depending on your choice of target OS. Be sure to learn about any bugs and share your issues and experiences in the official gitter channel and on the Janea Systems Github
In Cordova, you add plugins as needed to help scaffold your mobile app and you can also load them on either Android or iOS platforms or both if you wish. The Android native option requires Android Studio and the iOS version requires Xcode. Take a look at the prerequisites to dive right in.
The requirements for Node.js for mobile Cordova plugin are as follows:
- Cordova 7.0.0 or higher
- for iOS apps: iOS 11 or higher
Install prerequisites for building an app with Node.js for Mobile
To build and run apps with Apache Cordova, you need to install SDKs for each platform you have in mind. If we were building a web app for the browser you would simply use a browser platform which obviously doesn't require any platform SDKs. Even if you didn’t opt for the Cordova route and went with the native Android/iOS route you would still need to install Android Studio (with Android SDK) and iOS’s Xcode.
Janea Systems has created a Cordova sample app to get you started.
Basic steps to build a Cordova app should look like:
sudo npm install -g cordova
Go to your directory where you intend to maintain your source code, and create a cordova project:
$ cordova create MyApp
This creates the required directory structure for your cordova app. By default, the cordova create script generates a skeletal web-based application whose home page is the project's www/index.htmlfile.
$ cordova platform add ios $ cordova platform add android
Once you’ve selected and added a platform to develop on check to see if you meet all the requirements necessary for a successful build. To check if you satisfy requirements for building the platform:
$ cordova requirements Requirements check results for android: Java JDK: installed . Android SDK: installed Android target: installed android-19,android-21,android-22,android-23,Google Inc.:Google APIs:19,Google Inc.:Google APIs (x86 System Image):19,Google Inc.:Google APIs:23 Gradle: installed
Requirements check results for ios: Apple OS X: not installed Cordova tooling for iOS requires Apple OS X Error: Some of requirements check failed
Once you’ve completed the business logic and your app is ready to build you can compile it by running the following command to build the project for all platforms:
$ cordova build
You can optionally limit the scope of each build to specific platforms - 'ios' in this case:
$ cordova build ios
$ cordova run android
Regarding the Cordova sample code,
nodejs.channel.send will send a message from Cordova to
cordova.channel.send will send a message from
Add Node.js for mobile through the Janea Systems Cordova plugin
cordova create MyApp cd MyApp cordova platform add android cordova plugin add nodejs-mobile-cordova
For more samples or full documentation of Janea Systems Node.js for Mobile take a look at their GitHub.
Disclaimer: Node.js for Mobile Apps is not endorsed by or affiliated with the Node.js Foundation. They just did an awesome job at porting Node.js to mobile.
Janea Systems has done a great service to provide the same extensibility and performance we all know and love within Node.js for mobile. Node.js is an unmatched all-purpose tool that is the foundation of most applications of scale in modern day web development. The next logical step in its evolution was to port Node.js to mobile. No longer are we were held back by the mobile environments that imposed restrictions on runtime processes inherent to Node. With the help of Microsoft’s Chakracore, Janea Systems has opened a whole world of development possibilities on both Android and iOS.
At Jscrambler, we help companies all over the world to optimize and protect their Node.js applications. Sign-up for our free trial and let us know if you have any questions. We have a team of experts ready to help you!