Giter Site home page Giter Site logo

gqtt's Introduction

gqtt

MQTT5 broker/client implementation by golang.

See OASIS's spec: http://docs.oasis-open.org/mqtt/mqtt/v5.0/cs02/mqtt-v5.0-cs02.html

Requirement

  • Go1.12 or later

Installation

This packages uses go mod, so you need to enable it as export GO111MODULE=on in your environment.

Usage

To debug message passing, set export DEBUG=1 before start processes.

Broker

Simple broker with accept hook message example:

package main

import (
	"context"
	"github.com/ysugimoto/gqtt"
	"github.com/ysugimoto/gqtt/message"
)

func main() {
	server := gqtt.NewBroker(":9999")
	ctx := context.Background()
	// Start server inside goroutine
	go server.ListenAndServe(ctx)

	// Hooks of messages
	for evt := range server.MessageEvent {
		switch e := evt.(type) {
		// Client subscribed
		case *message.Subscribe:
			log.Println("Received SUBSCRIBE event: ", e.GetType())
		// Client connected
		case *message.Connect:
			log.Println("Received CONNECT event", e.GetType())
		// Client published message
		case *message.Publish:
			log.Println("Received PUBLISH event", e.GetType())
		}
	}
	<-ctx.Done()
}

Client

Simple connect (with authentication) -> subscribe -> publish example. The auth challenge during connect phase is new of MQTT5 spec.

package main

import (
	"log"
	"os"
	"time"
	"context"

	"github.com/ysugimoto/gqtt"
	"github.com/ysugimoto/gqtt/message"
)

func main() {
	client := gqtt.NewClient("mqtt://localhost:9999")
	defer client.Disconnect()

	ctx := context.Background()

	// Connect with authenticate
	auth := gqtt.WithLoginAuth("admin", "admin")
	if err := client.Connect(ctx, auth); err != nil {
		log.Fatal(err)
	}

	// Subscribe topic
	if err := client.Subscribe("gqtt/example", message.QoS2); err != nil {
		log.Fatal(err)
	}

	ticker := time.NewTicker(3 * time.Second)

	for {
		select {
		case <-client.Closed:
			log.Println("connection closed")
			return
		case <-ctx.Done():
			log.Println("context canceled")
			return
		case msg := <-client.Message:
			log.Printf("published message received: %s\n", string(msg.Body))
		case <-ticker.C:
			log.Printf("message publish")
			if err := client.Publish("gqtt/example", []byte("Hello, MQTT5!"), gqtt.WithQoS(message.QoS2)); err != nil {
				return
			}
		}
	}
}

See more examples in detail.

Features

This package now implements partial features. See following checks:

  • QoS0 message
  • QoS1 message (but not persistent storage, only store on memory)
  • QoS2 message (but not persistent storage, only store on memory)
  • Retain message (but not persistent storage, only store on memory)
  • Will message
  • Wildcard topics
  • User Property
  • MQTT over WebSocket
  • Connect redirection
  • Request/Response feature
  • Auth challenge (now experimental. Only basic/login auth with admin/admin)
  • Distirbuted brokers

LICENSE

MIT

Author

Yoshiaki Sugimoto

This package is still under development. PR is welcome :-)

gqtt's People

Contributors

ysugimoto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

togep moayyaed qhxnj

gqtt's Issues

Build errors during install

Hi,

The following errors are produced when installing the package.

>go get github.com/ysugimoto/gqtt

# github.com/ysugimoto/gqtt/broker
..\..\..\github.com\ysugimoto\gqtt\broker\client.go:37:24: multiple-value uuid.NewV4() in single-value context
# github.com/ysugimoto/gqtt/client
..\..\..\github.com\ysugimoto\gqtt\client\connect.go:19:31: multiple-value uuid.NewV4() in single-value context

Thanks,
-G

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.