Giter Club home page Giter Club logo

nanomq's Introduction

NanoMQ

GitHub Release Build Status Docker Pulls Discord Twitter YouTube Community License

The best IoT MQTT open source team looks forward to your joiningNanoMQ MQTT Broker (NanoMQ) is a lightweight and blazing-fast MQTT Broker for the IoT Edge platform.

NanoMQ bases on NNG's asynchronous I/O threading model, with an extension of MQTT support in the protocol layer and reworked transport layer, plus an enhanced asynchronous IO mechanism maximizing the overall capacity.

NanoMQ currently supports MQTT V3.1.1 and partially supports MQTT V5.0.

For more information, please visit NanoMQ homepage.

Features

  • Cost-effective on an embedded platform;
  • Fully base on native POSIX. High Compatibility;
  • Pure C/C++ implementation. High portability;
  • Fully asynchronous I/O and multi-threading;
  • Good support for SMP;
  • Low latency & High handling capacity.nanomq-blueprint

Quick Start

NanoMQ broker usage

nanomq broker start 
nanomq broker stop
nanomq broker restart 

MQTT Example:

nanomq broker start 

NanoMQ MQTT client usage

# Publish
nanomq pub  start --url <url> -t <topic> -m <message> [--help]

# Subscribe 
nanomq sub  start --url <url> -t <topic> [--help]

# Connect
nanomq conn start --url <url> [--help]

NanoMQ MQTT bench usage

nanomq bench start { pub | sub | conn } [--help]

POSIX message queue usage

nanomq mq start
nanomq mq stop

Incoming feature: NanoMQ Zmq usage

nanomq sp req -port 5555
nanomq sp rep -port 5555

Note: NanoMQ provides several ways of configurations so that user can achieve better performance on different platforms, check here for details.

Compile & Install

NanoMQ dedicates to delivering a simple but powerful Messaging Hub on various edge platforms.

With this being said, NanoMQ can run on different architectures such like x86_64 and ARM with minor migration efforts.

Docker

docker run -d -p 1883:1883 -p 8883:8883 --name nanomq emqx/nanomq:0.6.0

Building From Source

To build NanoMQ, requires a C99 & C++11 compatible compiler and CMake (version 3.13 or newer).

  • It is recommended to compile with Ninja:

    git clone https://github.com/emqx/nanomq.git ; cd nanomq
    git submodule update --init --recursive
    mkdir build && cd build
    cmake -G Ninja ..
    ninja
  • Or to compile without Ninja:

    git clone https://github.com/emqx/nanomq.git ; cd nanomq
    git submodule update --init --recursive
    mkdir build && cd build
    cmake .. 
    make

Note for Mac users: mq (one of the features) is not supported on Mac, please disable it before compiling.

cmake -G Ninja -DCFG_METHOD=CMAKE_CONFIG -DMQ=0  ..
ninja

Note (optional): TLS is disabled by default. If you want to build with TLS support you will also need mbedTLS. After installing mbedTLS, you can enable it by -DNNG_ENABLE_TLS=ON.

cmake -G Ninja -DNNG_ENABLE_TLS=ON ..
ninja

Note (optional): client ( pub / sub / conn ) is built by default, you can disable it via -DBUILD_CLIENT=OFF.

cmake -G Ninja -DBUILD_CLIENT=OFF ..
ninja

Note (optional): bench tool isn't built by default, you can enable it via -DBUILD_BENCH=ON.

cmake -G Ninja -DBUILD_BENCH=ON ..
ninja

Note (optional): nanolib & nanonng are dependencies of NanoMQ that can be compiled independently.

To compile nanonng (nanonng is the fork of nng repository with MQTT support):

cd nng/build
cmake -G Ninja ..
ninja

To compile nanolib:

cd nanolib/build
cmake -G Ninja ..
ninja

Configuration

NanoMQ as an MQTT broker with good compatibility and portability, it provides several options for optimizing performance according to your system.

Compiling parameters

With CMake, NanoMQ allows user to have broker natively tuned/configured when building from source. Please kindly find the parameters as follows:

