Skip to content
ashtonianPublic

About

Low-overhead MQTT v5 client for Go: allocation-free PUBLISH decoding and QoS 0 publishing, reconnect and session resume built in, QoS 1/2 that survives restarts, durable publishing, multi-broker failover and fan-out. Standard-library core.

Topics

Resources

Contributing

Security policy

Stars

6 stars

Watchers

0 watching

Forks

Latest commit

 

History

92 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mqttv5

Go Reference CI License

An idiomatic MQTT v5 client for Go with built-in reconnects, typed payloads, durable sessions, and support for multiple brokers.

Requires Go 1.27 or later. Pre-1.0: minor releases may change the API; see stability.

Why mqttv5

  • Standard-library-only core. Persistence, WebSocket transport and payload codecs are optional submodules.
  • Built-in supervisor. Reconnects, session resumption, in-flight replay and re-subscription are part of every Client.
  • Delivery you control. Channels, bounded queues or callbacks, with manual acknowledgements and per-subscription backpressure.
  • Typed payloads and enhanced authentication. JSON or msgpack encoding, custom codecs, rotating credentials and re-authentication.
  • Durable publishing. Optional file-backed queues and session stores continue publishing through outages and process restarts.
  • Multiple brokers. Failover, parallel sessions and publish pools cover different connection patterns.

See the comparison with Paho, reliability guarantees, and broker conformance suite.

Install

go get github.com/ashtonian/mqttv5

Quick start

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/ashtonian/mqttv5"
)

func main() {
    ctx := context.Background()

    cli, err := mqttv5.New(
        mqttv5.WithBroker("mqtt://localhost:1883"),
        mqttv5.WithClientID("quickstart"),
    )
    if err != nil {
        log.Fatal(err)
    }
    if err := cli.Connect(ctx); err != nil {
        log.Fatal(err)
    }
    defer cli.Disconnect(ctx)

    // Reconnects, session resumption and re-subscription happen underneath.
    msgs, _, err := cli.Subscribe(ctx, []mqttv5.TopicFilter{{Topic: "sensors/#", QoS: 1}})
    if err != nil {
        log.Fatal(err)
    }

    err = cli.Publish(ctx, mqttv5.PublishOptions{Topic: "sensors/a1", QoS: 1, Payload: []byte("22.5")})
    if err != nil {
        log.Fatal(err)
    }

    m := <-msgs
    fmt.Printf("%s: %s\n", m.Topic, m.Payload)
    _ = m.Ack() // the broker gets its PUBACK once the message is acked
}

Run it against a local broker:

docker run -d -p 1883:1883 eclipse-mosquitto mosquitto -c /mosquitto-no-auth.conf
go -C examples run ./readme/quickstart

The example uses channel delivery: call Ack after processing each message. For a consumer resuming a durable session, subscribe before connecting so queued messages have a handler as soon as they arrive.

Multiple brokers

Need API Connections
Fail over among interchangeable brokers or cluster nodes WithBrokers One at a time
Reach independent brokers with separate data ClientGroup One per member, all live
Increase concurrent publishing to the client's broker(s) WithPublisherPool Additional publish-only connections

These patterns compose. See the multi-broker guide, publish pools, and recipes for bridging, regional HA, quorum writes and sharding.

Performance

Allocations per Publish call with a 1 KiB payload, measured against the same pinned mosquitto broker. QoS 0 waits for the packet to be written (PublishWaitForFlush); QoS 1/2 wait for the broker's acknowledgement. autopaho is MQTT 5; paho3 is MQTT 3.1.1.

qos mqttv5 autopaho paho3
0 0 15 23
1 5 53 42
2 6 92 62

allocs/op: median of 10 runs ±95% CI; recorded 2026-10-09 at 80beb93780a2.

See the full benchmarks for decoding and delivery allocations, throughput, latency, methodology and reproduction commands.

Documentation

Task Read
Publish and receive messages Publishing, subscriptions, typed payloads
Keep messages through outages Durable publishing, reliability
Add authentication or another transport Authentication, WebSocket
Connect multiple brokers Multi-broker guide, recipes
Find options, errors and defaults Configuration reference, Go API
Operate a client Metrics and troubleshooting, security
Try runnable code Examples
Contribute Build and test, architecture

The documentation index also covers request/response, disconnecting, optional modules and dependencies.

License

Apache 2.0 — see LICENSE and NOTICE. Independent, clean-room implementation of the MQTT v5.0 specification.

About

Low-overhead MQTT v5 client for Go: allocation-free PUBLISH decoding and QoS 0 publishing, reconnect and session resume built in, QoS 1/2 that survives restarts, durable publishing, multi-broker failover and fan-out. Standard-library core.

Topics

Resources

Contributing

Security policy

Stars

6 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages