MQTT is already the de facto standard communication protocol for IoT. Among the various open source MQTT servers currently available on the market, the most common one is based on the Linux platform, and there is less support for the Windows platform. Even if it is supported, there are problems such as weak performance, single function, and version compatibility.

However, in the field of industrial automation and building intelligence, there are many scenarios that still rely on the Windows ecosystem. In particular, the industrial control host computer still uses a large number of C# to develop and run in the industrial computer installed with Windows, and some cost-sensitive scenarios even use the WinCE operating system. In order to enable these users to use the easy-to-use, lightweight, compact, and fully functional MQTT messaging service on Windows, NanoMQ, an ultra-lightweight IoT edge MQTT messaging server, relies on its powerful cross-platform and compatibility capabilities. MinGW's POSIX compilation environment has been successfully ported to various Windows environments.

This article will take NanoMQ as an example to demonstrate how to quickly build an IoT MQTT message server that can support multi-protocol connections in Windows system through two installation methods: installation package and source code compilation.

Introduction to NanoMQ

NanoMQ is an ultra-lightweight high-performance MQTT message server + message bus launched by EMQ for IoT edge computing scenarios. It has the advantages of ultra-lightweight, high throughput, low latency, high compatibility and portability. and deployed on CPU architectures.

Currently NanoMQ has the following functions:

  • Full support for MQTT 3.1.1/5.0 .
  • Embedded rules engine supports real-time processing and persistence of messages.
  • Support cloud bridging, and can perform offline caching and automatic retransmission of messages.
  • Provides rich HTTP REST APIs to facilitate cloud-side collaboration and remote operation and maintenance monitoring.
  • Multi-protocol support: MQTT over WebSocket, ZeroMQ and nanomsg, etc.
  • Support third-party HTTP authentication and WebHook integration.

installation package installation

The first method we will use the binary installation package to install directly.

Download the installation package

First go to the NanoMQ official website download page: https://nanomq.io/zh/downloads , and select the Windows platform to download the installation package:

Download the Windows distribution package from the NanoMQ official website

The latest NanoMQ Windows Release version is 0.10.5.

installation package installation

Download will get an installation package in msi format, double-click to install.

NanoMQ 安装包

Downloaded installation package

Select the installation path and the functions to be installed. The installation package also contains the library files of NanoSDK:

The program exits automatically after the installation is complete.

Test and start the NanoMQ service

At present, NanoMQ does not include a visual interface, so it needs to be started through the command line or service. The startup method is as follows:

Open a command line window or PowerShell and enter nanomq to see:

Check if the command can be used normally

It means that NanoMQ has been successfully installed in the system. You can enter --help to learn what command options are available.

If you start, you can use the path specified by the configuration file nanomq start --conf C:\nanomq\config\nanomq.conf Please find the corresponding configuration file according to the path selected in the second step of installation. The simple configuration options are:

 ## url
## Connect with the host and port
## 监听的端口和IP地址
## Value: nmq-tcp://host:port
url=nmq-tcp://localhost:1883

## num_taskq_thread
## Use a specified number of taskq threads 
## 线程数,建议设置成和CPU核数相同
## Value: 1-255
num_taskq_thread=4

## max_taskq_thread
## Use a specified maximunm number of taskq threads
## 线程数,建议设置成和CPU核数相同
## Value: 1-255
max_taskq_thread=4

## msq_len
## The queue length in-flight window
## This is essential for performance and memory consumption
## 飞行窗口长度大小,影响broker最大内存占用
## Value: 1-infinity
msq_len=256

On Windows platform, the monitored URL needs to be set to nmq-tcp://localhost:1883 . In addition to the nanomq.conf configuration file, the specific configuration methods of other functions can refer to the NanoMQ documentation page .

To test whether the NanoMQ service is running normally, you can use the client tool that comes with NanoMQ to test directly:

Through the Pub/Sub command of NanoMQ, you can see that messages can be sent and received normally, and the Sub client can receive the Hello message sent by the Pub client.

Compile and run the source code

The second method is to compile and install from source code, which allows us to re-develop NanoMQ on Windows and add the functions we need.

Currently, the compilation of NanoMQ under Windows requires MinGW-w64 , Make and CMake .

MinGW-w64 download address: https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/mingw-builds/installer/mingw-w64-install.exe/download
Make download address: https://sourceforge.net/projects/gnuwin32/files/make/3.81/make-3.81.exe/download?use_mirror=jaist&download=
CMake download address: https://cmake.org/download/

source code preparation

First, you need to download the source code from the NanoMQ source code repository. Note that NanoMQ contains a submodule NNG.

The source code of NNG needs to be placed in the nng directory of nanomq, and the compilation can be started after the source code is ready.

Compile and install NanoMQ

command line compilation

The following commands are run in Windows PowerShell or Git bash.
 # 1. clone 源码 (已通过ZIP下载源代码则不需要这一步)
PS: D:\Project> git clone https://github.com/emqx/nanomq.git
PS: D:\Project> cd nanomq

# 2. 更新和初始化 git 子模块
PS: D:\Project\nanomq> git submodule update --init --recursive

# 3. 创建并进入 build 目录
PS: D:\Project\nanomq> mkdir build
PS: D:\Project\nanomq> cd build

# 4. 编译 NanoMQ
PS: D:\Project\nanomq\build> cmake -G "MinGW Makefiles" ..
PS: D:\Project\nanomq\build> make -j 8

# 5. 运行 NanoMQ
PS: D:\Project\nanomq\build> .\nanomq\nanomq.exe broker start

Compile in Visual Studio Code

C/C++ and CMake related plugins of VS Code need to be installed in advance

  1. Click to select the compilation toolchain;
  2. Select GCC 8.1.0 i686-w64-mingw32 (may vary depending on the version of MinGW you have installed);
  3. Click build to start compiling NanoMQ.

run the test

In addition to the client tools that come with NanoMQ, you can also use the open source MQTT test client tool MQTT X to perform basic messaging testing.

MQTT X download address: https://www.emqx.com/zh/try?product=MQTTX

From MQTTX, it can be seen that messages can be sent and received normally through NanoMQ.

Epilogue

So far, we have successfully built a complete MQTT Broker on the Windows platform, providing a lightweight and powerful MQTT message server for the majority of Windows ecosystem users and other scenarios that cannot use the Linux environment. In the future, we will further introduce how to use NanoMQ's rules engine and WebHook and other more advanced features on Windows.

Copyright statement: This article is original by EMQ, please indicate the source when reprinting.

Original link: https://www.emqx.com/zh/blog/install-mqtt-broker-on-windows


EMQX
336 声望437 粉丝

EMQ(杭州映云科技有限公司)是一家开源物联网数据基础设施软件供应商,交付全球领先的开源 MQTT 消息服务器和流处理数据库,提供基于云原生+边缘计算技术的一站式解决方案,实现企业云边端实时数据连接、移动、...