CMake Configuration

To use CMake configuration, navigating to ./nanomq/build and typing the following command :

cmake -DCFG_METHOD=CMAKE_CONFIG ..

Be aware that, CMake configuration is enabled by default, If you leave all parameters empty, the default value will take effect.

  • Limiting the number of threads by specifying the number of and the max number of taskq threads:

    Recommendation: equal to your CPU cores

    cmake -G Ninja -DCFG_METHOD=CMAKE_CONFIG -DNNG_NUM_TASKQ_THREADS=<num> ..
    cmake -G Ninja -DCFG_METHOD=CMAKE_CONFIG -DNNG_MAX_TASKQ_THREADS=<num> ..
  • Setting the number of concurrent resolver threads:

    Recommendation: 1

    cmake -G Ninja -DCFG_METHOD=CMAKE_CONFIG -DNNG_RESOLV_CONCURRENCY=<num> ..

    Inherited from NNG

  • For debugging, NanoMQ has a debugging system that logs all information from all threads. Which is aligned with syslog standard. Enabling or disabling the debugging messages by (Mac users should disable it before compilation):

    Default: disabled (1)

    cmake -G Ninja -DCFG_METHOD=CMAKE_CONFIG -DNOLOG=0  ..
    cmake -G Ninja -DCFG_METHOD=CMAKE_CONFIG -DNOLOG=1  ..
  • Enabling or disabling messaging queue function

    Default: enabled (1)

    cmake -G Ninja -DCFG_METHOD=CMAKE_CONFIG -DMQ=1  ..
    cmake -G Ninja -DCFG_METHOD=CMAKE_CONFIG -DMQ=0  ..
  • Setting the logical concurrency limitation:

    Default: 32

    cmake -G Ninja -DCFG_METHOD=CMAKE_CONFIG -DPARALLEL=<num> ..

config.cmake.in

In the root directory of NanoMQ(./nanomq), there is a file named 'config.cmake.in'. It provides bunch of modifiable configurations (same as the parameters above).

navigating to ./nanomq/build , then:

cmake -DCFG_METHOD=FILE_CONFIG ..
ninja

to make your modification effective.

Booting Parameters

Users can also change the configuration parameters of NanoMQ while booting. However, part of the parameters is excluded in this method.

NanoMQ configuration file

NanoMQ will look up to it's configuration file in /etc/ by default. Please remember to copy conf file to /etc/ in your system if you wanna start NanoMQ without setting conf path manually. This file is different from 'config.cmake.in'. This 'nanomq.conf' allows you to configure broker when booting. Please be noted that if you start NanoMQ in the project's root directory, this file will be read automatically.

Configure MQTT bridging in NanoMQ: by modifying nanomq_bridge.conf, which is in the same directory with nanomq.conf.

You can also write your own configuration file. Be sure to start NanoMQ in this fashion to specify an effective configuration file:

nanomq broker start --conf <$FILE_PATH> [--bridge <$FILE_PATH>] [--auth <$FILE_PATH>]

NanoMQ Environment Variables

