https://github.com/mpolinowski/go-mqtt-client
Go MQTT Client (paho.mqtt.golang)
https://github.com/mpolinowski/go-mqtt-client
golang mqtt-client paho-mqtt
Last synced: about 1 month ago
JSON representation
Go MQTT Client (paho.mqtt.golang)
- Host: GitHub
- URL: https://github.com/mpolinowski/go-mqtt-client
- Owner: mpolinowski
- Created: 2021-08-08T12:48:12.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-08-08T12:48:15.000Z (almost 5 years ago)
- Last Synced: 2025-01-28T19:17:57.851Z (over 1 year ago)
- Topics: golang, mqtt-client, paho-mqtt
- Language: Go
- Homepage: https://mpolinowski.github.io/devnotes/2021-06-03--mqtt-with-golang
- Size: 90.8 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Go Paho MQTT Client
In this example we provide example code for TCP, Websocket, TLS, and Websockets protocols to connect to the MQTT Broker. For more documentation on the use of the go paho-mqtt client, see the [Go Client - documentation](https://godoc.org/github.com/eclipse/paho.mqtt.golang)
- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [Run](#run)
- [Parameter](#parameter)
## Prerequisites
```bash
go version
go version go1.16.6 windows/amd64
```
## Installation
* __github.com/eclipse/paho.mqtt.golang__: `v1.3.5`
* __github.com/gorilla/websocket__: `v1.4.2`
* __golang.org/x/net__: `v0.0.0-20210805182204-aaa1db679c0d`
```shell script
go get github.com/eclipse/paho.mqtt.golang
go get github.com/gorilla/websocket
go get golang.org/x/net/proxy
```
## Run
Add your broker data to `.main.go`:
```go
var Host = flag.String("host", "192.168.2.111", "server hostname or IP")
var Port = flag.Int("port", 8885, "server port")
var Action = flag.String("action", "pubsub", "pub/sub/pubsub action")
var Protocol = flag.String("protocol", "ws", "mqtt/mqtts/ws/wss")
var Topic = flag.String("topic", "cameras/go", "publish/subscribe topic")
var Username = flag.String("username", "admin", "mqtt broker username")
var Password = flag.String("password", "instar", "mqtt broker password")
var Qos = flag.Int("qos", 1, "MQTT QOS")
var Tls = flag.Bool("tls", false, "Enable TLS/SSL")
var CaCert = flag.String("cacert", "./broker.ca.crt", "tls certificate")
```
> In the example above I am using [this Mosquitto broker](https://mpolinowski.github.io/devnotes/2021-06-02--mqtt-cheat-sheet) with active [Websocket interface](https://mpolinowski.github.io/devnotes/2021-06-01--mqtt-with-reactjs#activating-websockets).
```shell script
go run main.go
```

### Parameter
You can also override the default values by adding those parameters to the run command:
```shell script
go run main.go -host "192.168.2.111" -port 8885 -protocol 'ws' -action 'pubsub' -topic 'cameras/go' -qos 1 -username 'admin' -password 'instar' -tls false -cacert './broker.ca.crt'
```