Dalton Gore
Dalton Gore

Reputation: 919

Best way to use multiple express processes?

So I'm running a series of chatbots, which have little websites associated with them. I'm using express and handlebars to do all the thing. I'm using iptables to route all data on port 3000 to port 80.

However, while the wrapper file works on port 3000, since the bots are started as child processes, each one has to have a different port. I am using the wrapper to route to the ports, but it's still annoying.

For example, I have chatbot.example.com on port 3000, but since bot #1 is a child process, it's running its webserver on port 3001.

So I have the wrapper route chatbot.example.com/bot1 to chatbot.example.com:3001/bot1 But that feels horrendously tacky.

Is there any better way to do this?

Current code example: snippi.

Upvotes: 1

Views: 1059

Answers (2)

Paul
Paul

Reputation: 36319

Consider using the cluster module for your setup, rather than manually creating a wrapper and spawning child processes. It's probably more stable than what you have, and should let them all share a port if that's what you're looking to do.

I also wouldn't couple (if it were me) between one process == one chatbot. Instead, I'd write the app such that any process can respond as any chatbot based on the path params or querystring params, using the regular Express routing stuff. This way if you can load balance your processes based on actual use; one chatbot that's getting hit a lot might be serviced by 2 or 3 processes, while one that's not getting used at all would not consume extra resources.

UPDATED for sample code So, it looks to me like you're kinda conflating purposes a tidge. I can't tell from your snippet what exactly each site is supposed to do, other than that it's related to a given chat bot. I'm guessing here that the purpose of each chatbot's site is to display the activity relative to that chatbot's chatting. Seems to me that you're better off launching the chatbots as one node app (or one per chatbot, either way works), which each log to a db of some kind whatever state you want to track. MongoDb, CouchDb, some SQL db, doesn't matter. The web app could then be just a single app that simply reads that DB and filters for a given chatbot based on the path.

Overall, feels like you're trying to get the one app do too much.

Upvotes: 1

Chris
Chris

Reputation: 1611

Express can handle vhosts. I run 3 sites on the same server with express.vhost() with cluster, and they all listen to the same port.

So, you could do something like chatbot1.site.com, chatbot2.site.com, all with the same app, just different vhosts.

// main 'server.js' app
var express = require('express');
var app = express();

app
    .use(express.vhost('chatbot1.site.com', require('./chatbot1.js').app ) )
    .use(express.vhost('chatbot2.site.com', require('./chatbot2.js').app ) );

// delay for db connections to start up (just what I do)
setTimeout( function () {
    app.listen(port);
}, 2000);

In your 'chatbot#.js' file, you'll need to export your app:

// chatbot#.js
var app = exports.app = express ();

The app in your 'chatbox.js' file will also NOT listen to a port, as the 'server.js' app starts the listening. You might even be able to get away with using only 1 chatbot.js file, depending on how you store/get it's info. I think it would be simple enough to copy 'chatbot1.js' and make 'chatbot2.js' with different settings though. Up to you and how your 'chatbot's get setup.

BTW, this system here also works well with cluster, as you'll only need to wrap the cluster code around the code in the 'server.js' file.

Upvotes: 0

Related Questions