Deploy Eclipse Mosquitto

Password-auth MQTT broker with dashboard, TCP, and WebSockets.

Deploy Eclipse Mosquitto

/mosquitto/data

Deploy and Host Eclipse Mosquitto with Railway

Eclipse Mosquitto is the MQTT broker used by devices and backend services to publish and subscribe to messages. This template runs Mosquitto 2.1.4 with password authentication, a volume for retained messages and sessions, MQTT on a TCP proxy, and MQTT over WebSockets on the public domain.

About Hosting Eclipse Mosquitto

Anonymous clients are refused. One user is created from service variables on every start. Retained messages and persistent sessions are stored on a volume at /mosquitto/data. A small gateway on the public domain serves the dashboard and /healthz, and passes WebSocket upgrades through to the broker.

Common Use Cases

  • IoT telemetry from devices that speak MQTT
  • A message bus for other services on Railway's private network
  • Browser clients that connect with MQTT.js over secure WebSockets
  • A dashboard for connected clients, traffic, and $SYS charts

Dependencies for Eclipse Mosquitto Hosting

  • Eclipse Mosquitto 2.1.4, pinned by image digest
  • A Railway volume mounted at /mosquitto/data

Deployment Dependencies

Why Deploy Eclipse Mosquitto on Railway?

Railway provides the TCP proxy devices need, the HTTPS domain browsers need, the private network other services need, and a volume so retained messages survive restarts. The template starts authenticated, with /healthz checking a real MQTT login.

EndpointAddressUse
MQTT, privatemqtt://${{RAILWAY_PRIVATE_DOMAIN}}:1883Other services in the same Railway environment
MQTT, public TCPmqtt://${{RAILWAY_TCP_PROXY_DOMAIN}}:${{RAILWAY_TCP_PROXY_PORT}}Devices and tools outside Railway
MQTT over WebSocketswss://${{RAILWAY_PUBLIC_DOMAIN}}/mqttBrowsers and MQTT.js
Dashboardhttps://${{RAILWAY_PUBLIC_DOMAIN}}/Mosquitto 2.1 dashboard. Same username and password.

The public TCP proxy is raw TCP. A password on that endpoint is not encrypted. Use wss:// for clients that can speak MQTT over WebSockets. Use the private hostname for anything else running on Railway.

Deploy

  1. Create a Railway service from this repo. The Dockerfile is the builder.
  2. Add a volume mounted at /mosquitto/data.
  3. Generate a public domain. Railway sends HTTP and WebSocket traffic to PORT (default 8080).
  4. Add a TCP proxy whose application port is 1883.
  5. Set the variables below, then deploy.

/healthz logs in to the broker over MQTT. The deploy is healthy only after that login succeeds.

Variables

VariableDefaultPurpose
MQTT_USERNAMEmqttBroker user. Letters, digits, and . _ @ -.
MQTT_PASSWORDgenerated on the volumePassword rewritten on every start. Set this so it stays stable.
MQTT_MAX_CONNECTIONS-1Per-listener connection cap. -1 is unlimited.
MQTT_DASHBOARDtrueServe the built-in dashboard on the public domain. Set false to leave / as a short status page.
MQTT_DASHBOARD_TRUST_HEADERemptyWhen set, the dashboard is served only if this request header is present. /healthz and /mqtt stay open.
MQTT_DASHBOARD_TRUST_SECRETemptyWhen set, that header must equal this value. A secret without a header name uses X-Dashboard-Token.
PORT8080HTTP port for the dashboard, /healthz, and /mqtt.

Suggested references for other services in the same project:

MQTT_USERNAME=mqtt
MQTT_PASSWORD=${{secret(32, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")}}
MQTT_URL=mqtt://${{RAILWAY_PRIVATE_DOMAIN}}:1883
MQTT_PUBLIC_URL=mqtt://${{RAILWAY_TCP_PROXY_DOMAIN}}:${{RAILWAY_TCP_PROXY_PORT}}
MQTT_WEBSOCKET_URL=wss://${{RAILWAY_PUBLIC_DOMAIN}}/mqtt

MQTT_URL, MQTT_PUBLIC_URL, and MQTT_WEBSOCKET_URL are connection strings for your apps.

Open the public domain in a browser and sign in with MQTT_USERNAME and MQTT_PASSWORD to see clients, traffic, and $SYS charts. /healthz stays open so Railway can check the broker without those credentials. Set MQTT_DASHBOARD=false and redeploy to turn the dashboard off.

Zero trust

The MQTT password is a shared secret. To put the dashboard behind a login you control, proxy a custom domain with Cloudflare and set:

MQTT_DASHBOARD_TRUST_HEADER=X-Dashboard-Token
MQTT_DASHBOARD_TRUST_SECRET={long-random-value}

On that hostname:

  • Add a Transform Rule that sets the request header X-Dashboard-Token to the same secret. Cloudflare overwrites a client-supplied value, so the secret is only added for traffic that passes through the proxy.
  • In Cloudflare Access, allow your identity provider for the hostname.
  • Add an Access bypass for /mqtt if public WebSocket clients connect without an Access login.
  • Leave /healthz out of Access. Railway calls it on the container, not through Cloudflare.

Delete the *.up.railway.app domain. That hostname skips Cloudflare, so it would skip both Access and the injected header. The TCP proxy on port 1883 is raw TCP and stays outside Access. Devices keep using that proxy or the private hostname. The broker password still applies after Access lets the browser through.

Connect

mosquitto_sub -h {tcp-proxy-host} -p {tcp-proxy-port} -u mqtt -P '{password}' -t 'test/#' -v
mosquitto_pub -h {tcp-proxy-host} -p {tcp-proxy-port} -u mqtt -P '{password}' -t test/hello -m hi

From another Railway service, use mqtt://{private-domain}:1883 with the same username and password.

MQTT.js over the public domain:

import mqtt from "mqtt";

const client = mqtt.connect("wss://{your-domain}/mqtt", {
  username: "mqtt",
  password: process.env.MQTT_PASSWORD,
});

Run locally

docker build -t mosquitto-railway .
docker run --rm -p 1883:1883 -p 8080:8080 \
  -e MQTT_USERNAME=mqtt \
  -e MQTT_PASSWORD=local-dev-password \
  -e PORT=8080 \
  -v mosquitto-data:/mosquitto/data \
  mosquitto-railway
curl -fsS http://127.0.0.1:8080/healthz
mosquitto_pub -h 127.0.0.1 -p 1883 -u mqtt -P local-dev-password -t test/hello -m hi

If MQTT_PASSWORD is omitted, the entrypoint generates one and stores it at /mosquitto/data/.password. It is not printed in the logs.

Publish as a Railway template

template.json is the deploy-form spec. Each entry under services.Mosquitto.variables is a field the deployer can change, with a default and a description, including the generated MQTT_PASSWORD.

Deploy the service once with the volume, the public domain, the TCP proxy on port 1883, and those variables. Then, from a linked CLI session:

railway templates create --project {project} --environment production --json
railway templates publish {template-id} \
  --category Queues \
  --description "Password-auth MQTT broker with dashboard, TCP, and WebSockets." \
  --readme-file README.md \
  --json

Publishing adds the template to the marketplace. The deploy button uses the template code Railway returns:

[![Deploy on Railway](https://railway.com/button.svg)](https://railway.com/deploy/eclipse-mosquitto-1)

Upgrade

Mosquitto is pinned to 2.1.4 by image digest in the Dockerfile. To move versions, change that pin after reading the release notes, then redeploy. The persistence file lives on the volume at /mosquitto/data.


Template Content

More templates in this category

View Template
Celery | Web, Worker and Scheduler as Three Services
Web, worker and scheduler wired up: Celery on Redis, outcomes in Postgres.

Templates Guru
0
View Template
smoothmq
A drop-in replacement for AWS SQS

poundifdef
7
View Template
Kafka UI
Kafbat UI — Open-source web UI to monitor and manage Apache Kafka clusters

codestorm
0