{"id":36586005,"url":"https://github.com/srishina/mqtt.go","last_synced_at":"2026-01-12T08:03:08.718Z","repository":{"id":53567358,"uuid":"330200037","full_name":"srishina/mqtt.go","owner":"srishina","description":"mqtt.go is a client library for the MQTTv5 protocol, written in Go. The users of the client library can use pull or push mechanism to receive messages.","archived":false,"fork":false,"pushed_at":"2024-09-17T01:41:01.000Z","size":291,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-09-17T05:26:57.847Z","etag":null,"topics":["client","go","golang","golang-library","library","mqtt","mqtt-client","mqtt-protocol","mqtt5","mqttv5"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/srishina.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-01-16T16:01:10.000Z","updated_at":"2024-09-17T01:41:04.000Z","dependencies_parsed_at":"2024-06-21T10:08:43.778Z","dependency_job_id":null,"html_url":"https://github.com/srishina/mqtt.go","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/srishina/mqtt.go","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srishina%2Fmqtt.go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srishina%2Fmqtt.go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srishina%2Fmqtt.go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srishina%2Fmqtt.go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/srishina","download_url":"https://codeload.github.com/srishina/mqtt.go/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srishina%2Fmqtt.go/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28336987,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-12T06:09:07.588Z","status":"ssl_error","status_checked_at":"2026-01-12T06:05:18.301Z","response_time":98,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["client","go","golang","golang-library","library","mqtt","mqtt-client","mqtt-protocol","mqtt5","mqttv5"],"created_at":"2026-01-12T08:03:07.949Z","updated_at":"2026-01-12T08:03:08.713Z","avatar_url":"https://github.com/srishina.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mqtt.go\nMQTTv5 client and server library\n\nGo implementation of MQTTv5 protocol\n\n(server library is work in progress)\n\n# Installation\n\n```bash\n# Go client\ngo get github.com/srishina/mqtt.go\n```\n\n# Run the tests\n```bash\ngo test ./... -race -v\n```\nnote: few test can take time, namely, TestBasicWithKeepAlive, TestPublishAfterReconnectWithSession, TestPublishAfterReconnectWithoutSession\n\n# Try out the examples\n```bash\ncd ./examples\n```\n\nConnect to a broker(basic client):\n```bash\ngo run ./basic-client/main.go -b ws://mqtt.eclipseprojects.io:80/mqtt -k 120 -cs=true // keep alive = 120secs, clean start=true\n```\nPublish a message:\n```bash\ngo run ./client-pub/main.go -b ws://mqtt.eclipseprojects.io:80/mqtt \"TEST/GREETING\" 1 \"Willkommen\"\n```\nSubscribe to a message:\n```bash\ngo run ./client-sub/main.go -b ws://mqtt.eclipseprojects.io:80/mqtt \"TEST/GREETING/#\" 1\n```\nWill message\n```bash\ngo run ./client-will-msg/main.go -b ws://mqtt.eclipseprojects.io:80/mqtt --will-delay-interval 5 \"TEST/GREETING/WILL\" 1 \"The Will message\" \"TEST/GREETING/#\" 1\n```\n# Network connection - client\n\nThe client library provides a possibility to provision a connection. The implementation of the \"Connection\" interface needs to be passed when initializing the client.\n```go\n// Connection represents a connection that the MQTT client uses.\n// The implementation of the Connection is responsible for\n// initialization of the connection(tcp, ws etc...) with the broker.\n// WebsocketConn, TCPConn is provided as part of the library, other\n// connections can be written by the implementations\ntype Connection interface {\n\tBrokerURL() string\n\t// Connect MQTT client calls Connect when it needs a io read writer.\n\t// If the Connect returns an error during reconnect then the MQTT client will\n\t// attempt a reconnect again. The reconnect interval depends on backoff delay\n\tConnect(ctx context.Context) (io.ReadWriter, error)\n\t// Closes the network connection\n\tClose()\n}\n```\n\nWebsocketConn, TCPConn implementations are provided as part of the library.\n```go\nu, err := url.Parse(broker)\nif err != nil {\n\tlog.Fatal(err)\n}\n\nvar conn mqtt.Connection\nswitch u.Scheme {\ncase \"ws\":\n\tfallthrough\ncase \"wss\":\n\tconn = \u0026mqtt.WebsocketConn{Host: broker}\ncase \"tcp\":\n\tconn = \u0026mqtt.TCPConn{Host: u.Host}\ndefault:\n\tlog.Fatal(\"Invalid scheme name\")\n}\nvar opts []mqtt.ClientOption\nopts = append(opts, mqtt.WithCleanStart(cleanStart))\nopts = append(opts, mqtt.WithKeepAlive(uint16(keepAlive)))\nopts = append(opts, mqtt.WithClientID(clientID))\n\nclient := mqtt.NewClient(conn, opts...)\n```\n\nIf the default implementations are not suitable and then more sophisticated implementations can be provisioned.\n\n\n# Subscriber overview - client\n\nIn order to receive messages published to a topic, the client needs to subscribe to the interesting topics. The client can either use push or pull mechanism to receive messages. In the pull model the client can decide when to read the messages. The messages are queued internally in the library. The client may run the message receiver in a separate go routine. In the push model the library delivers message to the client asynchronously as the PUBLISH messages are received.\n\n## Pull model\n```go\nrecvr := mqtt.NewMessageReceiver()\nvar wg sync.WaitGroup\nwg.Add(1)\ngo func() {\n\tdefer wg.Done()\n\tfor {\n\t\tp, err := recvr.Recv()\n\t\tif err != nil {\n\t\t\treturn\n\t\t}\n\t\tlog.Printf(\"PUBLISH recvd - Topic: %s QoS: %d Payload: %v\\n\", p.TopicName, p.QoSLevel, string(p.Payload))\n\t}\n}()\n// subscribe\nsubscriptions := []*mqtt.Subscription{}\nsubscriptions = append(subscriptions, \u0026mqtt.Subscription{TopicFilter: \"TEST/GREETING/#\", QoSLevel: 2})\n\nsuback, err := client.Subscribe(context.Background(), subscriptions, nil, recvr)\nif err != nil {\n\tlog.Fatal(err)\n}\n```\n\n## Push model\n```go\n\n// The messages are delivered asynchronously. The library does not order messages in this case. The messages\n// are delivered as it arrives. The callbacks are executed from the library using a go routine.\n\ns := []*Subscription{{TopicFilter: \"TEST/GREETING/#\", QoSLevel: 2}}\nsuback, err := client.CallbackSubscribe(context.Background(), s, nil, func(m *Publish) {\n\tlog.Printf(\"PUBLISH received - Topic: %s QoS: %d Payload: %v\\n\", p.TopicName, p.QoSLevel, string(p.Payload))\n})\n\n```\n\n# How the network reconnect is handled in the library?\n\nThe client library supports reconnecting and automatically resubscribe / publish the pending messages.\n\nMQTTv5 supports the possibility to set whether the session that is initiated with the broker should be clean or a continuation of the last session. In the later case, the session unique identifier is used. The specification also provides an extra property through which the client or the broker can decide how long a session should be kept. The client can set a session expiry interval. However, if the browser specifies a session expiry interval then that value takes precedence. If the client or broker does not specify session expiry interval then the session state is lost when the network connection is dropped.\n\nSo in summary, clean start + the session expiry interval + the CONNACK response from the broker determines how the client reconnects.\n\nThe library operates as below:\n\nIf the network connection is dropped, the library tries to reconnect with the broker with the CONNECT packet set by client. At the moment, the library does not provide a mechanism to override the CONNECT packet. Based on the broker response the client will perform one of the below.\n\n1. If the broker still has the session state, then the pending messages will be send, which can also include partial PUBLISH messages with QoS 2. No resubscription is needed as broker has the subscriptions.\n2. If the broker has no session state, then the client library resubscribes to the already subscribed topics and send pending messages. For QoS 1 \u0026 2 the library restarts the publish flow again. Note that, in this scenario the resubscription may fail and the client will be notified of the status of the resubscription.\n\nConnection retry uses exponential backoff with jitter.\n\n```go\nvar opts []ClientOption\nopts = append(opts, WithInitialReconnectDelay(50))\n// other as needed\nclient := NewClient(mqttMock, opts...)\n\nplease see func WithInitialReconnectDelay, WithMaxReconnectDelay, WithReconnectJitter for more information\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsrishina%2Fmqtt.go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsrishina%2Fmqtt.go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsrishina%2Fmqtt.go/lists"}