Variable Type Value
NANOMQ_BROKER_URL String 'nmq-tcp://host:port', 'nmq-tls://host:port'
NANOMQ_DAEMON Boolean Set nanomq as daemon (default: false).
NANOMQ_NUM_TASKQ_THREAD Integer Number of taskq threads used, num greater than 0 and less than 256.
NANOMQ_MAX_TASKQ_THREAD Integer Maximum number of taskq threads used, num greater than 0 and less than 256.
NANOMQ_PARALLEL Long Number of parallel.
NANOMQ_PROPERTY_SIZE Integer Max size for a MQTT user property.
NANOMQ_MSQ_LEN Integer Queue length for resending messages.
NANOMQ_QOS_DURATION Integer The interval of the qos timer.
NANOMQ_ALLOW_ANONYMOUS Boolean Allow anonymous login (default: true).
NANOMQ_WEBSOCKET_ENABLE Boolean Enable websocket listener (default: true).
NANOMQ_WEBSOCKET_URL String 'nmq-ws://host:port/path', 'nmq-wss://host:port/path'
NANOMQ_HTTP_SERVER_ENABLE Boolean Enable http server (default: false).
NANOMQ_HTTP_SERVER_PORT Integer Port for http server (default: 8081).
NANOMQ_HTTP_SERVER_USERNAME String Http server user name for auth.
NANOMQ_HTTP_SERVER_PASSWORD String Http server password for auth.
NANOMQ_TLS_ENABLE Boolean Enable TLS connection.
NANOMQ_TLS_URL String 'nmq-tls://host:port'.
NANOMQ_TLS_CA_CERT_PATH String Path to the file containing PEM-encoded CA certificates.
NANOMQ_TLS_CERT_PATH String Path to a file containing the user certificate.
NANOMQ_TLS_KEY_PATH String Path to the file containing the user's private PEM-encoded key.
NANOMQ_TLS_KEY_PASSWORD String String containing the user's password. Only used if the private keyfile is password-protected.
NANOMQ_TLS_VERIFY_PEER Boolean Verify peer certificate (default: false).
NANOMQ_TLS_FAIL_IF_NO_PEER_CERT Boolean Server will fail if the client does not have a certificate to send (default: false).
NANOMQ_CONF_PATH String NanoMQ main config file path (defalt: /etc/nanomq.conf).
NANOMQ_BRIDGE_CONF_PATH String Bridge config file path (defalt: /etc/nanomq_bridge.conf).
NANOMQ_AUTH_CONF_PATH String Auth config file path (defalt: /etc/nanomq_auth_username.conf).
  • Specify a broker url. On host system:

    export NANOMQ_BROKER_URL="nmq-tcp://0.0.0.0:1883"
    export NANOMQ_TLS_ENABLE=true
    export NANOMQ_TLS_URL="tls+nmq-tcp://0.0.0.0:8883"

    Creating docker container:

    docker run -d -p 1883:1883 -p 8883:8883 --name nanomq emqx/nanomq:0.6.0  \
               -e NANOMQ_BROKER_URL="nmq-tcp://0.0.0.0:1883" \
               -e NANOMQ_TLS_ENABLE=true \
               -e NANOMQ_TLS_URL="tls+nmq-tcp://0.0.0.0:8883"
  • Specify a nanomq config file path. On host system:

    export NANOMQ_CONF_PATH="/usr/local/etc/nanomq.conf"

    Creating docker container:

    docker run -d -p 1883:1883 --name nanomq emqx/nanomq:0.6.0 -e NANOMQ_CONF_PATH="/usr/local/etc/nanomq.conf"

NanoMQ Command-Line Arguments

The same configuration can be achieved by adding some command-line arguments when you start NanoMQ broker. There are a few arguments for you to play with. And the general usage is:

Usage: nanomq broker { { start | restart [--url <url>] [--conf <path>] [--bridge <path>] 
                     [--auth <path>] [-d, --daemon] [-t, --tq_thread <num>] 
                     [-T, -max_tq_thread <num>] [-n, --parallel <num>]
                     [-D, --qos_duration <num>] [--http] [-p, --port] } 
                     [--cacert <path>] [-E, --cert <path>] [--key <path>] 
                     [--keypass <password>] [--verify] [--fail]
                     | stop }

