binding mqtt topic and redirect to kafka server.
- Open
main.go
, modifyMQTTConfig
,KafkaConfig
. - Open
main.go
, increateMQTTClient
, you have to overwrite the topic want to be listen in[]string{"#"}
. (it can be many). - run
go run main.go MqttClient.go KafkaClient.go
Modify MqttClient
:
opts.SetUsername(*username)
opts.SetPassword(*password)
main.go: mqttHandler
:
func mqttHandler(client MQTT.Client, msg MQTT.Message) {
topic := msg.Topic()
payload := msg.Payload()
fmt.Printf("TOPIC: %s\n", msg.Topic())
fmt.Printf("MSG: %s\n", msg.Payload())
//custom...
mqttData := &mqttModel{}
json.Unmarshal([]byte(payload), mqttData)
}