This repo doesn't implement a message broker, just network layer of MQTT protocol.
Inspired by emmiter-io project.
MQTT protocol reference.
package main
import (
"log"
mqtt "github.com/maPaydar/mqtt-transport"
)
type MQTTHandlerImpl struct {
}
func (m MQTTHandlerImpl) OnConnect(packet *mqtt.Connect) error {
log.Println("OnConnect: ")
return nil
}
func (m MQTTHandlerImpl) OnSubscribe(topic []byte) error {
log.Println("OnSubscribe:", string(topic))
return nil
}
func (m MQTTHandlerImpl) OnUnsubscribe(topic []byte) error {
log.Println("OnUnsubscribe:", string(topic))
return nil
}
func (m MQTTHandlerImpl) OnPublish(packet *mqtt.Publish) error {
log.Println("OnPublish: ", string(packet.Topic))
return nil
}
func main() {
svc, err := mqtt.NewService("localhost:9090", &MQTTHandlerImpl{})
if err != nil {
log.Fatal(err)
}
log.Println("Service created")
err = svc.Listen()
if err != nil {
log.Fatal(err)
}
}
// client.js
var mqtt = require('mqtt');
var client = mqtt.connect('mqtt://localhost:9090');
client.on('connect', function () {
client.subscribe('presence', function (err) {
if (!err) {
client.publish('presence', 'Hello mqtt')
}
})
});
client.on('message', function (topic, message) {
console.log(message.toString());
client.end();
});
node client.js