I'm Trying to subscribe from hive mqtt broker installed in my pc using C# Application

Hi i published a message from paho client to MQTT broker and want to subscribe that message using that topic ; my problem is when debugger comes to the line client.MqttMsgPublishReceived += client_MqttMsgPublishReceived; it is not executing the method and moving to the next line.

I use hive mqtt broker and c# Form Application with M2MQTT libraries added.

I want to get the published message in the text box. will you people help me please.

Here is my code;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;

// including the M2Mqtt Library
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;

private void button1_Click(object sender, EventArgs e)
{
  MqttClient client = new MqttClient("IP.ADDRESS");
  client.ProtocolVersion = MqttProtocolVersion.Version_3_1;
  byte code = client.Connect(Guid.NewGuid().ToString());
  ushort msgIds = client.Subscribe(new string[] { @"Factory1\Sensor1" },
                    new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
  textBox1.Text = "";
  client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
}
void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
  this.Invoke((MethodInvoker)delegate ()
  {
    textBox1.Text += "Received = " + Encoding.UTF8.GetString(e.Message) + " 
    on topic " + e.Topic + "\r\n";
  });
}

Hi @VASU,

nice to see your interest in HiveMQ and MQTT.
This is the HiveMQ community forum, where we will happily provide you with information help around all HiveMQ products and MQTT in general to the best of our abilities.
In your case you are looking for guidance in regards to the MQTT Client Library M2Mqtt. The HiveMQ staff cannot help you with that. Maybe you will get some answers, if you try out the M2Mqtt community.

Kind regards,
Florian from the HiveMQ Team.

1 Like