Sending Messages by rabbitMq

4k Views Asked by At

I am using rabbit-Mq in my web app(Asp.net-MVC 4.0). My requirement is to send a message to a particular user. Suppose if user1 is online and he sends a message to user2 by rabbit-Mq. It should be received by "user2" only. The code I have used is a template which stores the message in the queue and whenever the user clicks on receive he will get that message but there is no restriction of particular user in my case. Anyone can get that message which is wrong and I have to handle that. Please help me regarding this.

Do we have something in rabbit-Mq that can distinguish the correct message to correct user/Consumer? Can we set a key with message and check the key while receiving? Is this possible?

Below I am writing the code I am using to send and receive the messages

public ActionResult SendMessage(MessagingModel ObjModel)
        {        var factory = new ConnectionFactory() { HostName = "localhost" };
                using (var connection = factory.CreateConnection())
                {
                    using (var channel = connection.CreateModel())
                    {
                        Message = ObjModel.Message;
                        channel.QueueDeclare("MessageQueue", true, false, false, null);
                        var body = Encoding.UTF8.GetBytes(ObjModel.Message);
   channel.BasicPublish("", "MessageQueue", null, body);
}
}
}

   public JsonResult RecieveMessage()
        {
  var factory = new ConnectionFactory() { HostName = "localhost" };
                using (var connection = factory.CreateConnection())
                {
                    using (var channel = connection.CreateModel())
                    {
                        channel.QueueDeclare("MessageQueue", true, false, false, null);
                        bool noAck = true;
                        BasicGetResult result = channel.BasicGet("MessageQueue", noAck);
                        if (result == null)
                        {
                            Message = "No Messages Found.";
                        }
                        else
                        {
                            IBasicProperties props = result.BasicProperties;
                            byte[] Body = result.Body;
                            Message = Encoding.Default.GetString(Body);
                        }
                    }
                }
1

There are 1 best solutions below

0
On

First, you must remember the following things:

  • All messages in RabbitMQ published through exchanges.
  • Queues binded to exchanges.
  • Event if you publish message directly into queue, actually it still passes through the default exchange - (AMPQ default).
  • There are different kinds of exchanges. You can read a bit about exchanges here: https://www.rabbitmq.com/tutorials/tutorial-three-dotnet.html

In you case you might consider use a topic or headers exchanges, but in this case you should for each user have a queue, and if the number of users in the system is large, then it will be very resource intensive.

Also you can add specific header to you message:

var props = model.CreateBasicProperties();
props.Headers.Add("UserId", userId);

and then in RecieveMessage() method after read message from queue see this header and if message intended for current user - receive it and acknowledge this message, otherwise not acknowledge this message.
But this is bad solution. I would just kept messages from the queue to the database, and then read them out filtering by user.