GithubHelp home page GithubHelp logo

hhy5277 / react-native-node Goto Github PK

View Code? Open in Web Editor NEW

This project forked from staltz/react-native-node

0.0 1.0 0.0 6.5 MB

Run a separate Node.js process behind a React Native app

License: MIT License

Java 67.71% JavaScript 23.55% Python 8.73%

react-native-node's Introduction

React Native Node

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.

Example app

Install

npm install --save react-native-node
react-native link react-native-node

Usage

  1. Develop the background Node.js project under some directory

    • e.g. ./background
  2. 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();
   }
   // ...
 }
  1. Bundle and insert the background application into the mobile app using the command
./node_modules/.bin/react-native-node insert ./background
  • Compresses your background app into ./android/app/src/main/res/raw/rnnodebundle
  • Updates AndroidManifest.xml to include a Service class
  1. (Re)build the mobile app
react-native run-android

Tip 1

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",

Tip 2

You can reduce the size of the bundle file rnnodebundle by using a tool like noderify to create a single js file.

Tip 3

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/errored
  • RNNodeService - debug tar/untar, node binary preparation etc.
  • RNNode

FAQ

How is this possible?

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.

What about iOS support?

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.

Does it support packages with native bindings?

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.

Why did you build this?

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.

react-native-node's People

Contributors

jsdario avatar soyuka avatar staltz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.