From 4f8bdab55e5695c0813e89b53ed469297301fc07 Mon Sep 17 00:00:00 2001 From: emqx-ci-robot Date: Tue, 15 Jul 2025 11:04:13 +0000 Subject: [PATCH] sync blog --- README-ZH.md | 1 + README.md | 1 + ...-dive-into-emqx-s-websocket-performance.md | 145 +++++++++++ zh/202507/emqx-nats-gateway.md | 237 ++++++++++++++++++ 4 files changed, 384 insertions(+) create mode 100644 en/202507/a-deep-dive-into-emqx-s-websocket-performance.md create mode 100644 zh/202507/emqx-nats-gateway.md diff --git a/README-ZH.md b/README-ZH.md index 1ec956e5..e5d3b500 100644 --- a/README-ZH.md +++ b/README-ZH.md @@ -199,6 +199,7 @@ Explore more with & via EMQ. ## [EMQX Open Source | Broker](https://www.emqx.com/zh/blog/category/emqx) EMQX is the world's most scalable open-source MQTT broker with a high performance that connects 100M+ IoT devices in 1 cluster, while maintaining 1M message per second throughput and sub-millisecond latency. +- [突破连接边界:EMQX 实现 MQTT 和 NATS 协议双向互通](https://www.emqx.com/zh/blog/emqx-nats-gateway) ([Edit](https://github.com/emqx/blog/blob/main/zh/202507/emqx-nats-gateway.md)) - [EMQX + Amazon S3 Tables:从实时物联网数据到数据湖仓](https://www.emqx.com/zh/blog/from-real-time-iot-data-to-big-data-analytics) ([Edit](https://github.com/emqx/blog/blob/main/zh/202507/from-real-time-iot-data-to-big-data-analytics.md)) - [智能网联 + AI:EMQX 5.10.0 大模型集成功能介绍](https://www.emqx.com/zh/blog/introducing-llm-integration-in-emqx-5-10) ([Edit](https://github.com/emqx/blog/blob/main/zh/202507/introducing-llm-integration-in-emqx-5-10.md)) - [EMQX 5.10.0 发布:AI 驱动数据流处理、Doris/S3 Tables 集成、NATS 协议网关](https://www.emqx.com/zh/blog/emqx-enterprise-5-10-0-release-notes) ([Edit](https://github.com/emqx/blog/blob/main/zh/202506/emqx-enterprise-5-10-0-release-notes.md)) diff --git a/README.md b/README.md index fe4aa634..1a50c8d6 100644 --- a/README.md +++ b/README.md @@ -213,6 +213,7 @@ Explore more with & via EMQ. ## [EMQX Open Source | Broker](https://www.emqx.com/en/blog/category/emqx) EMQX is the world's most scalable open-source MQTT broker with a high performance that connects 100M+ IoT devices in 1 cluster, while maintaining 1M message per second throughput and sub-millisecond latency. +- [Scaling to 2 Million: A Deep Dive into EMQX's WebSocket Performance](https://www.emqx.com/en/blog/a-deep-dive-into-emqx-s-websocket-performance) ([Edit](https://github.com/emqx/blog/blob/main/en/202507/a-deep-dive-into-emqx-s-websocket-performance.md)) - [EMQX NATS Gateway: Enabling MQTT-NATS Bidirectional Interoperability](https://www.emqx.com/en/blog/emqx-nats-gateway) ([Edit](https://github.com/emqx/blog/blob/main/en/202507/emqx-nats-gateway.md)) - [From Real-Time IoT Data to Big Data Analytics with Amazon S3 Tables and EMQX](https://www.emqx.com/en/blog/from-real-time-iot-data-to-big-data-analytics) ([Edit](https://github.com/emqx/blog/blob/main/en/202507/from-real-time-iot-data-to-big-data-analytics.md)) - [Real-Time AI for IoT: Introducing LLM Integration in EMQX 5.10](https://www.emqx.com/en/blog/introducing-llm-integration-in-emqx-5-10) ([Edit](https://github.com/emqx/blog/blob/main/en/202506/introducing-llm-integration-in-emqx-5-10.md)) diff --git a/en/202507/a-deep-dive-into-emqx-s-websocket-performance.md b/en/202507/a-deep-dive-into-emqx-s-websocket-performance.md new file mode 100644 index 00000000..42eec7f4 --- /dev/null +++ b/en/202507/a-deep-dive-into-emqx-s-websocket-performance.md @@ -0,0 +1,145 @@ +## Introduction + +In the world of real-time web applications - from live financial dashboards and collaborative whiteboards to interactive gaming and large-scale IoT fleet management - the underlying communication protocol is the engine that drives the experience. While [MQTT](https://www.emqx.com/en/blog/the-easiest-guide-to-getting-started-with-mqtt) is the de facto standard for IoT, **[MQTT over WebSocket](https://www.emqx.com/en/blog/connect-to-mqtt-broker-with-websocket)** has become the go-to solution for bridging the gap between backend services and browser-based clients, offering robust, bidirectional communication that easily traverses corporate firewalls. + +But as applications scale, a critical question emerges: how many concurrent WebSocket connections can a broker *truly* handle? At EMQX, we continuously push the boundaries of what's possible. Following our successful test of [100 million MQTT/TCP connections](https://www.emqx.com/en/blog/reaching-100m-mqtt-connections-with-emqx-5-0), we turned our attention to WebSockets. + +We are thrilled to announce that **a single cluster of EMQX 5.10 has successfully sustained 2 million concurrent MQTT over WebSocket connections**, demonstrating not only massive scalability but also exceptional low latency and resource efficiency. + +In this post, we’ll take you on the journey to 2 million connections, detailing our test setup, the results at each stage, and the architectural principles of EMQX that make this remarkable performance possible. + +## The Challenge: From 500k to 2M + +Our goal was to simulate a real-world, large-scale scenario: a huge number of WebSocket clients subscribing to unique topics and receiving a steady stream of messages. We designed a progressive benchmark, doubling the connection load at each stage to observe how EMQX scaled. + +**The Test Scenario:** + +- **Connections:** Each client establishes a persistent WebSocket connection. +- **Subscriptions:** Each client subscribes to a unique topic (`t/%i`) with QoS 1. +- **Publishing:** A corresponding number of publisher clients send messages to these unique topics. +- **Message Flow:** 1-to-1 communication, with a message rate of approximately 1 message every 10 seconds per connection. +- **Payload Size:** 256 bytes. + +All tests were conducted using our open-source [Performance testing framework based on Terraform](https://github.com/emqx/tf-emqx-performance-test) and the powerful [emqtt-bench](https://github.com/emqx/emqtt-bench) load generation tool. + +## Stage 1: Establishing a Baseline at 500,000 Connections + +Every journey starts with a single step. For us, that was establishing a solid baseline with 500,000 concurrent WebSocket connections. + +**EMQX Cluster Setup:** + +- **Nodes:** 2 EMQX core nodes +- **Instance Type:** AWS `c7g.4xlarge` (16 vCPU, 32 GiB RAM) + +**Results:** + +| Metric | Value | +| :------------------------ | :-------------- | +| Live Connections | 500,000 | +| Message Rate (in/out) | ~25,000 msg/s | +| Avg. CPU Usage (per node) | ~51% | +| Avg. RAM Usage (per node) | ~60% | +| Network RX/TX (per node) | ~50 / 54 Mbit/s | + +The cluster handled the 500,000 connections with ease. CPU and memory usage were stable and well within healthy limits, leaving plenty of headroom. This successful first stage confirmed our setup was solid and ready for the next challenge. + +![image.png](https://assets.emqx.com/images/9590274e4f044cab5136b401199cf312.png) + +
EMQX Dashboard showing 500k WebSocket connections
+ +## Stage 2: Doubling Down to 1 Million Connections + +With the baseline established, we doubled the stakes. The goal was to reach 1 million connections and see if EMQX would scale linearly. To accommodate the increased load, we vertically scaled our cluster nodes. + +**EMQX Cluster Setup:** + +- **Nodes:** 2 EMQX core nodes +- **Instance Type:** AWS `c7g.8xlarge` (32 vCPU, 64 GiB RAM) + +**Results:** + +| Metric | Value | +| :------------------------ | :---------------- | +| Live Connections | 1,000,000 | +| Message Rate (in/out) | ~50,000 msg/s | +| Avg. CPU Usage (per node) | ~46% | +| Avg. RAM Usage (per node) | ~55% | +| Network RX/TX (per node) | ~115 / 119 Mbit/s | + +![image.png](https://assets.emqx.com/images/d480a5cc6391bb91daaec63c691fb6dc.png) + +
EMQX Dashboard showing 1M WebSocket connections
+ +## Stage 3: The Summit - 2 Million Connections + +Reaching 1 million connections on just two nodes is impressive, but for the 2 million mark, we adopted the standard EMQX architecture for massive-scale deployments: separating the Core and Replicant nodes. + +**Why Core and Replicant Nodes?** +This architecture is a cornerstone of EMQX's scalability. + +- **Core Nodes:** Handle cluster management, routing information, and data persistence. They are the "brains" of the cluster. +- **Replicant Nodes:** Are stateless and handle the heavy lifting of client connections and message traffic. They can be added or removed from the cluster seamlessly to scale capacity horizontally. + +This separation of concerns prevents management overhead from interfering with connection performance, which is crucial at extreme scales. + +**EMQX Cluster Setup:** + +- **Core Nodes:** 2 x `c7g.2xlarge` (8 vCPU, 16 GiB RAM) +- **Replicant Nodes:** 4 x `c7g.8xlarge` (32 vCPU, 64 GiB RAM) + +**Results:** + +| Metric | Value | +| :-------------------- | :----------------- | +| Live Connections | **2,000,000** | +| Message Rate (in/out) | **~100,000 msg/s** | + +The resource usage tells a powerful story: + +- **Core Nodes:** CPU usage was negligible (~1%), as they were dedicated to management tasks, not connection handling. +- **Replicant Nodes:** Avg. CPU usage ranged from 56% to 69%, and RAM was stable at ~54%. The four replicant nodes efficiently distributed the 2 million connections, with each handling approximately 500,000 clients. + +![image.png](https://assets.emqx.com/images/68b2b2f1ce8ef189813db7b41113af77.png) + +
EMQX Dashboard showing 2M WebSocket connections
+ +#### The Crown Jewel: End-to-End Latency + +Handling a massive number of connections is one thing; doing it with low latency is another. At 2 million connections and nearly 100,000 messages per second, the end-to-end latency was exceptionally low. + +| Metric | Value | +| ----------------------- | ----------- | +| **e2e_latency_ms_95th** | **0.96 ms** | +| **e2e_latency_ms_99th** | **4.66 ms** | + +A 99th percentile latency of under 5 milliseconds at this scale is a testament to the efficiency of EMQX's internal message passing and the performance of its underlying Erlang/OTP runtime. This level of responsiveness is critical for applications where every millisecond counts. + +## Key Technical Insights + +1. **Linear Scalability is Key:** The journey from 500k to 1M showed that you can predictably scale EMQX by adding hardware resources. The move to 2M proved that you can scale even further by adding more nodes to the cluster. +2. **Core-Replicant Architecture Shines:** For deployments in the millions, the Core-Replicant model is the proven path. It ensures that the cluster remains stable and manageable while the Replicant nodes focus solely on performance. +3. **Efficiency of the Underlying Stack:** This level of performance wouldn't be possible without a highly optimized tech stack. EMQX benefits from the battle-tested Erlang/OTP environment, designed for building concurrent, fault-tolerant systems, and the high-performance [Cowboy web server](https://www.google.com/url?sa=E&q=https%3A%2F%2Fninenines.eu%2Farticles%2Fcowboy-2.13.0-performance%2F) for handling the WebSocket connections. +4. **A Note on Load Balancers:** For maximum performance and to eliminate variables, these tests were conducted by connecting load generators directly to the EMQX replicant nodes. In a production environment, a high-performance TCP/SSL load balancer would be placed in front of the replicant nodes. The test results show that EMQX itself is not the bottleneck. + +## Conclusion + +The digital world's demand for real-time interaction over the web is insatiable. This benchmark demonstrates that EMQX is more than ready to meet this demand. By successfully supporting **2 million concurrent MQTT over WebSocket connections** in a single cluster with outstanding low latency, EMQX establishes itself as a leading solution for any large-scale application requiring real-time data movement to and from browser clients. + +Whether you are building the next generation of collaborative tools, financial trading platforms, or a massive IoT network with a web-based dashboard, you can build with the confidence that EMQX provides the performance, scalability, and reliability you need. + +Ready to see for yourself? + +- [Download EMQX](https://www.emqx.com/en/downloads-and-install/enterprise) or deploy [EMQX Serverless](https://www.emqx.com/en/cloud/serverless-mqtt) completely for free + +- Explore the project on [GitHub](https://github.com/emqx/emqx). + +- [Contact our team](https://www.emqx.com/en/contact) to discuss your unique use case. + + + +
+
+ Talk to an Expert +
+ Contact Us → +
diff --git a/zh/202507/emqx-nats-gateway.md b/zh/202507/emqx-nats-gateway.md new file mode 100644 index 00000000..f4ae9ce9 --- /dev/null +++ b/zh/202507/emqx-nats-gateway.md @@ -0,0 +1,237 @@ +在当今高度互联的数字化世界中,实时数据流的复杂性日益增长,尤其是在物联网(IoT)和微服务架构领域。企业和开发者面临着一个普遍的挑战:各种数据协议和系统往往各自为政,形成难以逾越的“数据孤岛”。这种碎片化的局面不仅增加了开发和维护的巨大开销,还阻碍了对数据潜力的全面发掘和利用,导致关键业务洞察和实时决策的缺失。 + +作为服务于物联网实时智能的统一 MQ + AI 平台,EMQX 致力于提供高效可靠的物联网连接。自 5.0 版本起,EMQX 便引入了强大的协议网关特性,旨在打破传统 [MQTT 协议](https://www.emqx.com/zh/blog/the-easiest-guide-to-getting-started-with-mqtt)的边界,使其能够接收来自其他非 MQTT 协议的客户端连接。这一创新为 EMQX 赋予了卓越的多协议接入能力,使其成为一个真正意义上的统一消息平台,能够无缝集成各种异构系统和设备 。 + +![image.png](https://assets.emqx.com/images/fe9cc883cb77c71da319e2da399ec636.png) + +随着最新版本 EMQX 5.10.0 的发布,EMQX 协议网关家族又迎来了一位新成员:**EMQX NATS Gateway**。这项新功能进一步扩展了 EMQX 的连接边界,实现了 MQTT 与 NATS 协议之间的原生、双向互通,为构建更灵活、更强大的实时数据基础设施提供了前所未有的可能性 。 + +## 什么是 NATS 协议 + +[NATS(Neural Autonomic Transport System)](https://nats.io/)是一个高性能、轻量级、云原生的消息系统,专为现代分布式应用设计。它以其简洁、高效的特点而闻名,支持发布-订阅(Publish-Subscribe)、请求-响应(Request-Reply)等多种消息模式,并提供了丰富的客户端库,覆盖多种编程语言 。 + +**NATS 的核心特点包括:** + +- **高性能与低延迟:** NATS 采用轻量级协议和优化的路由机制,确保消息以极高的吞吐量和低延迟进行传输。Core NATS 提供“至多一次”(At-most-once)的消息传递语义,适用于对速度和可用性要求极高的场景 。 +- **云原生设计:** NATS 从设计之初就考虑了云环境的特性,易于部署在裸机、虚拟机、容器或 Kubernetes 等任何环境中,并支持集群化部署以实现高可用性和可扩展性 。 +- **简洁性:** NATS 协议简单,客户端库易于使用,降低了开发和运维的复杂性 。 +- **主题寻址:** NATS 基于主题(Subject)进行消息路由,并支持单层和多层的主题通配符,这使得 M:N(多对多)通信变得轻松。 + +| | **MQTT** | **NATS** | +| :------------- | :----------------------------------------------------------- | :----------------------------------------------------------- | +| **传输层** | TCP/TLSWebsocket/Websocket over SSL | TCP/TLSWebsocket/Websocket over SSL | +| **报文格式** | 二进制形式,报文紧凑且字段丰富 | 使用 PlainText,但语义非常精简 | +| **客户端连接** | 支持持久会话 (Clean Session),可断线重连保持会话状态和未送达消息 | 无持久化 Session 概念 | +| **消息模式** | 发布/订阅 (Publish/Subscribe) | 发布/订阅 (Publish/Subscribe)
请求/回复 (Request/Reply)
队列 (Queueing) | +| **主题** | **分层主题 (Topic)**,使用 `/` 进行层级划分,支持通配符 (`+`, `#`) | **扁平化 Subject**,使用 `.` 进行分隔,同样支持通配符 (`*`, `>`) | +| **消息质量** | **内置 QoS Quality of Service 级别:**
QoS0: 最多一次
QoS1: 至少一次
QoS2: 恰好一次 | 通过确认机制控制:
最多一次 (不开启确认机制)
至少一次 (开启确认机制) | +| **消息持久化** | **内置持久会话和保留消息,**依赖 Broker 实现 QoS 1/2 消息的持久存储 | **核心 NATS 无持久化**(JetStream 扩展提供强大、可配置的消息持久化 ) | +| **适用场景** | 物联网设备、传感器网络、移动应用、智能家居、工业自动化等资源受限或网络不稳定的环境。 | 微服务通信、实时数据流处理、命令与控制系统、金融服务、事件驱动架构、云原生应用。 | +| **生态系统** | 广泛应用于 IoT 领域,拥有丰富的客户端库、开源和商业 Broker 实现。 | CNCF (云原生计算基金会) 项目,在云原生、微服务和实时通信领域快速发展。 | + +尽管 NATS 和 MQTT 在各自领域都表现出色,但它们之间存在协议差异,传统上需要复杂的定制桥接才能实现互通。EMQX NATS Gateway 的出现,正是为了弥合这一鸿沟。它打通了 NATS 和 MQTT 协议,使得 IoT 设备(通常使用 MQTT)能够与后端微服务(通常使用 NATS)无缝共享数据,从而打破数据孤岛,为构建更全面、更具洞察力的应用提供了无限可能。这种集成不仅简化了系统架构,还为企业带来了前所未有的灵活性,使其能够根据具体需求选择最适合的协议,同时确保所有组件之间的无缝通信 。 + +## EMQX NATS Gateway 的快速配置与启动 + +本节将为您提供一个快速指南,介绍如何安装最新版本的 EMQX 5.10.0,以及如何配置和使用 EMQX NATS Gateway。 + +### 安装 EMQX 5.10.0 + +首先,您需要安装 EMQX 5.10.0。您可以从 [EMQX 官方下载页面](https://www.emqx.com/en/downloads-and-install/enterprise) 获取适用于您操作系统的安装包(例如 Debian 或 macOS 等)。 + +此处,以 Docker 为例: + +```shell +docker run --name emqx \ + -p 18083:18083 -p 1883:1883 -p 20243:20243 \ + -d emqx/emqx-enterprise:5.10.0 +``` + +启动成功后,您可以通过访问 [http://localhost:18083/ ](http://localhost:18083/)进入 EMQX Dashboard,默认用户名密码为 `admin`/`public` 。 + +### 开启和配置 NATS Gateway + +EMQX NATS Gateway 的配置非常灵活,可以通过 Dashboard 或配置文件进行: + +1. 登录 EMQX Dashboard。 + +2. 在左侧导航栏中,点击 **“管理” -> “网关”**。 + +3. 找到 **NATS** 网关,点击 **“配置”** 。 + + ![image.png](https://assets.emqx.com/images/0a2b925d6143a1ccefead552aa29dcf0.png) + +4. 进入**基础参数**配置,保持默认即可 + + ![image.png](https://assets.emqx.com/images/9ab6901d9a9bd85070ab159ff6addff5.png)其中: + + - **挂载点:**为所有 NATS 客户端发布/订阅的主题设置一个固定前缀。此处为空,表示不设置任何前缀 + - **默认心跳间隔:**配置 NATS 网关向客户端发送心跳的间隔时间 + - **心跳超时阈值:**即网关等待心跳的最大超时时间。此处为 5 秒,即 5 秒后未收到客户端的心跳应答,即认为客户端已断线。 + +5. 点击**下一步**,进入到监听器配置页面,点击**添加监听器**。配置监听器名称为 `default` 监听地址为 `20243` 端口,点击 **添加** 完成监听器配置。 + + ![image.png](https://assets.emqx.com/images/1cf2a6e9f93afd66f516197f055e2990.png) + +6. 设置完成后,点击启用即完成 NATS 网关的配置和启动。 + + ![image.png](https://assets.emqx.com/images/e961a1032921d228f77ee2cae4f3eb2c.png) + +## 使用演示:通过 Python 客户端代码实现 NATS 与 MQTT 消息互通 + +本节将通过 Python 客户端代码示例,演示如何连接 NATS Gateway,实现 NATS 客户端与 [MQTT 客户端](https://www.emqx.com/zh/blog/mqtt-client-tools)之间的双向消息互通。 + +首先,确保您已安装 Python 环境,并安装了 NATS 和 Paho MQTT 客户端库: + +```shell +pip install nats-py paho-mqtt +``` + +我们将演示以下两种情况: + +1. NATS 客户端发布消息,MQTT 客户端订阅并接收。 +2. MQTT 客户端发布消息,NATS 客户端订阅并接收。 + +### **NATS 客户端发布,MQTT 客户端接收** + +`nats_publisher.py` **:**此脚本连接到 EMQX NATS Gateway,并向 `iot.sensor.data.temperature` Subject 发布消息: + +```python +import asyncio +import nats + +async def run(): + nc = await nats.connect(servers=["nats://localhost:20243"]) + print("NATS Publisher connected to EMQX NATS Gateway.") + + subject = "sensor.data.temperature" + message = b'{"device_id": "sensor_001", "temp": 25.5}' + + await nc.publish(subject, message) + print(f"Published NATS message to subject '{subject}': {message.decode()}") + + await nc.drain() + print("NATS Publisher disconnected.") + +if __name__ == '__main__': + asyncio.run(run()) +``` + +`mqtt_subscriber.py`**:**此脚本连接到 EMQX MQTT 监听器,并订阅映射后的 MQTT 主题 `sensor/data/temperature`。 + +```python +import paho.mqtt.client as paho +from paho import mqtt +import time + +# MQTT 消息回调函数 +def on_message(client, userdata, msg): + print(f"Received MQTT message on topic '{msg.topic}': {msg.payload.decode()}") + +def run(): + client = paho.Client(client_id="", userdata=None, protocol=paho.MQTTv5) + client.on_message = on_message + + # 连接到 EMQX MQTT 监听器 (默认端口 1883) + client.connect("localhost", 1883, 60) + print("MQTT Subscriber connected to EMQX.") + + # 订阅映射后的 MQTT 主题 + # 根据 NATS Gateway 的 topic_mapping 规则,iot.sensor.data.temperature 映射到 sensor/data/temperature + client.subscribe("sensor/data/temperature", qos=1) + print("MQTT Subscriber subscribed to 'sensor/data/temperature'.") + + client.loop_forever() + +if __name__ == '__main__': + run() +``` + +**运行步骤:** + +1. 首先运行 `mqtt_subscriber.py`。 +2. 然后运行 `nats_publisher.py`。 您将看到 `mqtt_subscriber.py` 接收到 NATS 客户端发布的消息。 + +### **MQTT 客户端发布,NATS 客户端接收** + +`mqtt_publisher.py` :此脚本连接到 EMQX MQTT 监听器,并向 `command/device/light_001` 主题发布消息。 + +```python +import paho.mqtt.client as paho +from paho import mqtt +import time + +def run(): + client = paho.Client(client_id="", userdata=None, protocol=paho.MQTTv5) + + # 连接到 EMQX MQTT 监听器 (默认端口 1883) + client.connect("localhost", 1883, 60) + print("MQTT Publisher connected to EMQX.") + + topic = "command/device/light_001" + message = '{"action": "turn_on", "brightness": 80}' + + client.publish(topic, message, qos=1) + print(f"Published MQTT message to topic '{topic}': {message}") + + client.disconnect() + print("MQTT Publisher disconnected.") + +if __name__ == '__main__': + run() +``` + +`nats_subscriber.py`**:**此脚本连接到 EMQX NATS Gateway,并订阅映射后的 NATS Subject `command.device.light_001`。 + +```python +import asyncio +import nats + +async def message_handler(msg): + print(f"Received NATS message on subject '{msg.subject}': {msg.data.decode()}") + +async def run(): + # 连接到 EMQX NATS Gateway + nc = await nats.connect(servers=["nats://localhost:20243"]) + print("NATS Subscriber connected to EMQX NATS Gateway.") + + # 订阅映射后的 NATS Subject + # command/device/light_001 映射到 command.device.light_001 + await nc.subscribe("command.device.light_001", cb=message_handler) + print("NATS Subscriber subscribed to 'device.command.light_001'.") + + # 保持连接,等待消息 + try: + while True: + await asyncio.sleep(1) + except asyncio.CancelledError: + pass + finally: + await nc.drain() + print("NATS Subscriber disconnected.") + +if __name__ == '__main__': + asyncio.run(run()) +``` + +**运行步骤:** + +1. 首先运行 `nats_subscriber.py`。 +2. 然后运行 `mqtt_publisher.py`。 您将看到 `nats_subscriber.py` 接收到 MQTT 客户端发布的消息。 + +通过这些简单的示例,您可以看到 EMQX NATS Gateway 无缝地在 MQTT 和 NATS 协议之间转换和转发消息,极大简化了异构系统间的集成工作。 + +## 总结 + +EMQX 5.10.0 NATS Gateway 的发布,是 EMQX 在构建统一、灵活的实时数据基础设施方面迈出的又一重要步伐。它通过提供 MQTT 和 NATS 协议之间的原生、双向互通能力,有效地打破了实时通信领域长期存在的协议壁垒,为构建更加互联互通、灵活高效的分布式系统奠定了坚实基础。 + +这项创新不仅显著简化了复杂的集成挑战,消除了对定制桥接或独立消息中间件的需求,从而降低了开发和运营成本,更开启了物联网、微服务和实时控制等领域应用的新篇章。无论是智能工厂中的传感器数据流向云端微服务进行实时分析,还是后端控制系统向边缘设备发送指令,EMQX NATS Gateway 都能够确保数据在不同协议生态系统之间自由、高效地流动。 + + + +
+
+ 咨询 EMQ 技术专家 +
+ 联系我们 → +