๐Ÿง  node_flutter

Run a full Node.js runtime inside Flutter โ€” with background service support and a powerful communication bridge between Dart and Node.js.

Inspired by nodejs-mobile, this plugin allows Flutter apps to run native Node.js scripts and exchange messages efficiently using tagged messages.


โœจ Features

  • โœ… Run full Node.js scripts inside your Flutter app (even in background)
  • โœ… Communicate from Flutter to Node.js and vice versa
  • โœ… Foreground service support โ€” your server never dies!
  • โœ… Structured messaging with tags
  • โœ… Native Dart API and Node.js wrapper

๐Ÿš€ Quick Start

๐Ÿ› ๏ธ 1. Create Node.js entry file

Create this folder structure in your Flutter project:

android/app/src/main/assets/nodejs-project/
  โ””โ”€โ”€ main.js

main.js example:

const bridge = require('./bridge');

bridge.on('hello', (msg) => {
  console.log("Flutter says:", msg);
  bridge.send('hello', 'Hello back from Node.js!');
});

You can create a bridge.js file with the code shown in the ๐Ÿ“ฆ Node.js API section below.


๐Ÿฆ 2. Register assets in pubspec.yaml

flutter:
  assets:
    - nodejs-project/

๐Ÿ“ฒ 3. Flutter main.dart example

import 'package:flutter/material.dart';
import 'package:node_flutter/node_flutter.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await Nodejs.start(); // Start Node.js with main.js
  Nodejs.sendMessage("hello", "Hello from Flutter");

  Nodejs.onMessageReceived.listen((event) {
    if (event['channelName'] == 'hello') {
      print("Node says: ${event['message']}");
    }
  });

  runApp(MyApp());
}

๐Ÿ” Communication Model

Messages between Flutter and Node.js are sent as tagged string payloads, like:

When sent from Flutter to Node.js

msg { "tag": "chat", "message": "Hello world" }

When sent from Node.js to Flutter

msg { "channelName": "chat", "message": "Hello world" }

You can send plain strings or JSON objects (automatically parsed).



๐Ÿ“ฆ Node.js API

Create a main.js file inside /path/to/your-project/android/app/src/main/assets/nodejs-project/:

const bridge = require('flutter-bridge');

console.log("Node.js started");
bridge.send("node", "STARTED");

bridge.on('ping', (message) => {                  // Listen to messages with tag ping sent from Flutter
  console.log(`GOT PING: ${message}`);
  bridge.send("pong", "Hello Flutter!")
});

bridge.on('_EVENTS_', (data) => {                 // This Capture any type of message sent from Flutter
  if (typeof data === 'object' && data !== null) {
    data = JSON.stringify(data);
  }
  console.log(`EVENT: ${data}`);
});

๐Ÿงฉ Flutter API

await Nodejs.start(); // Starts the Node.js project from main.js
await Nodejs.startWithScript("console.log('Hello world'");

await Nodejs.startService(
  "main.js",
  title: "Node Service",
  content: "Running",
);

await Nodejs.sendMessage("tag", "message");

Nodejs.onMessageReceived.listen((Map<String, dynamic> msg) {
  print("TAG: ${msg['tag']} โ€” Message: ${msg['message']}");
});

final abi = await Nodejs.getCurrentABIName();
final nodePath = await Nodejs.getNodeJsProjectPath();
final version = await Nodejs.getPlatformVersion();


๐Ÿงช Example Use Cases

  • Background file server
  • LAN sync service
  • Local database syncing (SQLite or JSON files)
  • MQTT/WebSocket clients
  • Media file indexing

๐Ÿ“ Folder Structure

your_flutter_project/
โ”œโ”€โ”€ android/app/src/main/assets/nodejs-project/
โ”‚   โ”œโ”€โ”€ main.js
โ”‚   โ”œโ”€โ”€ package.json (optional)
โ”‚   โ”œโ”€โ”€ node_modules (optional)
โ”œโ”€โ”€ lib/
โ”‚   โ””โ”€โ”€ main.dart

๐Ÿ”ฎ Roadmap

  • iOS support
  • x Persistent Node.js service
  • Auto-restart service on device boot

๐Ÿ“œ License

MIT License


๐Ÿ”ง Contributing to this project

  1. Clone and cd to this project
git clone https://github.com/binbard/node_flutter
cd node_flutter
  1. Extract Download nodejs-mobile-v.x-android.zip and ensure this structure:
โ”œโ”€โ”€ /path/to/node_flutter/android/libnode/
โ”‚   โ”œโ”€โ”€ bin/
โ”‚   โ”œโ”€โ”€ include/
  1. Ensure these are configured:

/path/to/node_flutter/android/app/build.gradle:

android {
    ndkVersion = "27.0.12077973"
}
  1. Follow flutter commands to get packages, build and run:
flutter pub get
flutter run

โค๏ธ Support & Contributions

If you love this project, star it โญ and consider contributing! PRs welcome.