MQTT Client

641 views
Skip to first unread message

shriyansh jain

unread,
May 14, 2015, 1:37:09 AM5/14/15
to rabbitm...@googlegroups.com
Hi,
I have to connect MQTT client to RabbitMQ server. I have enabled MQTT plugin in RabbitMQ Server(version 3.4.4) but confused about the implementation of C# MQTT client. I am using M2MQTT(version 4.1.0) client library but unable to send message on RabbitMQ server using this library. My code is :

            MqttClient client = new MqttClient("Public IP of my RabbitMQ Server");
            client.Connect(Guid.NewGuid().ToString());
            string[] topic = { "/#/queues/test" };
            byte[] qoslevels = { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE };
            client.Subscribe(topic, qoslevels);
            var msg = Encoding.UTF8.GetBytes("temp");
            client.Publish("/#/queues/test", msg, MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE, true);
            client.Unsubscribe(topic);
            client.Disconnect();
            Console.WriteLine("done");

Thanks in advance.

Michael Klishin

unread,
May 14, 2015, 6:33:40 AM5/14/15
to shriyansh jain, rabbitm...@googlegroups.com
You are using patterns in a topic for publishers. I can't immediately say if that is allowed by the spec but it makes no sense to me. Only consumers should use patterns.

Start with a simpler example that uses no patterns.

MK
--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-user...@googlegroups.com.
To post to this group, send email to rabbitm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Laing, Michael

unread,
May 14, 2015, 10:17:10 AM5/14/15
to Michael Klishin, shriyansh jain, rabbitm...@googlegroups.com

The Topic Name in the PUBLISH Packet MUST NOT contain wildcard characters [MQTT-3.3.2-2]

shriyansh jain

unread,
May 15, 2015, 5:38:29 AM5/15/15
to Laing, Michael, Michael Klishin, rabbitm...@googlegroups.com

Hi,
Thanks Laimg and Michael for your quick response.

Please let me know right way to publish data to rabbitmq queue using MQTT protocol through c# client and please let me know how we should write the code for consumer.

Thanks
Shreyansh

Michael Klishin

unread,
May 15, 2015, 6:32:51 AM5/15/15
to shriyansh jain, rabbitm...@googlegroups.com
On 15 May 2015 at 12:38:27, shriyansh jain (shriya...@gmail.com) wrote:
> Please let me know right way to publish data to rabbitmq queue
> using MQTT protocol through c# client and please let me know how
> we should write the code for consumer.

Just don't use any patterns in topics *in the publisher*.

There should be enough MQTT introduction material on the Web,
please do a bit of research.
--
MK

Staff Software Engineer, Pivotal/RabbitMQ


shriyansh jain

unread,
May 21, 2015, 3:25:14 AM5/21/15
to rabbitm...@googlegroups.com, shriya...@gmail.com
Hi,
Thanks for your reply.

I have successfully able to send data on rabbitmq server using mqtt protocol and also able to receive it on message arrived using the code given below.
I am using M2Mqtt library installed (through nugetpackage).

class Program
    {
        static void Main(string[] args)
        {            
            MqttClient client = new MqttClient("Public IP of server");          
            client.Connect("client_id", "username", "password");
            string[] topic = { "/queue/test" };
            byte[] qoslevels = { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE };
            client.Subscribe(topic, qoslevels);
            client.MqttMsgPublished += new MqttClient.MqttMsgPublishedEventHandler(client_MqttMsgPublished);
            client.MqttMsgPublishReceived += new MqttClient.MqttMsgPublishEventHandler(client_PublishArrived);            

            for (int i = 0; i < 10; i++)
            {
                ushort t = client.Publish("/queue/test", Encoding.UTF8.GetBytes("testmessage" + i), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, true);
                Console.WriteLine("Sending: " + "testmessage" + i);
            }   
            
            Console.ReadLine();
        }

        private static void client_MqttMsgPublished(object sender, MqttMsgPublishedEventArgs e)
        {
            Console.WriteLine("Message Published");
            Console.WriteLine(e.IsPublished);
            Console.WriteLine(e.MessageId);
        }

        private static void client_PublishArrived(object sender, MqttMsgPublishEventArgs e)
        {
            Console.WriteLine("Message Received");
            Console.WriteLine(e.Topic);
            Console.WriteLine(Encoding.UTF8.GetString(e.Message));
        }
    }

But i am unable to see the physical existence of my topic on rabbitmq server.
If i want to receive data from my topic later on then i need a receiver that can fetch that data again and again for me and not on message published.
How to create that receiver ??

Satyam Bobby

unread,
Feb 28, 2017, 8:01:40 AM2/28/17
to rabbitmq-users
Hi Shriyansh,

I Seen Your Post, in that you mentioned "how to create receiver???"
Have you Created the Receiver????
Reply all
Reply to author
Forward
0 new messages