├── .gitignore ├── Gopkg.lock ├── Gopkg.toml ├── Makefile ├── README.md └── main.go /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | -------------------------------------------------------------------------------- /Gopkg.lock: -------------------------------------------------------------------------------- 1 | # This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. 2 | 3 | 4 | [[projects]] 5 | name = "github.com/eclipse/paho.mqtt.golang" 6 | packages = [".","packets"] 7 | revision = "aff15770515e3c57fc6109da73d42b0d46f7f483" 8 | version = "v1.1.0" 9 | 10 | [[projects]] 11 | branch = "master" 12 | name = "golang.org/x/net" 13 | packages = ["proxy","websocket"] 14 | revision = "a04bdaca5b32abe1c069418fb7088ae607de5bd0" 15 | 16 | [solve-meta] 17 | analyzer-name = "dep" 18 | analyzer-version = 1 19 | inputs-digest = "4b1e2f372b614cd9bb649cbca18cc5f79b2ad7ee294c4e85011e0287bfd9dfbe" 20 | solver-name = "gps-cdcl" 21 | solver-version = 1 22 | -------------------------------------------------------------------------------- /Gopkg.toml: -------------------------------------------------------------------------------- 1 | 2 | # Gopkg.toml example 3 | # 4 | # Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md 5 | # for detailed Gopkg.toml documentation. 6 | # 7 | # required = ["github.com/user/thing/cmd/thing"] 8 | # ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] 9 | # 10 | # [[constraint]] 11 | # name = "github.com/user/project" 12 | # version = "1.0.0" 13 | # 14 | # [[constraint]] 15 | # name = "github.com/user/project2" 16 | # branch = "dev" 17 | # source = "github.com/myfork/project2" 18 | # 19 | # [[override]] 20 | # name = "github.com/x/y" 21 | # version = "2.4.0" 22 | 23 | 24 | [[constraint]] 25 | name = "github.com/eclipse/paho.mqtt.golang" 26 | version = "1.1.0" 27 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: deps run 2 | 3 | deps: 4 | @dep ensure 5 | 6 | run: 7 | @go run main.go 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Go MQTT Example 2 | 3 | This is a simple Go example on how to interact with MQTT. 4 | 5 | This example sends a messages every second and the same process receive the message and prints it to the console. 6 | 7 | ## Getting started 8 | 9 | * Make sure you have [dep](https://github.com/golang/dep) installed 10 | * Clone this repo `git clone https://github.com/CloudMQTT/go-mqtt-example.git` 11 | * `export CLOUDMQTT_URL=mqtt://:@.cloudmqtt.com:/` 12 | * Run `make` to download dependencies and run the application 13 | 14 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "net/url" 7 | "os" 8 | "time" 9 | 10 | mqtt "github.com/eclipse/paho.mqtt.golang" 11 | ) 12 | 13 | func connect(clientId string, uri *url.URL) mqtt.Client { 14 | opts := createClientOptions(clientId, uri) 15 | client := mqtt.NewClient(opts) 16 | token := client.Connect() 17 | for !token.WaitTimeout(3 * time.Second) { 18 | } 19 | if err := token.Error(); err != nil { 20 | log.Fatal(err) 21 | } 22 | return client 23 | } 24 | 25 | func createClientOptions(clientId string, uri *url.URL) *mqtt.ClientOptions { 26 | opts := mqtt.NewClientOptions() 27 | opts.AddBroker(fmt.Sprintf("tcp://%s", uri.Host)) 28 | opts.SetUsername(uri.User.Username()) 29 | password, _ := uri.User.Password() 30 | opts.SetPassword(password) 31 | opts.SetClientID(clientId) 32 | return opts 33 | } 34 | 35 | func listen(uri *url.URL, topic string) { 36 | client := connect("sub", uri) 37 | client.Subscribe(topic, 0, func(client mqtt.Client, msg mqtt.Message) { 38 | fmt.Printf("* [%s] %s\n", msg.Topic(), string(msg.Payload())) 39 | }) 40 | } 41 | 42 | func main() { 43 | uri, err := url.Parse(os.Getenv("CLOUDMQTT_URL")) 44 | if err != nil { 45 | log.Fatal(err) 46 | } 47 | topic := uri.Path[1:len(uri.Path)] 48 | if topic == "" { 49 | topic = "test" 50 | } 51 | 52 | go listen(uri, topic) 53 | 54 | client := connect("pub", uri) 55 | timer := time.NewTicker(1 * time.Second) 56 | for t := range timer.C { 57 | client.Publish(topic, 0, false, t.String()) 58 | } 59 | } 60 | --------------------------------------------------------------------------------