Run a real Node.js process in the background, behind a React Native app.
DEPRECATED. Please use Node.js Mobile by Janea Systems instead. It has the same purpose as this library, but is more updated, and supports iOS. This library still works as documented, but I'm not committed to maintaining it anymore.
Using this package you can: run http
servers in Android, use Node streams, interface with the filesystem, off load some heavy processing out of the JS thread in React Native, and more! Running the real Node.js in Android, you can do everything that Node.js on desktop can.
npm install --save react-native-node
react-native link react-native-node
Develop the background Node.js project under some directory
./background
In your React Native JavaScript source files, spawn the background process:
+import RNNode from "react-native-node";
class MyApp extends Component {
// ...
componentDidMount() {
+ RNNode.start();
+ // or specify arguments to the process:
+ RNNode.start(['--color', 'red', '--port', '3000'])
}
componentWillUnmount() {
+ RNNode.stop();
}
// ...
}
./node_modules/.bin/react-native-node insert ./background
./android/app/src/main/res/raw/rnnodebundle
AndroidManifest.xml
to include a Service classreact-native run-android
If you want to bundle and insert the background app always before building the mobile app, make a prestart
package.json script (assuming you use the start
script):
"scripts": {
+ "prestart": "react-native-node insert ./background",
"start": "node node_modules/react-native/local-cli/cli.js start",
You can reduce the size of the bundle file rnnodebundle
by using a tool like noderify to create a single js file.
To debug, use adb logcat
with the nodejs
tag. For example with react:
adb logcat *:S nodejs:V ReactNative:V ReactNativeJS:V
These additional logging tags are used by react-native-node
:
RNNodeThread
- will tell you if your process has started/terminated/erroredRNNodeService
- debug tar/untar, node binary preparation etc.RNNode
Node.js v7.1.0 (with V8, not JavaScriptCore) was compiled to a binary bin_node_v710
following the approach used by "NodeBase". This binary is prebuilt and included in this library. If you are concerned about security, you shouldn't use the prebuilt binary, but compile it yourself.
We can't run V8 Node.js on iOS since that violates Apple's policy around Just-In-Time compilation, but ChakraCore Node.js can run on iOS. We are depending on this project by Janea Systems to open source their methods and include a proper open source license.
Yes, in theory, but that's the job of individual libraries having native bindings for android. Most packages don't have. I believe sodium-native has. Hint: if you want to compile the native part of packages, I recommend not trying to cross-compile. Instead, install termux
on an Android device and compile from the phone directly.
I am bringing the Scuttlebutt ecosystem to mobile, and I built the package react-native-scuttlebot which uses this tool as dependency. These are in turn used by the mobile app project mmmmm.