Over a million developers have joined DZone.

How to Run Node.JS in an Android App

This quick tutorial will run you through running Node.js in an Android app.

· Mobile Zone

Hello geekers, Android is more customizable and easy to use mobile operating system nowadays. And in upcoming growing language node.js has their great power which uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.

And for a developer, it would be great to integrate Node.Js with their Android application to use that power with existing application.

Here is the library which do it same for you named as Node-Android library which is used to run Node.js on Android by rewrite Node.js in Java with the compatible API.

Download this library from here

Now let's dive in with the process of integration and features.

Build

Clone the code, open Android Studio (1.*) and import the project.

For Eclipse ADT user, refer to this.

Javascript code injection

> adb shell am start -a android.intent.action.VIEW -n com.iwebpp.nodeandroid/.MainActivity -e js "var run = function () { return 'hello world'; } run();"

Features

  • Node.js 0.10.x compatible API
  • Multi-threading: run separate node context in Java thread
  • libUV native support
  • Timer, set/clear Timeout/Interval
  • EventEmitter
  • Stream
  • HttpParser(rewrite http-parser.c in java)
  • HTTP
  • HTTPP(run http over udp)
  • TCP
  • UDT(udp transport)
  • DNS
  • URL
  • IPv6
  • for API usage, check 
  • WebSocket/WebSocketServer supported, check 
  • Connect middleware
  • Crypto: NACL support, public box,secret box,signature/verify
  • SecureWebSocket over NACL

JS Runtime

  1. Rhino supported
  2. Exposed node-android packages: com.iwebpp.node.http, com.iwebpp.node.stream, com.iwebpp.node.net, etc
  3. Exposed node-android classes: com.iwebpp.node.EventEmitter2, com.iwebpp.node.Dns, com.iwebpp.node.Url, etc
  4. Exposed node-android native context in JS standard scope as NodeCurrentContext alias NCC
  5. Exposed Android API: android.util.Log
  6. NodeJS compatible internal modules are available in JS standard scope
  7. Exposed WebSocket classes: com.iwebpp.wspp.WebSocket, com.iwebpp.wspp.WebSocketServer
Topics:
mobile ,android ,node js

Published at DZone with permission of Adam Smith. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}