2020-12-26 15:32:45 +00:00
|
|
|
# MQTT Broker
|
|
|
|
|
|
|
|
The MQTT broker is useful for IoT based applications
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
Drop in import
|
|
|
|
|
|
|
|
```go
|
2021-01-20 21:01:10 +00:00
|
|
|
import _ "github.com/asim/go-micro/plugins/broker/mqtt"
|
2020-12-26 15:32:45 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
Flag on command line
|
|
|
|
|
|
|
|
```shell
|
|
|
|
go run main.go --broker=mqtt
|
|
|
|
```
|
|
|
|
|
|
|
|
Alternatively use directly
|
|
|
|
|
|
|
|
```go
|
|
|
|
import (
|
2021-01-20 21:01:10 +00:00
|
|
|
"github.com/asim/go-micro/v3"
|
|
|
|
"github.com/asim/go-micro/plugins/broker/mqtt"
|
2020-12-26 15:32:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
service := micro.NewService(
|
|
|
|
micro.Name("my.service"),
|
|
|
|
micro.Broker(mqtt.NewBroker()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Encoding
|
|
|
|
|
|
|
|
Because MQTT does not support message headers the plugin encodes messages using JSON.
|
|
|
|
If you prefer to send and receive the mqtt payload uninterpreted use the `noop` codec.
|
|
|
|
|
|
|
|
Example
|
|
|
|
|
|
|
|
```go
|
|
|
|
import (
|
|
|
|
"github.com/micro/broker"
|
|
|
|
"github.com/micro/broker/codec/noop"
|
2021-01-20 21:01:10 +00:00
|
|
|
"github.com/asim/go-micro/plugins/broker/mqtt"
|
2020-12-26 15:32:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
b := mqtt.NewBroker(
|
|
|
|
broker.Codec(noop.NewCodec()),
|
|
|
|
)
|
|
|
|
```
|