I am integrating MQTT
in our existing application, I have used this https://github.com/chkr1011/MQTTnet library for running an embedded MQTT broker.
Currently the following method is used to start the broker:
public async Task StarBrokerAsync()
{
var optionsBuilder = new MqttServerOptionsBuilder()
.WithConnectionBacklog(ConnectionBacklog)
.WithDefaultEndpointPort(Port);
MqttServer = new MqttFactory().CreateMqttServer();
await MqttServer.StartAsync(optionsBuilder.Build());
}
What I want is to listen for messages in a specific topic without creating a separate client at best. I've not found documentation for the library or any similar questions.
Is there any solutions for the problem?
mqttServer.UseApplicationMessageReceivedHandler(e =>
{
try
{
string topic = e.ApplicationMessage.Topic;
if (string.IsNullOrWhiteSpace(topic) == false)
{
string payload = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
Console.WriteLine($"Topic: {topic}. Message Received: {payload}");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message, ex);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With