咨询:是否存在支持TCP与WebSocket同时连接的MQTT Broker?
Hey there! Great question—yes, absolutely, there are plenty of MQTT brokers that support both TCP and WebSocket connections simultaneously, and even the MQTTnet library you're using can do this (you just might have missed the configuration for multiple endpoints). Let me break this down for you:
1. 开源MQTT Broker推荐
These are popular, battle-tested options that work out of the box (or with minimal config) for your use case:
- EMQX: One of the most widely used open-source MQTT brokers, it enables both TCP (default port 1883) and WebSocket (default port 8083) connections by default. It comes with a web-based management console that makes it easy to monitor connections, topics, and message flow—super friendly for beginners.
- Mosquitto: A lightweight, classic MQTT broker. To enable WebSocket alongside TCP, you just need to add a few lines to its configuration file (
mosquitto.conf):
Restart the broker, and it'll handle both TCP and WebSocket connections seamlessly.listener 1883 listener 8083 protocol websockets
2. 用MQTTnet实现多端点支持
You don't have to switch brokers if you prefer MQTTnet! It absolutely supports running multiple endpoints (TCP + WebSocket) at the same time. Here's a quick code example to set this up:
using MQTTnet; using MQTTnet.Server; var options = new MqttServerOptionsBuilder() // Configure TCP endpoint (default port 1883) .WithDefaultEndpoint() .WithDefaultEndpointPort(1883) // Configure WebSocket endpoint (port 8083) .WithWebSocketEndpoint() .WithWebSocketEndpointPort(8083) .Build(); var server = new MqttFactory().CreateMqttServer(); await server.StartAsync(options); Console.WriteLine("MQTT server running with TCP (1883) and WebSocket (8083) endpoints!");
This will start a single MQTTnet server that listens for both TCP connections (for your devices) and WebSocket connections (for your web app).
Quick Tip
If you're just getting started, EMQX might be the easiest pick since it requires almost no configuration to get both connection types up and running. But if you're already invested in the .NET ecosystem, configuring MQTTnet for multiple endpoints is straightforward and avoids switching tools.
内容的提问来源于stack exchange,提问作者Dmitry




