### Install Node.js Mobile React Native Plugin Source: https://github.com/nodejs-mobile/nodejs-mobile-react-native/blob/main/README.md Installs the nodejs-mobile-react-native plugin using npm. For iOS, it also requires running 'pod install' to link native dependencies. ```bash npm install nodejs-mobile-react-native --save cd iOS && pod install ``` -------------------------------- ### nodejs.startWithScript Method Signature Source: https://github.com/nodejs-mobile/nodejs-mobile-react-native/blob/main/README.md The `nodejs.startWithScript` method enables starting the Node.js runtime directly from a string containing JavaScript code. It also supports optional startup configuration. ```javascript nodejs.startWithScript(scriptBody [, options]) // Param scriptBody: string (JavaScript code) // Param options: [StartupOptions](#ReactNative.StartupOptions) ``` -------------------------------- ### React-Native Application Integration Source: https://github.com/nodejs-mobile/nodejs-mobile-react-native/blob/main/README.md Instructions on how to integrate and start the Node.js runtime within a React Native application using the `nodejs-mobile-react-native` library. ```APIDOC ## React-Native Application Integration To communicate with Node.js from your React Native application, import the library and configure its startup in your main component. ### Importing the library ```javascript import nodejs from 'nodejs-mobile-react-native'; ``` ### Starting the Node.js runtime Add the following to your application's main component's `componentWillMount` lifecycle event: ```javascript componentWillMount() { nodejs.start("main.js"); nodejs.channel.addListener( "message", (msg) => { alert("From node: " + msg); }, this ); } ``` This initializes a dedicated Node.js thread starting with `main.js` and sets up a listener for messages from Node.js. **Note:** The Node.js project runs as a singleton. Only the first `nodejs.start()` call is effective. Subsequent calls, including those during React Native's hot reloading, will not restart the Node.js thread. ``` -------------------------------- ### nodejs.startWithArgs Method Signature Source: https://github.com/nodejs-mobile/nodejs-mobile-react-native/blob/main/README.md The `nodejs.startWithArgs` method allows starting the Node.js runtime with a script and passing command-line arguments to it. The `command` parameter should typically include the script file name as the first argument. ```javascript nodejs.startWithArgs(command [, options]) // Param command: string (e.g., 'main.js --arg1 --arg2') // Param options: [StartupOptions](#ReactNative.StartupOptions) ``` -------------------------------- ### Start Node.js Runtime in React Native Source: https://github.com/nodejs-mobile/nodejs-mobile-react-native/blob/main/README.md This snippet shows how to initialize the Node.js runtime within a React Native application during the `componentWillMount` lifecycle event. It starts the Node.js thread with 'main.js' and sets up a listener to receive messages from Node.js. ```javascript componentWillMount() { nodejs.start("main.js"); nodejs.channel.addListener( "message", (msg) => { alert("From node: " + msg); }, this ); } ``` -------------------------------- ### Node.js Mobile React Native API Methods Source: https://github.com/nodejs-mobile/nodejs-mobile-react-native/blob/main/README.md This lists the core methods available in the nodejs-mobile-react-native library for interacting with the Node.js runtime from the React Native layer. These methods allow for starting the runtime, managing communication channels, and sending messages. ```javascript import nodejs from 'nodejs-mobile-react-native'; // Methods available: // nodejs.start // nodejs.startWithArgs // nodejs.startWithScript // nodejs.channel.addListener // nodejs.channel.post // nodejs.channel.send ``` -------------------------------- ### Set Android NDK Home Environment Variable Source: https://github.com/nodejs-mobile/nodejs-mobile-react-native/blob/main/README.md Sets the ANDROID_NDK_HOME environment variable to point to the NDK installation directory. This is required for Android builds to locate and configure NDK requirements. ```sh export ANDROID_NDK_HOME=/Users/username/Library/Android/sdk/ndk-bundle ``` -------------------------------- ### Handle App Lifecycle Events in Node.js (Pause/Resume) Source: https://github.com/nodejs-mobile/nodejs-mobile-react-native/blob/main/README.md Provides examples of how to register callback functions for application lifecycle events like 'pause' and 'resume' using 'rn_bridge.app.on'. The 'pause' event includes a pauseLock for graceful backgrounding on iOS. ```javascript rn_bridge.app.on('pause', (pauseLock) => { console.log('[node] app paused.'); pauseLock.release(); }); rn_bridge.app.on('resume', () => { console.log('[node] app resumed.'); }); ``` ```javascript rn_bridge.app.on('pause', (pauseLock) => { server.close( () => { // App will only suspend after the server stops listening for connections and current connections are closed. pauseLock.release(); }); }); ``` -------------------------------- ### Get Node.js Data Directory Path Source: https://github.com/nodejs-mobile/nodejs-mobile-react-native/blob/main/README.md Shows how to retrieve a writable path for persistent data storage using 'rn_bridge.app.datadir()'. This path corresponds to the application's document directory on iOS and file directory on Android. ```javascript const dataPath = rn_bridge.app.datadir(); console.log('Data directory:', dataPath); ``` -------------------------------- ### nodejs.start Method Signature Source: https://github.com/nodejs-mobile/nodejs-mobile-react-native/blob/main/README.md The `nodejs.start` method is used to initiate the Node.js runtime thread. It takes the main script file name as an argument and optionally accepts startup options. ```javascript nodejs.start(scriptFileName [, options]) // Param scriptFileName: string // Param options: [StartupOptions](#ReactNative.StartupOptions) ``` -------------------------------- ### Core API Methods Source: https://github.com/nodejs-mobile/nodejs-mobile-react-native/blob/main/README.md Overview of the primary methods available in the `nodejs-mobile-react-native` layer for interacting with the Node.js runtime. ```APIDOC ## Core API Methods These methods are available in the React Native JavaScript layer: ```javascript import nodejs from 'nodejs-mobile-react-native'; ``` ### `nodejs.start(scriptFileName [, options])` Starts the Node.js runtime thread using a specified JavaScript file from the `nodejs-project` directory. - **`scriptFileName`** (`string`): The name of the main script file. - **`options`** (`[StartupOptions](#ReactNative.StartupOptions)`): Optional startup configuration. ### `nodejs.startWithArgs(command [, options])` Starts the Node.js runtime thread with a script and passes command-line arguments to it. - **`command`** (`string`): The command string, typically starting with the script name (e.g., `'main.js --arg1 --arg2'`). - **`options`** (`[StartupOptions](#ReactNative.StartupOptions)`): Optional startup configuration. ### `nodejs.startWithScript(scriptBody [, options])` Starts the Node.js runtime thread by executing the provided script body directly. - **`scriptBody`** (`string`): The JavaScript code to execute. - **`options`** (`[StartupOptions](#ReactNative.StartupOptions)`): Optional startup configuration. ### `nodejs.channel.addListener(event, callback)` Registers a callback function to handle user-defined events emitted from the Node.js side. - **`event`** (`string`): The name of the event to listen for. - **`callback`** (`[function](#ReactNative.channelCallback)`): The function to execute when the event is received. ### `nodejs.channel.post(event, ...message)` Emits a user-defined event from the React Native side to the Node.js side. - **`event`** (`string`): The name of the event to emit. - **`...message`** (`any`): Any data that can be serialized/deserialized with JSON. ### `nodejs.channel.send(...message)` An alias for `nodejs.channel.post('message', ...message)`. Used to send messages to the default 'message' channel. - **`...message`** (`any`): Data to send, serializable via JSON. **Note:** The `nodejs.channel` object extends React Native's `EventEmitter`, with `emit` removed and `post` and `send` added for Node.js communication. ``` -------------------------------- ### Sample Node.js Main File for React Native Bridge Source: https://github.com/nodejs-mobile/nodejs-mobile-react-native/blob/main/README.md A sample JavaScript file for the Node.js project that communicates with React Native. It echoes messages received from React Native and sends an initialization confirmation. ```javascript var rn_bridge = require('rn-bridge'); // Echo every message received from react-native. rn_bridge.channel.on('message', (msg) => { rn_bridge.channel.send(msg); } ); // Inform react-native node is initialized. rn_bridge.channel.send("Node was initialized."); ``` -------------------------------- ### Enable Native Module Building for Android/iOS Source: https://github.com/nodejs-mobile/nodejs-mobile-react-native/blob/main/README.md Enables the automatic building of native Node.js modules for the React Native project. This is done by creating a BUILD_NATIVE_MODULES.txt file with the content '1'. ```sh echo "1" > nodejs-assets/BUILD_NATIVE_MODULES.txt react-native run-android ``` ```sh echo "1" > nodejs-assets/BUILD_NATIVE_MODULES.txt react-native run-ios ``` -------------------------------- ### Clean Android Build Script (Windows) Source: https://github.com/nodejs-mobile/nodejs-mobile-react-native/blob/main/README.md Provides commands to perform a clean rebuild of a React Native Android application on Windows. This is useful when the build process fails to rebuild assets. ```sh cd android gradlew clean cd .. react-native run-android ``` -------------------------------- ### Configure Android Native Libraries with CMake Source: https://github.com/nodejs-mobile/nodejs-mobile-react-native/blob/main/android/CMakeLists.txt This snippet sets up the CMake build environment for Android native code. It defines the minimum CMake version, creates a shared native library named 'nodejs-mobile-react-native-native-lib' from specified C++ source files, and includes necessary directories for Node.js headers and C++ sources. It also imports a prebuilt 'libnode.so' library and links the main native library against 'libnode' and the Android NDK's 'log' library. ```cmake cmake_minimum_required(VERSION 3.4.1) add_library( # Sets the name of the library. nodejs-mobile-react-native-native-lib # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). src/main/cpp/native-lib.cpp src/main/cpp/rn-bridge.cpp ) include_directories(libnode/include/node/) include_directories(src/main/cpp/) add_library( libnode SHARED IMPORTED ) set_target_properties( # Specifies the target library. libnode # Specifies the parameter you want to define. PROPERTIES IMPORTED_LOCATION # Provides the path to the library you want to import. ${CMAKE_SOURCE_DIR}/libnode/bin/${ANDROID_ABI}/libnode.so ) find_library( # Sets the name of the path variable. log-lib # Specifies the name of the NDK library that # you want CMake to locate. log ) target_link_libraries( # Specifies the target library. nodejs-mobile-react-native-native-lib libnode # Links the target library to the log library # included in the NDK. ${log-lib} ) ``` -------------------------------- ### Prebuilds for Native Modules Source: https://github.com/nodejs-mobile/nodejs-mobile-react-native/blob/main/README.md Information on how nodejs-mobile-react-native automatically detects and disables the compilation of native modules that have prebuilt binaries. ```APIDOC ## Prebuilds for Native Modules The plugin automatically detects prebuilt native modules and disables their on-the-fly compilation. Prebuilds are identified as `.node` files in the following path within an npm package: ``` nodejs-assets/nodejs-project/node_modules//prebuilds/-/.node ``` Supported values for `PLATFORM` and `ARCH` are: - **PLATFORM**: `android` - **ARCH**: `arm`, `arm64`, `x64` - **PLATFORM**: `ios` - **ARCH**: `arm64`, `x64` Compilation is disabled by modifying the module's `binding.gyp` and `package.json` to prevent node-gyp from compiling it. ``` -------------------------------- ### Clean Android Build Script (Linux/macOS) Source: https://github.com/nodejs-mobile/nodejs-mobile-react-native/blob/main/README.md Provides commands to perform a clean rebuild of a React Native Android application on Linux or macOS. This is useful when the build process fails to rebuild assets. ```sh cd android ./gradlew clean cd .. react-native run-android ``` -------------------------------- ### Configure iOS Deployment Target Source: https://github.com/nodejs-mobile/nodejs-mobile-react-native/blob/main/README.md Modifies the iOS Podfile to set the minimum deployment target to 13.0, ensuring compatibility with the plugin. This is necessary if React Native's default minimum iOS version is lower. ```diff -platform :ios, min_ios_version_supported +platform :ios, 13 ``` -------------------------------- ### Import Node.js Mobile for React Native Source: https://github.com/nodejs-mobile/nodejs-mobile-react-native/blob/main/README.md This code snippet demonstrates how to import the nodejs-mobile-react-native library into your React Native application. This is the first step required to utilize Node.js functionalities within your mobile app. ```javascript import nodejs from 'nodejs-mobile-react-native'; ``` -------------------------------- ### Sending Messages from React Native Source: https://github.com/nodejs-mobile/nodejs-mobile-react-native/blob/main/README.md Demonstrates how to send messages from the React Native interface to the running Node.js thread. ```APIDOC ## Sending Messages from React Native You can send messages to the Node.js project from your React Native UI. ### Example Button ```javascript