Options: 
  --url <url>                Specify listener's url: 'nmq-tcp://host:port', 'nmq-tls://host:port' 
                             or 'nmq-ws://host:port/path' or 'nmq-wss://host:port/path'
  --conf <path>              The path of a specified nanomq configuration file 
  --bridge <path>            The path of a specified bridge configuration file 
  --auth <path>              The path of a specified authorize configuration file 
  --http                     Enable http server (default: false)
  -p, --port <num>           The port of http server (default: 8081)
  -t, --tq_thread <num>      The number of taskq threads used, `num` greater than 0 and less than 256
  -T, --max_tq_thread <num>  The maximum number of taskq threads used, `num` greater than 0 and less than 256
  -n, --parallel <num>       The maximum number of outstanding requests we can handle
  -s, --property_size <num>  The max size for a MQTT user property
  -S, --msq_len <num>        The queue length for resending messages
  -D, --qos_duration <num>   The interval of the qos timer
  -d, --daemon               Run nanomq as daemon (default: false)
  --cacert                   Path to the file containing PEM-encoded CA certificates
  -E, --cert                 Path to a file containing the user certificate
  --key                      Path to the file containing the user's private PEM-encoded key
  --keypass                  String containing the user's password. Only used if the private keyfile is password-protected
  --verify                   Set verify peer certificate (default: false)
  --fail                     Server will fail if the client does not have a certificate to send (default: false)
  • start, restart, and stop command is mandatory as it indicates whether you want to start a new broker, or replace an existing broker with a new one, or stop a running broker;

    If stop is chosen, no other arguments are needed, and an existing running broker will be stopped:

    nanomq broker stop

    All arguments are useful when start and restart are chosen. An URL is mandatory (unless an URL is specified in the 'nanomq.conf', or in your configuration file), as it indicates on which the host and port a broker is listening:

    nanomq broker start|restart 					
    nanomq broker start|restart --conf <$FILE_PATH> [--bridge <$FILE_PATH>] [--auth <$FILE_PATH>] 
  • Telling broker that it should read your configuration file.

    Be aware that command line arguments always has a higher priority than both 'nanomq.conf' and your configuration file:

    nanomq broker start|restart --conf <$FILE_PATH> [--bridge <$FILE_PATH>] [--auth <$FILE_PATH>]
  • Running broker in daemon mode:

    nanomq broker start|restart --daemon
  • Running broker with tls:

    nanomq broker start --url "tls+nmq-tcp://0.0.0.0:8883" [--cacert <path>] [-E, --cert <path>] [--key <path>] [--keypass <password>] [--verify] [--fail]
  • Limiting the number of threads by specifying the number of and the max number of taskq threads:

    nanomq broker start|restart  --tq_thread <num>
    nanomq broker start|restart  --max_tq_thread <num>
  • Limiting the maximum number of logical threads:

    nanomq broker start|restart --parallel <num>
  • Setting the max property size for MQTT packet:

    Default: 32 bytes

    nanomq broker start|restart --property_size <num>
  • Setting the queue length for a resending message:

    Default: 64 bytes

    nanomq broker start|restart --msq_len <num>
  • Setting the interval of the qos timer (Also a global timer interval for session keeping):

    Default: 30 seconds

    nanomq broker start|restart --qos_duration <num>

Priority: Command-Line Arguments > Environment Variables > Config files

For tuning NanoMQ according to different hardware, please check the Doc.

Community

Our Website

Visit our official website to have a good grasp on NanoMQ MQTT broker and see how it can be applied in current industries.

Test Report

This test report shows how extraordinary and competitive the NanoMQ is in Edge Computing.

Currently the benchmark is for 0.2.5, the updated one with ver 0.3.5 is coming soon

Open Source

NanoMQ is fully open-sourced!

Questions

The Github Discussions provides a place for you to ask questions and share your ideas with users around the world.

Slack

You could join us on Slack. We now share a workspace with the entire EMQ X team. After joining, find your channel!

  • #nanomq: is a channel for general usage, where for asking question or sharing using experience;
  • #nanomq-dev: is a channel for MQTT lover and developer, your great thoughts are what we love to hear;
  • #nanomq-nng: is a channel for guys who are interested in NNG, one of our fabulous dependencies.

MQTT Specifications

MQTT Version 3.1.1

MQTT Version 5.0

MQTT SN

MQTT Client Examples

MQTT-Client-Examples

EMQ X Broker

EMQ X Broker

HStreamDB

HStreamDB

License

MIT License

Authors

The EMQ X Edge Computing team.

nanomq's People

Contributors

alvin1221 avatar alvinwong2018 avatar emqplus avatar hongwei-w avatar hujung-de avatar jaylinyu avatar lee-emqx avatar longlene avatar red-asuka avatar rory-z avatar swilder-m avatar wanghaemq 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.