Documentation
¶
Overview ¶
Package goqtt is a MQTT 3.1.1 client library.
It does not implement the full client specification, see README for more information.
Use an instantiated Client to interact with a MQTT broker:
client := goqtt.NewClient("example.broker")
err := client.Connect()
...
err := client.Subscribe()
...
err := client.Publish("hello world")
...
mes, err := client.ReadLoop() // call in a 'for' loop for indefinite reading
...
It is nice to let the broker know when you are disconnecting:
err := client.Connect()
if err != nil {
// handle error
}
defer client.Disconnect()
To configure a Client, the only required parameter is the address of the broker. There are default values set for the other options. See the Constants section for their values.
client := goqtt.NewClient("example.broker")
The other configuration options are: Port, Topic, ClientId, and KeepAlive. To set these, use their functions and pass to the NewClient:
p := goqtt.Port("1883")
t := goqtt.Topic("hello/world")
cid := goqtt.ClientId("goqtt-clientId")
ka := goqtt.KeepAlive(15) // seconds
client := goqtt.NewClient("example.broker", p, t, cid, ka)
Note that you can pass any of these options, you don't need to pass all of them.
Index ¶
Examples ¶
Constants ¶
const ( DefaultPort = "1883" DefaultKeepAlive = 60 // seconds DefaultTopic = "goqtt" )
Default values for the Client configuration. See the DefaultClientId function for the Client's default ClientId value.
Variables ¶
This section is empty.
Functions ¶
func ClientId ¶ added in v0.0.10
func ClientId(cid string) option
ClientId is the identifier you use to tell the MQTT broker who you are. A broker will require unique ClientId's for all connected clients.
func DefaultClientId ¶ added in v0.0.10
func DefaultClientId() string
DefaultClientId creates a default value for the Client's clientId. It uses the process id and the current time to try to prevent client collisions with the broker.
func KeepAlive ¶ added in v0.0.10
func KeepAlive(seconds int) option
KeepAlive is the time in seconds that the broker should wait before disconnecting you if no packets are sent.
Types ¶
type Client ¶ added in v0.0.8
type Client struct {
Config *clientConfig
// contains filtered or unexported fields
}
Client is the main interaction point for sending and receiving Packets. An instantiated Client needs to call the Connect() method before sending/receiving any packets.
func NewClient ¶ added in v0.0.8
NewClient creates a Client struct which can be used to interact with a MQTT broker. It sets default values for most of the configuration, so only a server address is required to instantiate it. Other configuration options are available and can be passed in as needed.
func (*Client) Connect ¶ added in v0.0.8
Connect attempts to create a TCP connection to the broker specified in the client's ClientConfig. It sends a CONNECT packet and reads the CONNACK packet.
func (*Client) Disconnect ¶ added in v0.0.8
func (c *Client) Disconnect()
Disconnect sends a DISCONNECT packet.
func (*Client) Publish ¶ added in v0.0.10
Publish sends a given message to the topic specified by the Client.
Example ¶
This example subscribes to a MQTT broker will print any incoming messages to std out.
package main
import (
"log"
"github.com/andschneider/goqtt"
)
func main() {
// Create Client
client := goqtt.NewClient("mqtt.eclipse.org")
// Attempt a connection to the specified MQTT broker
client.Connect()
defer client.Disconnect()
// Attempt to publish a message
err := client.Publish("hello world")
if err != nil {
log.Printf("could not send message: %v\n", err)
}
}
Output:
func (*Client) ReadLoop ¶ added in v0.0.8
func (c *Client) ReadLoop() (*packets.PublishPacket, error)
ReadLoop should be used after a successful subscription to a topic and reads any incoming messages. It returns a PublishPacket if one has been received for further processing.
Example ¶
This example subscribes to a MQTT broker will print any incoming messages to std out, until the program exits.
package main
import (
"log"
"github.com/andschneider/goqtt"
)
func main() {
// Create Client
client := goqtt.NewClient("mqtt.eclipse.org")
// Attempt a connection to the specified MQTT broker
client.Connect()
defer client.Disconnect()
// Attempt to subscribe to the topic
client.Subscribe()
// Read messages indefinitely
for {
log.Println("waiting for message")
m, _ := client.ReadLoop()
if m != nil {
log.Printf("received message: '%s' from topic: '%s'", string(m.Message), m.Topic)
}
}
}
Output:
func (*Client) SendPing ¶ added in v0.0.8
SendPing creates a Ping packet and writes it to the client's TCP connection right away.
func (*Client) Subscribe ¶ added in v0.0.8
Subscribe attempts to create a subscription for a client to it's configured topic. It sends a SUBSCRIBE packet and reads the SUBACK packet.
Example ¶
This example subscribes to a MQTT broker. You will want to call ReadLoop afterwards to process incoming messages.
package main
import (
"log"
"github.com/andschneider/goqtt"
)
func main() {
// Create Client
client := goqtt.NewClient("mqtt.eclipse.org")
// Attempt a connection to the specified MQTT broker
client.Connect()
defer client.Disconnect()
// Attempt to subscribe to the topic
err := client.Subscribe()
if err != nil {
log.Fatalf("could not subscribe to topic: %v\n", err)
}
}
Output:
func (*Client) Unsubscribe ¶ added in v0.0.8
Unsubscribe sends an UNSUBSCRIBE packet for a given topic and reads the UNSUBACK packet.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
connect
command
This example simply connects to a MQTT broker and then terminates.
|
This example simply connects to a MQTT broker and then terminates. |
|
publish
command
This example publishes a message to a given topic to run: go run ./examples/publish/main.go The default broker is the publicly available server hosted by the Eclipse foundation, but can be changed by specifying a different host name or IP address with the -server flag.
|
This example publishes a message to a given topic to run: go run ./examples/publish/main.go The default broker is the publicly available server hosted by the Eclipse foundation, but can be changed by specifying a different host name or IP address with the -server flag. |
|
subscribe
command
This example subscribes to a MQTT broker will print any incoming messages to the terminal.
|
This example subscribes to a MQTT broker will print any incoming messages to the terminal. |
|
Package packets make up the MQTT control packets and their core functionality.
|
Package packets make up the MQTT control packets and their core functionality. |
