How to embed multiple instances of node-red in nod

2019-04-13 18:25发布

问题:

Node-red documentation here gives info on how to embed a single node-red app inside a nodejs app - http://nodered.org/docs/embedding

We wanted our site's users to have their own node-red's on different ports for some custom programming. Is it possible to embed multiple node-red apps in a nodejs applicaiton?

I tried repeating same steps for embedding by changing settings of each call with different port but only one time it is created. First time, a node-red instance is created based on settings. Next time we call, we get port in use. I assume this has something to do with node require doing caching and all... Any workaround for this issue?

回答1:

No, currently Node-RED has no multi-user capabilities and no way to instantiate multiple instances in one process.

You'll have to run separate instances of the application for each user. Have a look at something like FRED for an example of this. This runs individual instances and proxies them to make the integration look like it's all on the same port/domain



回答2:

If you're interested I created a fork of the node-red project allowing this feature.

this is how you would initiate it:

var http = require('http');
var express = require("express");
var RED = require("node-red")();
var RED2 = require("node-red")();

// Create an Express app
var app = express();

// Add a simple route for static content served from 'public'
app.use("/",express.static("public"));


// Create a server
var server = http.createServer(app);


// Create the settings object - see default settings.js file for other options
var settings = {
    httpAdminRoot:"/red1",
    httpNodeRoot: "/api",
    userDir:"./hhh",
    functionGlobalContext: { }    // enables global context
};



// Initialise the runtime with a server and settings

RED.init(server,settings);

console.log(RED2.settings === RED.settings, 888, RED2.settings.userSettings);

// Serve the editor UI from /red
app.use(settings.httpAdminRoot,RED.httpAdmin);


// Serve the http nodes UI from /api
app.use(settings.httpNodeRoot,RED.httpNode);


server.listen(8005);


// Start the runtime
RED.start();


var app2 = express();
app2.use("/",express.static("public"));
var server2 = http.createServer(app2);
var settings2 = {
    httpAdminRoot:"/red2",
    httpNodeRoot: "/api",
    userDir:"./hhhh",
    functionGlobalContext: { }
};

RED2.init(server2,settings2);
app2.use(settings2.httpAdminRoot,RED2.httpAdmin);
app2.use(settings2.httpNodeRoot,RED2.httpNode);



RED2.start();
server2.listen(8006);

console.log(RED.settings.httpAdminRoot);
console.log(RED2.settings.httpAdminRoot);
console.log(RED2.settings === RED.settings);

also, works on the same port. but make sure to use different paths is so.

https://github.com/aryeharmon/node-red