The fully compliant, embeddable high-performance Go MQTT v5 server for IoT, smarthome, and pubsub
The fully compliant, embeddable high-performance Go MQTT v5 server for IoT, smarthome, and pubsub
English | 简体中文 | 日本語 | Translators Wanted!
mochi-co/mqtt is now part of the new mochi-mqtt organisation. Read about this announcement here.
Mochi MQTT is an embeddable fully compliant MQTT v5 broker server written in Go, designed for the development of telemetry and internet-of-things projects. The server can be used either as a standalone binary or embedded as a library in your own applications, and has been designed to be as lightweight and fast as possible, with great care taken to ensure the quality and maintainability of the project.
MQTT stands for MQ Telemetry Transport. It is a publish/subscribe, extremely simple and lightweight messaging protocol, designed for constrained devices and low-bandwidth, high-latency or unreliable networks (Learn more). Mochi MQTT fully implements version 5.0.0 of the MQTT protocol.
Because of the overlap between the v5 specification and previous versions of mqtt, the server can accept both v5 and v3 clients, but note that in cases where both v5 an v3 clients are connected, properties and features provided for v5 clients will be downgraded for v3 clients (such as user properties).
Support for MQTT v3.0.0 and v3.1.1 is considered hybrid-compatibility. Where not specifically restricted in the v3 specification, more modern and safety-first v5 behaviours are used instead - such as expiry for inflight and retained messages, and clients - and quality-of-service flow control limits.
Unless it's a critical issue, new releases typically go out over the weekend.
Mochi MQTT can be used as a standalone broker. Simply checkout this repository and run the cmd/main.go entrypoint in the cmd folder which will expose tcp (:1883), websocket (:1882), and dashboard (:8080) listeners.
cd cmd
go build -o mqtt && ./mqttYou can now pull and run the official Mochi MQTT image from our Docker repo:
docker pull mochimqtt/server
or
docker run -v $(pwd)/config.yaml:/config.yaml mochimqtt/server For most use cases, you can use File Based Configuration to configure the server, by specifying a valid yaml or json config file.
A simple Dockerfile is provided for running the cmd/main.go Websocket, TCP, and Stats server, using the allow-all auth hook.
docker build -t mochi:latest .
docker run -p 1883:1883 -p 1882:1882 -p 8080:8080 -v $(pwd)/config.yaml:/config.yaml mochi:latestYou can use File Based Configuration with either the Docker image (described above), or by running the build binary with the --config=config.yaml or --config=config.json parameter.
Configuration files provide a convenient mechanism for easily preparing a server with the most common configurations. You can enable and configure built-in hooks and listeners, and specify server options and compatibilities:
listeners:
- type: "tcp"
id: "tcp12"
address: ":1883"
- type: "ws"
id: "ws1"
address: ":1882"
- type: "sysinfo"
id: "stats"
address: ":1880"
hooks:
auth:
allow_all: true
options:
inline_client: truePlease review the examples found in examples/config for all available configuration options.
There are a few conditions to note:
If you need to implement custom hooks or listeners, please do so using the traditional manner indicated in cmd/main.go.
Importing Mochi MQTT as a package requires just a few lines of code to get started.
…Examples of running the broker with various configurations can be found in the examples folder.
The server comes with a variety of pre-packaged network listeners which allow the broker to accept connections on different protocols. The current listeners are:
| Listener | Usage |
|---|---|
| listeners.NewTCP | A TCP listener |
| listeners.NewUnixSock | A Unix Socket listener |
| listeners.NewNet | A net.Listener listener |
| listeners.NewWebsocket | A Websocket listener |
| listeners.NewHTTPStats | An HTTP $SYS info dashboard |
| listeners.NewHTTPHealthCheck | An HTTP healthcheck listener to provide health check responses for e.g. cloud infrastructure |
Use the
listeners.Listenerinterface to develop new listeners. If you do, please let us know!
A *listeners.Config may be passed to configure TLS.
Examples of usage can be found in the examples folder or cmd/main.go.
A number of configurable options are available which can be used to alter the behaviour or restrict access to certain features in the server.
server := mqtt.New(&mqtt.Options{
Capabilities: mqtt.Capabilities{
MaximumSessionExpiryInterval: 3600,
MaximumClientWritesPending: 3,
Compatibilities: mqtt.Compatibilities{
ObscureNotAuthorized: true,
},
},
ClientNetWriteBufferSize: 4096,
ClientNetReadBufferSize: 4096,
SysTopicResendInterval: 10,
InlineClient: false,
})Review the mqtt.Options, mqtt.Capabilities, and mqtt.Compatibilities structs for a comprehensive list of options. ClientNetWriteBufferSize and ClientNetReadBufferSize can be configured to adjust memory usage per client, based on your needs. The size of Capabilities.MaximumClientWritesPending will affect the memory usage of the server. If the number of IoT devices online at the same time is large, and the set value is very large, even if there is no data transmission, the memory usage of the server will increase a lot. The default value is 1024*8, and this parameter can be adjusted according to the actual situation.
Some choices were made when deciding the default configuration that need to be mentioned here:
server.Options.Capabilities.MaximumMessageExpiryInterval is set to 86400 (24 hours), in order to prevent exposing the broker to DOS attacks on hostile networks when using the out-of-the-box configuration (as an infinite expiry would allow an infinite number of retained/inflight messages to accumulate). If you are operating in a trusted environment, or you have capacity for a larger retention period, you may wish to override this (set to 0 for no expiry).A universal event hooks system allows developers to hook into various parts of the server and client life cycle to add and modify functionality of the broker. These universal hooks are used to provide everything from authentication, persistent storage, to debugging tools.
Hooks are stackable - you can add multiple hooks to a server, and they will be run in the order they were added. Some hooks modify values, and these modified values will be passed to the subsequent hooks before being returned to the runtime code.
| Type | Import | Info |
|---|---|---|
| Access Control | mochi-mqtt/server/hooks/auth . AllowHook | Allow access to all connecting clients and read/write to all topics. |
| Access Control | mochi-mqtt/server/hooks/auth . Auth | Rule-based access control ledger. |
| Persistence | mochi-mqtt/server/hooks/storage/bolt | Persistent storage using BoltDB (deprecated). |
| Persistence | mochi-mqtt/server/hooks/storage/badger | Persistent storage using BadgerDB. |
| Persistence | mochi-mqtt/server/hooks/storage/pebble | Persistent storage using PebbleDB. |
| Persistence | mochi-mqtt/server/hooks/storage/redis | Persistent storage using Redis. |
| Debugging | mochi-mqtt/server/hooks/debug | Additional debugging output to visualise packet flow. |
Many of the internal server functions are now exposed to developers, so you can make your own Hooks by using the above as examples. If you do, please Open an issue and let everyone know!
By default, Mochi MQTT uses a DENY-A
No open issues yet, or sync has not completed.