Go

Description

Go/Golang client can be found on https://github.com/emitter-io/go. This library provides a nicer MQTT interface fine-tuned and extended with specific features provided by Emitter. The code uses the Eclipse Paho MQTT Go Client for handling all the network communication and MQTT protocol, and is released under the same license (EPL v1).

Go Language

Installation

The installation of the library can be done simply by executing go get command, as shown below.

go get github.com/emitter-io/go

Usage

This library aims to be as simple and straighforward as possible. First thing you'll need to do is to import it. Then, you can use the functions exposed by Emitter type - they are simple methods such as Connect, Publish, Subscribe, Unsubscribe, GenerateKey, Presence, etc. See the example below.

func main() {
	// Create the options with default values
	o := emitter.NewClientOptions()

	// Set the message handler
	o.SetOnMessageHandler(func(client emitter.Emitter, msg emitter.Message) {
		fmt.Printf("Received message: %s\n", msg.Payload())
	})

	// Create a new emitter client and connect to the broker
	c := emitter.NewClient(o)
	sToken := c.Connect()
	if sToken.Wait() && sToken.Error() != nil {
		panic("Error on Client.Connect(): " + sToken.Error().Error())
	}

	// Subscribe to the presence demo channel
	c.Subscribe("X4-nUeHjiAygHMdN8wst82S3c2KcCMn7", "presence-demo/1")

	// Publish to the channel
	c.Publish("X4-nUeHjiAygHMdN8wst82S3c2KcCMn7", "presence-demo/1", "hello")

	// Ask for presence
	r := emitter.NewPresenceRequest()
	r.Key = "X4-nUeHjiAygHMdN8wst82S3c2KcCMn7"
	r.Channel = "presence-demo/1"
	c.Presence(r)
}