๐ง 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
x
Persistent Node.js service
๐ License
MIT License
๐ง Contributing to this project
- Clone and cd to this project
git clone https://github.com/binbard/node_flutter
cd node_flutter
- Extract Download nodejs-mobile-v.x-android.zip and ensure this structure:
โโโ /path/to/node_flutter/android/libnode/
โ โโโ bin/
โ โโโ include/
- Ensure these are configured:
/path/to/node_flutter/android/app/build.gradle
:
android {
ndkVersion = "27.0.12077973"
}
- 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.