跳到正文
Hacker News (official RSS)(github.com)·· 7 天前品質評分44

Eclipse Mosquitto:支援多種 MQTT 版本的開源訊息伺服器

Eclipse Mosquitto – An open source MQTT broker

重點導讀

不同程式要用 MQTT 傳送訊息時,需要一個負責收發的伺服器。

  • Eclipse Mosquitto 是開源的 MQTT 伺服器,也附有 C 和 C++ 程式庫,以及發布、訂閱訊息和管理伺服器的工具。舉例來說,可以用 mosquitto_sub 訂閱 test/topic,再用 mosquitto_pub 傳送 hello world。這種簡單啟動方式只允許同一臺電腦連線,而且不需要登入。
  • 若要讓其他電腦連線,就得設定監聽位置和登入驗證,尤其伺服器公開時,不建議開放匿名連線。

AI 伴隨問答

自備 Key 深度分析

針對這篇報導有疑問?可直接點選快捷問題或向 AI 追問背景脈絡與技術細節:

正文

Mosquitto is an open source implementation of a server for version 5.0, 3.1.1, and 3.1 of the MQTT protocol. It also includes a C and C++ client library, the mosquitto_pub mosquitto_rr, and mosquitto_sub utilities for publishing and subscribing, and the mosquitto_ctrl, mosquitto_signal, and mosquitto_passwd applications for helping administer the broker.

Links

See the following links for more information on MQTT:

Mosquitto project information is available at the following locations:

There is also a public test server available at https://test.mosquitto.org/

Installing

See https://mosquitto.org/download/ for details on installing binaries for various platforms.

Quick start

If you have installed a binary package the broker should have been started automatically. If not, it can be started with a very basic configuration:

mosquitto

Then use mosquitto_sub to subscribe to a topic:

mosquitto_sub -t 'test/topic' -v

And to publish a message:

mosquitto_pub -t 'test/topic' -m 'hello world'

Note that starting the broker like this allows anonymous/unauthenticated access but only from the local computer, so it's only really useful for initial testing.

If you want to have clients from another computer connect, you will need to provide a configuration file. If you have installed from a binary package, you will probably already have a configuration file at somewhere like /etc/mosquitto/mosquitto.conf. If you've compiled from source, you can write your config file then run as mosquitto -c /path/to/mosquitto.conf.

To start your config file you define a listener and you will need to think about what authentication you require. It is not advised to run your broker with anonymous access when it is publicly available.

For details on how to do this, look at the authentication methods available and the dynamic security plugin.

Documentation

Documentation for the broker, clients and client library API can be found in the man pages, which are available online at https://mosquitto.org/man/. There are also pages with an introduction to the features of MQTT, the mosquitto_passwd utility for dealing with username/passwords, and a description of the configuration file options available for the broker.

Detailed client library API documentation can be found at https://mosquitto.org/api/

Building from source

To build from source the recommended route for end users is to download the archive from https://mosquitto.org/download/.

On Windows and Mac, use cmake to build. On other platforms, just run make to build. For Windows, see also README-windows.md.

If you are building from the git repository then the documentation will not already be built. Use make binary to skip building the man pages, or install docbook-xsl on Debian/Ubuntu systems.

Build Dependencies

  • cJSON - required
  • c-ares (libc-ares-dev on Debian based systems) - optional, enable with WITH_SRV=yes
  • libedit - for mosquitto_ctrl interactive shell - optional, disable with WITH_EDITLINE=no
  • libmicrohttpd - for broker http api support - optional, disable with WITH_HTTP_API=no
  • openssl (libssl-dev on Debian based systems) - optional, disable with WITH_TLS=no
  • pthreads - for client library thread support. This is required to support the mosquitto_loop_start() and mosquitto_loop_stop() functions. If compiled without pthread support, the library isn't guaranteed to be thread safe.
  • sqlite3 - for persistence support in the broker - optional, disable with WITH_SQLITE=no
  • uthash / utlist - bundled versions of these headers are provided, disable their use with WITH_BUNDLED_DEPS=no
  • xsltproc (xsltproc and docbook-xsl on Debian based systems) - only needed when building from git sources - disable with WITH_DOCS=no

Equivalent options for enabling/disabling features are available when using the CMake build. It is also possible to enable/disable building of specific plugins in the CMake build.

Building mosquitto - Using vcpkg

You can download and install mosquitto using the vcpkg dependency manager:

git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install mosquitto

The mosquitto port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please create an issue or pull request on the vcpkg repository.

Credits

Mosquitto was written by Roger Light roger@atchoo.org. There have been substantial contributions by other people in the community both in terms of code and other help.

讀者討論

理性交流 · 即時討論