Package broker provides an extensible MQTT 3.1.1 broker implementation.
Get it using go's standard toolset:
$ go get github.com/gomqtt/broker
server, err := transport.Launch("tcp://localhost:8080")
if err != nil {
panic(err)
}
engine := NewEngine()
engine.Accept(server)
c := client.New()
wait := make(chan struct{})
c.Callback = func(msg *packet.Message, err error) {
if err != nil {
panic(err)
}
fmt.Println(msg.String())
close(wait)
}
cf, err := c.Connect(client.NewOptions("tcp://localhost:8080"))
if err != nil {
panic(err)
}
cf.Wait()
sf, err := c.Subscribe("test", 0)
if err != nil {
panic(err)
}
sf.Wait()
pf, err := c.Publish("test", []byte("test"), 0, false)
if err != nil {
panic(err)
}
pf.Wait()
<-wait
err = c.Disconnect()
if err != nil {
panic(err)
}
err = server.Close()
if err != nil {
panic(err)
}
engine.Close()
// Output:
// <Message Topic="test" QOS=0 Retain=false Payload=[116 101 115 116]>