High-performance MQTT+UDP to WebSocket bridge service based on libuv + libwebsockets
Windows:
- Visual Studio 2019/2022 (Recommended)
- CMake 3.16+
- Git
Linux/macOS:
- GCC 7+ or Clang 8+
- CMake 3.16+
- Git
git clone https://github.com/nxtreaming/mqtt-websocket-proxy
cd mqtt-websocket-proxy
# Initialize submodules
git submodule update --init --recursive
# If you need to add submodules
git submodule add https://github.com/libuv/libuv.git third_party/libuv
git submodule add https://github.com/warmcat/libwebsockets.git third_party/libwebsockets
- Download libuv to
third_party/libuv/
- Download libwebsockets to
third_party/libwebsockets/
- Download nlohmann/json to
third_party/nlohmann/
mkdir build
cd build
# Visual Studio 2019
cmake .. -G "Visual Studio 16 2019" -A x64
# Visual Studio 2022
cmake .. -G "Visual Studio 17 2022" -A x64
# Build
cmake --build . --config Release
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j$(nproc)
# Copy configuration file
cp config/gateway.json.example config/gateway.json
# Edit configuration file
vim config/gateway.json # Linux/macOS
notepad config/gateway.json # Windows
# Run program
./bin/mqtt-websocket-proxy # Linux/macOS
.\bin\Release\mqtt-websocket-proxy.exe # Windows
mqtt-websocket-proxy/
βββ CMakeLists.txt # Main CMake configuration
βββ src/ # Source code
β βββ main.cpp # Program entry point
β β
β βββ server/ # Server core implementation
β β βββ gateway_server.cpp # Main gateway server implementation
β β βββ gateway_server.h # Gateway server interface
β β βββ mqtt_server.cpp # MQTT server implementation
β β βββ mqtt_server.h # MQTT server interface
β β βββ udp_server.cpp # UDP server implementation
β β βββ udp_server.h # UDP server interface
β β
β βββ protocol/ # Protocol handling
β β βββ mqtt_packet.cpp # MQTT packet serialization/deserialization
β β βββ mqtt_packet.h # MQTT packet definitions
β β βββ mqtt_protocol.cpp # MQTT protocol implementation
β β βββ mqtt_protocol.h # MQTT protocol interface
β β
β βββ connection/ # Connection management
β β βββ mqtt_connection.cpp # MQTT connection handling
β β βββ mqtt_connection.h # MQTT connection interface
β β βββ websocket_bridge.cpp # WebSocket bridge implementation
β β βββ websocket_bridge.h # WebSocket bridge interface
β β
β βββ utils/ # Utility components
β β βββ config_manager.cpp # Configuration management implementation
β β βββ config_manager.h # Configuration interface
β β βββ crypto_utils.cpp # Cryptographic simulated implementation
β | βββ crypto_utils_openssl.cpp # OpenSSL-based crypto implementation
β β βββ crypto_utils.h # Crypto utilities interface
β β βββ logger.cpp # Logging implementation
β β βββ logger.h # Logging interface
β β βββ mcp_proxy.cpp # MCP proxy implementation
β β βββ mcp_proxy.h # MCP proxy interface
β β βββ mqtt_auth.cpp # MQTT authentication implementation
β β βββ mqtt_auth.h # Authentication interface
β β
β βββ common/ # Common definitions
β βββ types.h # Type definitions
β βββ constants.h # Constant definitions
β βββ error_codes.h # Error code definitions
βββ tests/ # Test suite
β βββ test_basic.cpp # Basic functionality tests
β βββ test_mqtt_protocol.cpp # MQTT protocol tests
β βββ test_mqtt_auth.cpp # Authentication tests
β βββ test_mcp_proxy.cpp # MCP proxy tests
β βββ test_js_compatibility.cpp # JavaScript compatibility tests
β βββ test_config_hot_reload.cpp # Configuration reload tests
β βββ test_tcp_server.cpp # TCP server tests
β βββ test_udp_server.cpp # UDP server tests
β βββ test_encryption.cpp # Encryption tests
β βββ test_audio_packet_format.cpp # Audio packet format tests
β βββ test_websocket_reconnection.cpp # WebSocket reconnection tests
β βββ integration_test.cpp # Integration tests for all components
β βββ test_complete_gateway.cpp # End-to-end gateway tests
βββ third_party/ # Third-party libraries
β βββ libuv/ # libuv source code
β βββ libwebsockets/ # libwebsockets source code
β βββ nlohmann/ # JSON library
βββ config/ # Configuration files
βββ gateway.json.example
Edit config/gateway.json
:
{
"mqtt": {
"host": "0.0.0.0",
"port": 1883,
"max_connections": 10000,
"max_payload_size": 8192
},
"udp": {
"host": "0.0.0.0",
"port": 8884,
"public_ip": "your-server-ip"
},
"websocket": {
"production_servers": [
"wss://chat.xiaozhi.me/ws"
],
"development_servers": [
"wss://dev-chat.xiaozhi.me/ws"
],
"development_mac_addresses": [
"aa:bb:cc:dd:ee:ff"
]
},
"logging": {
"enabled": true,
"level": "info",
"file_path": "logs/gateway.log"
}
}
# Use system libraries instead of built-in libraries
cmake .. -DUSE_SYSTEM_LIBUV=ON -DUSE_SYSTEM_LIBWEBSOCKETS=ON
# Disable tests
cmake .. -DBUILD_TESTS=OFF
# Specify installation path
cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local
- High Concurrency: Support 10,000+ concurrent MQTT connections
- Low Latency: MQTT message processing latency < 1ms
- High Throughput: UDP audio data processing > 1000 packets/sec
- Low Memory: Memory usage per connection < 1KB
- Cross-platform: Native support for Windows/Linux/macOS
- Language: C++17
- Network Library: libuv (Async I/O)
- WebSocket: libwebsockets
- JSON: nlohmann/json
- Encryption: OpenSSL/mbedTLS
- Build System: CMake
Feature | Node.js Version | C++ Version |
---|---|---|
Memory Usage | ~50MB | ~5MB |
Startup Time | ~2s | ~0.1s |
Concurrent Connections | 1,000 | 10,000+ |
CPU Usage | Higher | Lower |
Deployment Complexity | Simple | Medium |
The project includes a comprehensive test suite covering all major components:
# Build tests
cmake .. -DBUILD_TESTS=ON
make -j$(nproc) # Linux/macOS
cmake --build . --config Release # Windows
# Run individual tests
./bin/test_basic # Linux/macOS
.\bin\Release\test_basic.exe # Windows
Test | Description |
---|---|
test_basic | Basic functionality and utility tests |
test_mqtt_protocol | MQTT protocol parsing and serialization |
test_mqtt_auth | MQTT authentication mechanisms |
test_mcp_proxy | MCP proxy functionality |
test_js_compatibility | JavaScript compatibility tests |
test_config_hot_reload | Configuration hot reload functionality |
test_tcp_server | TCP server implementation |
test_udp_server | UDP server implementation |
test_encryption | Encryption and decryption tests |
test_audio_packet_format | Audio packet format handling |
test_websocket_reconnection | WebSocket reconnection logic |
test_complete_gateway | End-to-end gateway functionality |
- libuv not found: Ensure submodules are initialized
- OpenSSL errors: Use vcpkg to install OpenSSL on Windows
- Compiler version: Ensure using C++17 compatible compiler
- Port in use: Check if MQTT/UDP ports are used by other programs
- Configuration file errors: Verify JSON format is correct
- Permission issues: Ensure program has permission to bind ports
- Project structure and CMake configuration
- Core types and interface definitions
- Logger system implementation
- Configuration management system
- Gateway server framework
- Error handling system
- MQTT protocol parser (
src/protocol/mqtt_protocol.cpp
) - QoS 0 only - MQTT packet serialization (
src/protocol/mqtt_packet.cpp
) - Core packets - TCP server for MQTT connections (
src/server/mqtt_server.cpp
) - Full implementation - MQTT connection management (
src/connection/mqtt_connection.cpp
) - Complete - WebSocket bridge client (
src/connection/websocket_bridge.cpp
) - Full implementation with auto-reconnection - WebSocket auto-reconnection - Exponential backoff, server failover, infinite retry capability
- UDP server implementation (
src/server/udp_server.cpp
) - Complete with encrypted session management - Audio data encryption (
src/utils/crypto_utils_openssl.cpp
) - AES-128-CTR compatible with JavaScript - Complete message forwarding - MQTT β WebSocket β UDP encrypted audio data
- Session duration tracking - Track and log client session durations
- Goodbye message handling - Send proper goodbye messages with session information
- Configuration hot reload - Dynamic configuration updates without restart
- Performance optimization for high-throughput audio
- Advanced monitoring and metrics
- Complete test suite
- Performance optimization
- Documentation improvements
This is a MQTT forwarding gateway, not a full MQTT broker:
- Accepts MQTT client connections (QoS 0 only)
- Forwards MQTT messages to WebSocket servers
- Handles UDP audio data bidirectionally
- No message persistence or complex broker features
Issues and Pull Requests are welcome!
MIT License