using MQTTnet.Client.Options;
Console.WriteLine("Subscriber");
var mqttFactory = new MqttFactory();
IMqttClient mqttClient = mqttFactory.CreateMqttClient();
var mqttClientOptions = new MqttClientOptionsBuilder()
.WithClientId(Guid.NewGuid().ToString())
.WithTcpServer("test.mosquitto.org", 1883)
mqttClient.UseConnectedHandler(async e =>
Console.WriteLine("Connected to the Broker Succesfully");
var topicFilter = new TopicFilterBuilder()
.WithTopic("astar/vfarmproject")
await mqttClient.SubscribeAsync(topicFilter);
mqttClient.UseDisconnectedHandler(e =>
Console.WriteLine("Disconnected to the Broker Succesfully");
mqttClient.UseApplicationMessageReceivedHandler(e =>
Console.WriteLine($"Received Message - {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
await mqttClient.DisconnectAsync();