Node.JS plugin for Apache Cordova (built on JXcore)
This project is intended to ;
- create an easy to use node.js plugin for Apache Cordova (Android, iOS)
- show JXcore's embedding interface in details.
If you don't have Cordova installed, follow the steps from this link to install Apache Cordova.
$ npm install -g jxcor if you have JXcore installed:
$ jx install -g jxcOn posix platform you may need to run it as sudo.
Before installing the plugin you might want to enable jxc caching as follows. This is to prevent multiple downloads of same jxcore-cordova plugin by jxc.
$ jxc config set cacheNow, assuming your Cordova JXcore application is located under /hello folder, go under /hello folder.
$ jxc installBy default the latest version of the plugin is used. However you may specify an exact version, e.g.:
$ jxc install 0.1.10For list of other possible versions see http://az836273.vo.msecnd.net.
- Download the latest package
- either manually (e.g. http://az836273.vo.msecnd.net/0.0.8/io.jxcore.node.jx and save into e.g. c:\jxcore-cordova)
- or through
jxc downloadcommand (in command prompt window started in e.g. c:\jxcore-cordova)
- open your cordova solution in Visual Studio
- double click config.xml in Solution Explorer
- select Plugins and then Custom tab
- select Local radio button and browse for c:\jxcore-cordova\io.jxcore.node
- click Add button
Once everything is set and you have added jxcore-cordova extension, create a folder named
jxcore right under www
www/jxcore/app.js is your entry point to JXcore's JS.
Node modules should go in the www/jxcore/node_modules folder.
Important Steps for the First Timers
Under the sample folder you will find express_sample application. There you have the entire
www folder that you can use instead of the www folder under cordova project root folder.
Replace www folder from the project's root to the one under the sample/express_sample.
You can also use the automated script on posix platforms: install_and_run.sh. More on this
here.
Are you are looking for a minimalistic sample? follow the steps below;
-
Under the
sample/wwwfolder of this repo, you will findindex.html. This sample file shows how to integrate JXcore interface into Cordova client side. Prior to installing JXcore plugin, you should update Cordova's index.html as shown from this sample file. -
This plugin expects you to have a folder named
jxcoreunderwwwfolder. The sampleindex.htmltries to loadapp.jsfrom this folder. You can copysample/www/folder intowwwto run the basic demo.
!In case you have a problem with installing the plugin. You may follow the steps below; (be careful though since this trick removes the existing platforms and installs them back)
cordova platform remove ios
cordova platform remove android
cordova plugins remove io.jxcore.node
# re-add the plugin:
cordova plugins add io.jxcore.node
# or if you use jxc:
jxc install --force
cordova platform add ios
cordova platform add androidNow you can visit platforms/ios or platforms/android folders and open Xcode project file
or import the android project from Eclipse.
Below are the steps to be taken if you want to update JXcore binaries in your Cordova JXcore
application. They all should be called prior to cordova plugin add command. This step is
optional. We keep the core binaries are updated.
-
Rebuild JXcore binaries as a static library by embedding leveldown:
$ git clone https://github.com/jxcore/jxcore.git $ cd jxcore $ build_scripts/android-configure.sh /path/to/android/ndk/ $ build_scripts/android_compile.sh /path/to/android/ndk/ --embed-leveldown -
Refresh
jxcore-cordova/src/android/jxcore-binariesfolder contents:$ cd /my/cordova/app $ git clone https://github.com/jxcore/jxcore-cordova.git $ rm -f ./jxcore-cordova/src/android/jxcore-binaries/* $ cp -f /jxcore/repo/out_android/android/bin/* jxcore-cordova/src/android/jxcore-binaries/
-
Recompile .so files
$ cd jxcore-cordova/src/android/jni $ ~/android-ndk-path/ndk-build
-
Add/re-add the plugin/platform
$ cd ../../../../ $ cordova plugin add jxcore-cordova/ $ cordova platforms add android -
You may run the app now
$ cordova run
JavaScript on UI side works on top of Cordova's webUI. JXcore's JavaScript is a separate instance.
So you need an API to communicate between Cordova JS to JXcore JS.
These API methods are used on the side of Apache Cordova (for example, in the main index.html
of your Cordova application).
jxcore(name_of_the_function).register(a_function_to_register);Example:
jxcore('alert').register(function(msg){ alert(msg); });jxcore(name_of_the_function).call(params_to_send..., callback);Example:
jxcore('asyncPing').call('Hello', function(p1, p2, p3...){ });These API methods are used on the side of JXcore (for example, in the main app.js of your
application based on Node API).
Mobile(name_of_the_function).registerSync(a_function_to_register);This method expects the registered function to be synchronous (i.e. to immediately return a value).
Example:
Mobile('syncPing').registerSync(function(msg){ return msg + ' pong'; });Mobile(name_of_the_function).registerAsync(a_function_to_register);This method expects the registered function to be asynchronous (i.e. to return some value using a callback).
Example:
Mobile('asyncPing').registerAsync(function(msg, callback){ callback(msg + ' pong') });Mobile(name_of_the_function).call(params...);Example:
Mobile('log').call(msg);You may also define JXcore JS side methods those you want to call from Java / Obj-C.
If you need a JS side method that you want to call multiple times use below approach instead depending on a method callback id.
Visit www/jxcore folder and install the node modules there. It's adviced to use 'jx install' command to install node modules from npm.
For example
// UNIX
www/jxcore > sudo jx install jxm --autoremove "*.gz"
// Windows
www/jxcore > jx install jxm --autoremove "*.gz"'--autoremove "*.gz"' will be removing the gz files from modules folder. Android APK doesn't allow you to put .gz files into application's assets.
Consider using either process.userPath or require('os').tmpdir() to get the Documents
(on ios) or a folder you have the write access. process.cwd() or __dirname may not
target a folder that you have the write access!
If you are okay with using Mobile specific API see Mobile.GetDocumentsPath below;
Returns the location for Application (specific) writable folder.
Mobile.getDocumentsPath(function(err, location) {
if (err)
console.error("Error", err);
else
console.log("Documents location", location);
});Android and iOS file systems behave differently. Android OS supports external persistent storage. If you want to store a persistent information on Android OS, consider using sdcard location.
Returns device's connection status
Mobile.getConnectionStatus(function(err, status) {
if (status.NotConnected)
console.log("No internet connection");
else if (status.WiFi)
console.log("WiFi");
else if (status.WWAN)
console.log("Mobile Connection");
});Returns device's manufacturer and model name
Mobile.getDeviceName(function(err, name) {
if (err)
console.error("Something bad has happened");
else
console.log("Device name", name)
});- pause
Occurs whenever an application is paused on the device (e.g. goes to the background).
process.on('pause', function() {
console.log('pause');
});- resume
Occurs whenever an application will start interacting with the user (e.g. comes back from the background).
process.on('resume', function() {
console.log('resume');
});- connectionStatusChanged(status)
Occurs whenever network connection status has been changed on mobile device (e.g. WiFi has been turned on or Plane Mode has been enabled).
The status is a string containing one of the following: WiFi, WWAN, NotConnected.
See also Mobile.getConnectionStatus.
process.on('connectionStatusChanged', function(status) {
console.log('new network status:', status);
});If you want to customize JS side errors, visit JXMobile.java for Android and JXMobile.m
for iOS and update OnError behavior
- JXcore cordova interface doesn't keep the reference for a callback id once it's used.
- JavaScript is a single threaded language. Don't call the referenced JS methods from other threads.
Mobile('fromJXcore').registerToNative(function(param1, param2){
// this method is reachable from Java or ObjectiveC
// OBJ-C : [JXcore callEventCallback:@"fromJXcore" withParams:arr_parms];
// Java : jxcore.CallJSMethod("fromJXcore", arr_params);
});See JXcoreExtension.java / JXcoreExtension.m / .h for sample Java/Obj-C definitions.
If you see a mistake / bug or you think there is a better way to do the things, feel free to contribute. All the contributions are considered under MIT license.