in Education by
I am using MQTT client library from this link https://www.hivemq.com/blog/mqtt-client-library-encyclopedia-m2mqtt/ My sample code is as follows :- public partial class Form1 : Form { MqttClient client = null; public Form1() { InitializeComponent(); client = new MqttClient("broker.hivemq.com"); byte code = client.Connect("lenovofullondude"); } private void button1_Click(object sender, EventArgs e) { client = new MqttClient("broker.hivemq.com"); byte code = client.Connect("lenovofullondude"); client.ProtocolVersion = MqttProtocolVersion.Version_3_1; client.MqttMsgPublished += client_MqttMsgPublished; ushort msgId = client.Publish("/my_topic", // topic Encoding.UTF8.GetBytes("MyMessageBody"), // message body MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, // QoS level false); } void client_MqttMsgPublished(object sender, MqttMsgPublishedEventArgs e) { Debug.WriteLine("MessageId = " + e.MessageId + " Published = " + e.IsPublished); } void client_MqttMsgSubscribed(object sender, MqttMsgSubscribedEventArgs e) { Debug.WriteLine("Subscribed for id = " + e.MessageId); } private void button2_Click(object sender, EventArgs e) { try { client = new MqttClient("broker.hivemq.com"); byte code = client.Connect("lenovofullondude"); client.ProtocolVersion = MqttProtocolVersion.Version_3_1; client.MqttMsgSubscribed += Client_MqttMsgSubscribed; client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived; ushort msgId = client.Subscribe(new string[] { "/my_topic" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE }); } catch (Exception H) { } } private void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) { Debug.WriteLine("Received = " + Encoding.UTF8.GetString(e.Message) + " on topic " + e.Topic); } private void Client_MqttMsgSubscribed(object sender, MqttMsgSubscribedEventArgs e) { Debug.WriteLine("Subscribed for id = " + e.MessageId); } } The code works as follows :- I have 2 buttons on my application when I click button 1 it publishes the message. clicking on button 2 is expected to subscribe and receive the published message through MQTT via this handler Client_MqttMsgPublishReceived. Problem:- Button 1 click is working fine but clicking on button 2 the subscribing is working fine but it is not receiving the published message. Can anyone suggest what I am doing wrong. And I am just curious that my current application publishes and receives the message on Topic (using the same code). Is it possible to make two different Application .One application will publish to Topic. And Second Appication will receive message on that Topic (Since it is subscriber) JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Your button click handlers create a new MQTT client for each click and each client uses the same client id. There can only be one client using a given client id at any particular instance so re-using the same client id will disconnect the previous MQTT client. This disconnection means that there is never a window during which a client is subscribed to a topic and another (or the same) client is publishing to the topic. This can be fixed by connecting once in the Form constructor and using the same MQTT connection in your application. This would mean removing the following two lines from both of the click handlers: client = new MqttClient("broker.hivemq.com"); byte code = client.Connect("lenovofullondude"); Is it possible to make two different Application .One application will publish to Topic. And Second Appication will receive message on that Topic (Since it is subscriber) Yes. But both applications will need to different client ids during the MQTT connect.

Related questions

0 votes
    I have an XML document which basically looks like this: SomeSeller 2535 Now I'm trying to parse the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I have a ASP.NET application running on a remote web server and I just started getting this error: ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    Need to read my SMTP email settings defined under system.net section in my web.config file. Below is one example of SMTP email setting defined in web.config file: (Under Section)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I want to make a button Go To Cart visible after adding a product into cart. But when page gets ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I have a fileupload option in my project. It inclueds a query which returns a dataset. It works fine ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    I have tried many solution online but I am unable to fix. First off all i check in bin and it ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    So the problem is i don't get get a value from textbox field on GridView RowUpdating event. Here ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    I have been developing a webpage which uses n number of dropdownlists which are binding dynamically inside a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    I have a master page with a form element and the defaultbutton attribute set to a server-side ImageButton. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 16, 2022 in Education by JackTerrance
0 votes
    I have a Login control placed inside LoginView control contained within a PANEL. I want to set login button ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    I am developing solution where there are three webapi projcets. Each of them is secured with JWT tokens ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    All How can I download a file so the user sees that it is downloading (like with a stream?) I ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I'm working on a DNN skin using Christoc's DNN templates and when I create a normal ASP.NET ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    I have two submit buttons and a few textboxes on my page. When I enter details in the textbox and hit ... model.FirsttName) @Html.TextBoxFor(model => model.LastName) Get History...
asked May 22, 2022 in Education by JackTerrance
0 votes
    Description: I have a solution in Visual Studio which has multiple projects as application layers. Business Logic ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
...