---
title: "Deploy Mosquitto — Self-Hosted MQTT Broker for IoT"
description: "Self-host Mosquitto: lightweight MQTT broker for IoT & Home Assistant."
category: "Queues"
url: https://railway.com/deploy/mosquitto-mqtt-broker
---

# Deploy Mosquitto — Self-Hosted MQTT Broker for IoT

Self-host Mosquitto: lightweight MQTT broker for IoT & Home Assistant.

**[Deploy Mosquitto — Self-Hosted MQTT Broker for IoT on Railway](https://railway.com/template/mosquitto-mqtt-broker)**

- **Creator:** SilverBanana
- **Category:** Queues
- **Total deploys:** 1

## Template content

### Mosquitto https://raw.githubusercontent.com/docker-library/docs/757578e3a44e5460a8a11d32a81776f8b74231a9/eclipse-mosquitto/logo.png

- **Source:** Lima-e-Silva/mosquitto_railway_template

## Documentation

# Deploy and Host Mosquitto on Railway

Eclipse Mosquitto is the most widely used open-source MQTT broker — the lightweight pub/sub
backbone for IoT devices, sensors, home automation, and machine-to-machine messaging. Devices
publish to topics, subscribers receive them, and Mosquitto routes it all with QoS delivery
guarantees and retained messages, so devices reconnecting after downtime still get the latest
state. It supports MQTT 5.0, 3.1.1, and 3.1, plus WebSockets and TLS, and it's famously tiny —
around 3MB RAM for 1,000 connected clients, with tested deployments up to 100,000 clients.

This template deploys Mosquitto with password authentication and persistent storage, pre-wired on
Railway. Connect any MQTT client, Home Assistant, or Node-RED, and run an always-on broker for
~$5/month — unlimited connections and messages, with no per-device cloud fees.

---

## What This Template Deploys

| Service | Purpose |
|---|---|
| **Eclipse Mosquitto** | The MQTT broker — MQTT on port `1883`, MQTT-over-WebSocket on `9001`, backed by config and data volumes |
| **Persistent Volume** | Mounts `/mosquitto/config` and `/mosquitto/data` — your config, password file, retained messages, and sessions survive redeploys |

Single tiny service built in C — negligible footprint, so it runs cheaply on the smallest Railway
instance. Password authentication is configured so the broker isn't open to the world; retained
messages and QoS state persist on the data volume.

---

## About Hosting Mosquitto

MQTT is the standard IoT protocol — lightweight, low-bandwidth, reliable on flaky networks — and
Mosquitto is its reference broker. Any project with sensors, smart-home devices, microcontrollers,
or mobile clients wants an MQTT broker in the middle, decoupling publishers from subscribers.

Running it means a persistent broker, a config file, storage for retained messages, and network
access for devices. Railway runs Mosquitto as a managed container with HTTPS on WebSockets and a
persistent volume — set your credentials, connect devices, and you have an always-on broker without
keeping a Raspberry Pi or home server running 24/7.

> **Config and security note:** Mosquitto needs a config file to do anything useful, and by default
> a secure setup requires `allow_anonymous false` plus a password file (created with
> `mosquitto_passwd`). This template ships with authentication configured so the broker isn't open
> on first boot — always set your own credentials before connecting production devices.

Typical cost: **~$5/month** on Railway's Hobby plan — Mosquitto's footprint is negligible. Hosted
MQTT services like HiveMQ Cloud and cloud IoT hubs charge per-connection or per-message; self-
hosting gives you unlimited connections and messages at flat compute with full ACL control.

---

## Deploy in Under 3 Minutes

1. Click **Deploy on Railway** — Mosquitto builds automatically (~1 minute)
2. Add a persistent volume for `/mosquitto/config` and `/mosquitto/data`
3. Create a user with `mosquitto_passwd` and set `allow_anonymous false` in the config
4. Connect an MQTT client to your Railway host on port `1883` (or `9001` for WebSocket)
5. Publish and subscribe — `mosquitto_pub`/`mosquitto_sub`, Home Assistant, Node-RED, or any SDK

No compilation. No server administration. No reverse proxy for WebSockets.

---

## Common Use Cases

- **IoT sensor and device messaging** — the pub/sub backbone for temperature sensors, smart plugs,
  microcontrollers, and any device that speaks MQTT, at unlimited scale with no per-device fees
- **Home automation broker for Home Assistant** — Mosquitto is the recommended MQTT broker for
  Home Assistant, Node-RED, and Zigbee2MQTT; host it in the cloud instead of on a home server
- **Machine-to-machine (M2M) communication** — decouple services and embedded systems with
  lightweight pub/sub messaging that works reliably over constrained or intermittent networks
- **Mobile and browser pub/sub** — MQTT over WebSocket (port `9001`) lets browser and mobile apps
  publish and subscribe directly, enabling live updates without polling
- **Bridging MQTT networks** — Mosquitto's bridge mode connects distributed brokers, passing
  messages between locations to build a resilient multi-site MQTT network

---

## Configuration

| Variable / Setting | Required | Description |
|---|---|---|
| `mosquitto.conf` | ✅ Required | The config file on the volume — defines listeners, auth, and persistence |
| `listener 1883` | ✅ Pre-set | The standard MQTT port |
| `listener 9001` (websockets) | Optional | MQTT over WebSocket for browser/mobile clients |
| `allow_anonymous false` | ✅ Recommended | Require authentication — set with a password file |
| `password_file` | ✅ Recommended | Path to the `mosquitto_passwd`-generated credentials file |
| `persistence true` | ✅ Pre-set | Persist retained messages and sessions to `/mosquitto/data` |
| `listener 8883` (TLS) | Optional | MQTT over TLS — add certificates for encrypted device connections |

> Mosquitto is configured via `mosquitto.conf` on the mounted config volume, not environment
> variables. Set `allow_anonymous false` and create a password file with `mosquitto_passwd` before
> exposing the broker — an open MQTT broker on the public internet is a security risk. Mount both
> config and data volumes so credentials and retained messages survive redeploys.

---

## Mosquitto vs. MQTT Broker Alternatives

| | Mosquitto (Railway) | HiveMQ Cloud | AWS IoT Core | EMQX |
|---|---|---|---|---|
| **Pricing** | ~$5/mo flat | Per-connection | Per-message | Self-host |
| **Self-hosted / own data** | ✅ Yes | ❌ Cloud | ❌ AWS | ✅ Yes |
| **Footprint** | ✅ Tiny (~3MB/1k clients) | N/A | N/A | ⚠️ Heavier |
| **MQTT 5.0 / 3.1.1** | ✅ Both | ✅ Both | ✅ Both | ✅ Both |
| **WebSocket support** | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |
| **Per-device fees** | ✅ None | ❌ Yes | ❌ Yes | ✅ None |
| **Home Assistant / Node-RED** | ✅ Recommended broker | ⚠️ Works | ⚠️ Complex | ✅ Works |
| **Open source** | ✅ EPL/EDL | ❌ No | ❌ No | ✅ Apache-2.0 |

---

## Dependencies for Mosquitto Hosting

- Railway account — Hobby plan (~$5/month); Mosquitto's footprint is negligible
- Persistent volumes for `/mosquitto/config` (config + password file) and `/mosquitto/data`
- An MQTT client — `mosquitto_pub`/`mosquitto_sub`, Home Assistant, Node-RED, or any SDK

### Deployment Dependencies

- [Eclipse Mosquitto](https://mosquitto.org) — documentation and downloads
- [Mosquitto Docker Image](https://hub.docker.com/_/eclipse-mosquitto) — the official `eclipse-mosquitto` image
- [Mosquitto GitHub](https://github.com/eclipse-mosquitto/mosquitto) — source and issues
- [Railway Volumes Documentation](https://docs.railway.com/volumes) — config and message persistence

### Implementation Details

This template deploys the official `eclipse-mosquitto` image with persistent volumes at
`/mosquitto/config` (config and password file) and `/mosquitto/data` (retained messages, sessions).
Mosquitto is a compact C binary — ~3MB RAM for 1,000 clients — so it runs on the smallest Railway
instance. MQTT is on `1883`; enable a WebSocket listener on `9001` for browser/mobile clients.

Config lives in `mosquitto.conf`: set `allow_anonymous false` with a `password_file` (via
`mosquitto_passwd`) so the broker requires auth, and `persistence true` (pre-set) so retained
messages survive restarts. Add a TLS listener on `8883` for encrypted connections. Config and data
volumes persist across redeploys.

---

## Frequently Asked Questions

**What is Mosquitto used for?**
Mosquitto is an MQTT broker — the central hub for pub/sub messaging between IoT devices, sensors,
and applications. Devices publish messages to topics; subscribers receive them; Mosquitto routes it
all with QoS guarantees. It's the standard for IoT sensor networks, home automation, and any
low-bandwidth machine-to-machine messaging.

**Is Mosquitto good for Home Assistant?**
Yes — Mosquitto is the recommended MQTT broker for Home Assistant, Node-RED, and Zigbee2MQTT.
Hosting it on Railway means your automation broker is always on in the cloud rather than depending
on a home server, and reachable from anywhere.

**How do I secure the broker?**
Set `allow_anonymous false` in `mosquitto.conf` and create a password file with `mosquitto_passwd`.
This template ships with authentication configured. For encrypted connections, add a TLS listener
on port `8883` with certificates. Never run an open MQTT broker on the public internet.

**Do I lose retained messages if Railway redeploys?**
No — with `persistence true` (pre-set) and the `/mosquitto/data` volume mounted, retained messages
and session state survive redeploys. Your config and password file persist on the `/mosquitto/config`
volume.

**How many devices can Mosquitto handle?**
Mosquitto is extremely lightweight — roughly 3MB RAM for 1,000 connected clients, with tested
deployments up to 100,000 clients at modest message rates. For most home and small-to-medium IoT
deployments, a single small Railway instance is more than enough.

**Can browsers and mobile apps connect?**
Yes. Enable the WebSocket listener on port `9001` and browser/mobile apps can publish and subscribe
over MQTT-over-WebSocket, enabling real-time updates without polling — Railway provides HTTPS for
the WebSocket endpoint automatically.

---

## Why Deploy and Host Mosquitto on Railway?

Railway is a singular platform to deploy your infrastructure stack. Railway will host your
infrastructure so you don't have to deal with configuration, while allowing you to vertically and
horizontally scale it.

By deploying Mosquitto on Railway, you get the most widely used open-source MQTT broker — MQTT 5.0,
WebSockets, QoS, and retained messages — in a tiny always-on service with persistent storage at
~$5/month flat, with unlimited connections and no per-device fees.

## Similar templates

- [smoothmq](https://railway.com/deploy/AJv-64) — A drop-in replacement for AWS SQS
- [Kafka UI](https://railway.com/deploy/kafka-ui) — Kafbat UI — Open-source web UI to monitor and manage Apache Kafka clusters
- [Hatchet Lite](https://railway.com/deploy/hatchet-lite) — Hatchet Lite with postgres

Open this page in a browser: https://railway.com/deploy/mosquitto-mqtt-broker
