1

I am trying to send MQTT messages using RabbitMQ as a broker. I wrote the code below and tried to run it. I am able to see the MQTT Queue in the RabbitMQ dashboard, but there is no message. There is no message received, only the queue is being shown in Rabbit :/ Any advice, please?

This is how it looks like: Inline image 1

and this

Inline image 2 Code:

using System;
using System.Text;
using System.Net;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
using System.Threading;

namespace MQTTcl
{
    class Program
    {
      public static void Main()
        {
           // create client instance
           MqttClient client = new MqttClient(IPAddress.Parse("127.0.0.1"));
            // register to message received 
            client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
            string clientId = Guid.NewGuid().ToString();
            client.Connect(clientId);
            // subscribe to the topic "/home/temperature" with QoS 1 
            client.Subscribe(new string[] { "Xhindi" }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE });

        string strValue = Convert.ToString("This is a test message by Xhino!!!");
        string strValue1 = Convert.ToString("This is a test message by Sandri!!!");
        string strValue2 = Convert.ToString("KNP!!!");
        string strValue3 = Convert.ToString("KONTH!!!");
        System.Threading.Timer timer = null;
        timer = new System.Threading.Timer((obj) =>
        {


            // publish a message on "/home/temperature" topic with QoS 1 
            client.Publish("Xhindi", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, false);
           // client.Publish("Xhindi", Encoding.UTF8.GetBytes(strValue1), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, false);
           // client.Publish("Xhindi", Encoding.UTF8.GetBytes(strValue2), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, false);
           // client.Publish("Xhindi", Encoding.UTF8.GetBytes(strValue3), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, false);
           // timer.Dispose();
        },
                    null, 15000, System.Threading.Timeout.Infinite);
        int milliseconds = 5000;
        Thread.Sleep(milliseconds);
        //LoadJson();
        //writeJson();

    }}}
hardillb
  • 54,545
  • 11
  • 67
  • 105
Admir
  • 11
  • 1
  • 2
  • Are you able to connect using something like mosquitto_pub or mosquitto_sub tools ? Can you check the return byte from the Connect method ? Is it possible that the RabbitMQ MQTT support doesn't support client id greather then 23 characters so only MQTT 3.1 and not MQTT 3.1.1 (you are using a Guid so it's greather then 23 chars). – ppatierno Feb 07 '17 at 14:02
  • What I am doing is: 1. Send a lot of mqtt messages through mosquito broker. 2. Do the same with mqtt but this time using RabbitMQ as a broker. I am simulating this for my thesis where I want to show that RabbitMQ is a better solution where we do not use data since information is saved in the queue when the Server (Receiver of the info) goes down. I am simulating a server down-time by putting on sleep the Receiver.cs program I wrote. If I use mqtt on mosquitto, when Receiver.cs "sleeps" I do not receive this data. – Admir Feb 07 '17 at 14:17
  • Usually, Virtual Power Plants use such solutions, since they have to receive a lot of data from hundreds of sensors installed in bulding they manage. Do you have any suggestions how could I deal with what I started? I could use any java client also for mqtt if you say that M2Mqtt is not useful in this case – Admir Feb 07 '17 at 14:18
  • @ppatierno I tested the code with Mosquitto and it works perfectly. No it is not the clientID, I tried the ClientID with 10 characters and its the same. I have the queue, but there is no message on it :/ Also tried to play with ttl, QoS etc Any idea plz? – Admir Feb 07 '17 at 22:45
  • @ppatierno , here is what my server log file shows: =INFO REPORT==== 9-Feb-2017::21:43:14 === accepting MQTT connection <0.986.0> (127.0.0.1:55233 -> 127.0.0.1:1883) =INFO REPORT==== 9-Feb-2017::21:43:28 === MQTT detected network error for "127.0.0.1:55233 -> 127.0.0.1:1883": peer closed TCP connection Which I believe it is normal to show it when I close the client window, so when I close the window of my program, my code. Any idea? I tried to run rabbitmq on different PCs but still the same. – Admir Feb 09 '17 at 20:56
  • Can you check the MQTT traffic using Wireshark ? – ppatierno Feb 10 '17 at 07:03

0 Answers0