Native Node.js for Mobile | Jscrambler Blog

March 08, 2018 0 Comments

Native Node.js for Mobile | Jscrambler Blog

 

 

native-js-for-mobile

Introduction

Since its creation in 2009, thanks to Ryan Dahl, Node.js has become a cornerstone of the development community even extending beyond JavaScript. Numerous modern day applications including some big names like LinkedIn, Netflix, Lyft, Uber and Yahoo all utilize Node.js as an integral part of their development cycle.

Node.js

For those new to development, Node.js is a popular JavaScript runtime, based on Chrome's V8 engine. Similar to a Java Runtime Environment, Node.js has a virtual machine called JVM (JavaScript Virtual Machine) used for performing tasks, runtime compilation, and memory management. APIs that serve mobile applications are a common example of Node.js in the real world. For example, social media authentication as it occurs when you log into your favourite network could be represented as an I/O operation.

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

Although Node.js is used in hybrid apps and mobile web development there hasn’t been a native Node.js iteration for mobile. That is until now. Janea Systems has successfully created a Node.js for mobile built on top of ChakraCore). The Android version of the core library uses the V8 JavaScript engine – much like regular Node.js.

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

for android:

$ cordova run android

Regarding the Cordova sample code, nodejs.channel.send will send a message from Cordova to nodejs-mobile and cordova.channel.send will send a message from javascript nodejs-mobile to Cordova.

Before running these commands, you’ll need to set up the device for testing, following procedures that vary for each platform. You can modify your default generated app to take advantage of standard web technologies, but for the app to access device-level features, you need to add plugins. A plugin in Cordova exposes a JavaScript API for native SDK functionality.

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.

Conclusions

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.

For many enterprises and their developers, using JavaScript as a backend might seem questionable. This may have been the case 10 years ago, luckily modern JavaScript web development is in an awesome place thanks to the open source development communities. Node.js has more than proven itself to be secure, reliable and flexible with the right security measures. It structures the foundation for scalable applications and provides clean and secure solutions. Only now it has finally made its way to mobile. At Jscrambler we help companies all over the world to optimize and protect their Node.js applications, sign-up for our free trial here and let us know if you have any questions! We have a team of experts ready to help you!


Tag cloud