Hosting Multiple Instances of Rocket.Chat

When setting up Rocket Chat for a site running Ubuntu I ran across an interesting problem. The default method for running the service from 16.04+ is to use a Snap. This amounts to basically a docker component, or for the uninitiated a self-contained application. It's a nice setup, you install the snap management service and then install rocket chat from that, it automatically self-updates periodically and you're pretty much hands-free. The issue comes when you need to run multiple rocket chat services for different domains as they did. There is no easy way to get the snap service to fork into multiple running instances. What's worse is the database and everything else is contained within the walled garden of the snap. So I did what any self-respecting admin did: I read 2 pages of the manual, got frustrated with it, tossed it out, googled "multiple snap instances", got nothing useful, and then had a drink. When I returned to the actual problem it occurred to me that I only needed to find out how to activate the service normally from a non-snap environment and then explicitly reference each of those elements from within the snap. From there it became rather easy. The service uses a mongo database to store everything so I needed to copy that as well. Here are the step-by-step instructions to accomplish this if you feel the need:

Log into MongoDB and copy parties database to another new database
You will need to be within the Snap
#>cd /snap/rocketchat-server/current/bin
#>sudo ./mongo

Now copy the database
db.copyDatabase("parties", "parties2", “127.0.0.1")

This will take a second, wait for the prompt to come back. I swear, I thought it locked up or I forgot a semi-colon. Once that’s done do an “exit”.

Ok, so you have your data ready for a new instance.

Head to your home directory and make a new bash script to start the thing up:
#>Cd ~
#>Nano startRocket

#!/bin/bash
export DEPLOY_METHOD=snap
export NODE_ENV=production
export BABEL_CACHE_DIR=/tmp
export ROOT_URL=http://chat.domain.com
export PORT=3001
export MONGO_URL=mongodb://localhost:27017/parties2
export MONGO_OPLOG_URL=mongodb://localhost:27017/local
export Accounts_AvatarStorePath=/var/snap/rocketchat-server/common/uploads2


/snap/rocketchat-server/current/bin/node /snap/rocketchat-server/current/main.js

This does the heavy lifting. If you’re doing more than two, you can keep doing this process, just make sure you update the first mongodb line, root url, the port, and the avatar path.

Now make it executable:
#>Chmod 775 startRocket

Copy it into the /usr/bin directory
#>sudo cp startRocket /usr/bin

Now build a system service for it
#>nano rocketService.service

[Unit]
Description=Rocket Chat Service

[Service]
ExecStart=/usr/bin/startRocket

[Install]
WantedBy=multi-user.target


Now you can move that over to /etc/systemd/system/
#>sudo cp rocketService.service /etc/systemd/system

And finally, turn it on!
#>sudo systemctl start rocketService.service

If you want it to start automatically then do this as well:
#>sudo systemctl enable rocketService.service

You may also want to adjust the firewall
#>sudo ufw allow 3001
#>sudo ufw disable
#>sudo ufw enable

This ensures that it’ll have full access.

Happy chatting!