Skip to content

The transport layer of MQTT protocol go implementation

Notifications You must be signed in to change notification settings

maPaydar/mqtt-transport

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MQTT transport layer implementation

This repo doesn't implement a message broker, just network layer of MQTT protocol.

Inspired by emmiter-io project.

MQTT protocol reference.

Run Server

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)
	}
}

JS mqtt client

// 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

About

The transport layer of MQTT protocol go implementation

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages