Running multiple bots off one server instance

Hey! So I’ve been playing around with a twitch bot for a while now and I’m getting to the point where some of the people using my bot would like to customize the name of the bot itself in their channel and I was wondering what the best way of doing that is. My first thought was just making an account for their custom bot and essentially running everything twice in the main app.js but that seems very inefficient.

I just want to be able to feed the server multiple keys but run the same exact bot for however many keys fed while using the same event listeners, commands, etc. If there is a better way of changing bot name per channel I’m open to any suggestions! Code samples/GitHub/documentation appreciated :slight_smile:

The bot is built off the tmi.js API and hosted on Heroku.

It’s entirely up to you how you wish to go about it. Some people run multiple connections from a single process, others use a separate process for each connection, it’s entirely up to you and what’s best for your use case, growth, and server.

Examples of ways to do this would be for a single process that loads the credentials for all bot accounts, then rather than creating just a single connection you create a connection for each account, and then just add the same listeners to each of them to reduce the need to duplicate code.

Or for a multi-process app you could run the app once for each account and pass the credentials to the process as arguments and the app would use whatever arguments it’s given to create a connection with.

Regardless of how you choose to go about it, you’re going to need 1 connection per account at a minimum (if some accounts are going to be in a lot of channels you may need to have multiple connections for that account), so as long as you’re reusing the same code for each connection then there isn’t a great deal of difference on the small scale.

1 Like

Ohhhhh okay that makes sense. So essentially just create multiple clients based on how many keys I have and the register the same handler functions to all clients like this?

// create bot clients
const client_1 = new tmi.client(config);
const client_2 = new tmi.client(config);
// register event handlers
client_1.on(“message”, onMessageHandler);
client_2.on(“message”, onMessageHandler);

For the processes themselves I’m a bit confused when it comes to how to do this in Node/JS in general (usually use Python for process/threaded work). I’ve done some digging and it seems like the two main options are worker threads and child processes but not sure what would be best for this problem. My first guess would be create a process for each client and that would work but was wondering if making commands run off a worker thread from the child process be worth it or even possible? Or if just running each client off a worker thread would be sufficient.

I know that most options that I gave there would work just not sure if spawning worker threads from child processes is actually a thing. I’m essentially looking for the most efficient way of solving the problem whether that’s using child processes, worker threads or a combination of both. Thanks!

2nd part is correct, but when creating the clients you would pass each one a different config as they’d have different username/token, and different default channels.

I’d suggest while you’re still learning and the bot is still growing to just stick to a single process for now, it’s unlikely you’ll run into issues in terms of scaling for now and it’ll be much easier for you to develop and also see how it acts performance wise (usually chat bots are very low resource usage so shouldn’t be an issue).