Skip to content

koron/go-mqtt

Repository files navigation

MQTT for golang

PkgGoDev Actions/Go Go Report Card

Yet another MQTT packages for golang.

This provides three MQTT related packages:

  • packet - MQTT packets encoder/decoder
  • client - MQTT client library
  • server - MQTT broker/server adapter

Client

How to connect with WebSocket

To connect MQTT server with WebSocket, use ws:// scheme for Addr field.

clinet.Connect(client.Param{
    ID:   "wsclient-1234",
    Addr: "ws://localhost:8082/mqtt/over/websocket",
})

This will estimate Origin header to connect to WS server. If you want to specify Origin set Param.Options.WSOrigin option field.

clinet.Connect(client.Param{
    ID:   "wsclient-1234",
    Addr: "ws://localhost:8082/mqtt/over/websocket",
    Options: &client.Options{
        WSOrigin: "http://localhost:80/your/favorite/origin",
        // other fields are copied from client.DefaultOptions
        Version:      4,
        CleanSession: true,
        KeepAlive:    30,
    },
})

When you want to use secure WebSocket, try wss:// scheme and Options.TLSConfig field.

clinet.Connect(client.Param{
    ID:   "wssclient-1234",
    Addr: "wss://localhost:8082/mqtt/over/websocket",
    Options: &client.Options{
        TLSConfig: &tls.Config{
            // your favorite TLS configurations.
        },
    },
}

References