Giter Site home page Giter Site logo

yunziyuan / gmqtt Goto Github PK

View Code? Open in Web Editor NEW

This project forked from drmagice/gmqtt

0.0 1.0 0.0 1.34 MB

Gmqtt is a flexible, high-performance MQTT broker library that fully implements the MQTT protocol V3.1.1 and V5 in golang

License: MIT License

Go 98.66% Dockerfile 0.08% Shell 0.68% Makefile 0.58%

gmqtt's Introduction

中文文档

Gmqtt Mentioned in Awesome Go Build Status codecov Go Report Card

News: Cluster mode is now supported, see federation plugin for examples and details.

Installation

$ go get -u github.com/DrmagicE/gmqtt

Features

  • Provide hook method to customized the broker behaviours(Authentication, ACL, etc..). See server/hooks.go for details
  • Support tls/ssl and websocket
  • Provide flexible plugable mechanism. See server/plugin.go and /plugin for details.
  • Provide Go interface for extensions to interact with the server. For examples, the extensions or plugins can publish message or add/remove subscription through function call. See Server interface in server/server.go and admin for details.
  • Provide metrics (by using Prometheus). (plugin: prometheus)
  • Provide GRPC and REST APIs to interact with server. (plugin:admin)
  • Provide session persistence which means the broker can retrieve the session data after restart. Currently, only redis backend is supported.
  • Provide clustering, see federation plugin for examples and details.

Get Started

The following command will start gmqtt broker with default configuration. The broker listens on 1883 for tcp server and 8883 for websocket server with admin and prometheus plugin loaded.

$ cd cmd/gmqttd
$ go run . start -c default_config.yml

configuration

Gmqtt use -c flag to define configuration path. If not set, gmqtt reads $HOME/gmqtt.yml as default. If default path not exist, Gmqtt will start with default configuration.

session persistence

Gmqtt uses memory to store session data by default and it is the recommended way because of the good performance. But the session data will be lose after the broker restart. You can use redis as backend storage to prevent data loss from restart:

persistence:
  type: redis  
  redis:
    # redis server address
    addr: "127.0.0.1:6379"
    # the maximum number of idle connections in the redis connection pool
    max_idle: 1000
    # the maximum number of connections allocated by the redis connection pool at a given time.
    # If zero, there is no limit on the number of connections in the pool.
    max_active: 0
    # the connection idle timeout, connection will be closed after remaining idle for this duration. If the value is zero, then idle connections are not closed
    idle_timeout: 240s
    password: ""
    # the number of the redis database
    database: 0

Authentication

Gmqtt provides a simple username/password authentication mechanism. (Provided by auth plugin). It is not enabled in default configuration, you can change the configuration to enable it:

# plugin loading orders
plugin_order:
  - auth
  - prometheus
  - admin

When auth plugin enabled, every clients need an account to get connected.You can add accounts through the HTTP API:

# Create: username = user1, password = user1pass
$ curl -X POST -d '{"password":"user1pass"}' 127.0.0.1:8083/v1/accounts/user1
{}
# Query
$ curl 127.0.0.1:8083/v1/accounts/user1
{"account":{"username":"user1","password":"20a0db53bc1881a7f739cd956b740039"}}

API Doc swagger

Docker

$ docker build -t gmqtt .
$ docker run -p 1883:1883 -p 8883:8883 -p 8082:8082 -p 8083:8083  -p 8084:8084  gmqtt

Documentation

godoc

Hooks

Gmqtt implements the following hooks:

Name hooking point possible usages
OnAccept When accepts a TCP connection.(Not supported in websocket) Connection rate limit, IP allow/block list.
OnStop When the broker exists
OnSubscribe When received a subscribe packet Subscribe access control, modifies subscriptions.
OnSubscribed When subscribe succeed
OnUnsubscribe When received a unsubscribe packet Unsubscribe access controls, modifies the topics that is going to unsubscribe.
OnUnsubscribed When unsubscribe succeed
OnMsgArrived When received a publish packet Publish access control, modifies message before delivery.
OnBasicAuth When received a connect packet without AuthMethod property Authentication
OnEnhancedAuth When received a connect packet with AuthMethod property (Only for v5 clients) Authentication
OnReAuth When received a auth packet (Only for v5 clients) Authentication
OnConnected When the client connected succeed
OnSessionCreated When creates a new session
OnSessionResumed When resumes from old session
OnSessionTerminated When session terminated
OnDelivered When a message is delivered to the client
OnClosed When the client is closed
OnMsgDropped When a message is dropped for some reasons
OnWillPublish When the client is going to deliver a will message Modify or drop the will message
OnWillPublished When a will message has been delivered

See /examples/hook for details.

How to write plugins

How to write plugins

Contributing

Contributions are always welcome, see Contribution Guide for a complete contributing guide.

Test

Unit Test

$ go test -race ./...

Integration Test

paho.mqtt.testing.

gmqtt's People

Contributors

drmagice avatar matthias-relabs avatar muesli avatar saeidakbari avatar guilt avatar bergotorino avatar blessmylovexy avatar

Watchers

James Cloos avatar

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.