[rabbitmq-discuss] RabbitMq[Functionality with multiple users]

Sukhjeet sukhjeet.singh1 at kindlebit.com
Wed Dec 11 06:23:53 GMT 2013


Hello Team,
                           I am working out with rabbit-mq from some 
days. It is a very nice application and I am trying to incorporating 
this app into my Asp.net MVC 4.0 Web app for providing a facility
                           of mailing system.  I have gone through the 
tutorials and certain examples for C#.

Requirement: Sir, my requirement is to provide a mail like system in the 
web app using Rabbit-Mq. For Example: If I have one user logged in to my 
application and sends a message to another
                         online user , I need to send the message to 
that user. I am using a very basic code given on Rabbit-mq web site but 
I am not getting an idea on how rabbit-mq will server this.As for   now 
I when I click on send message , a message is stored in the queue and 
then when I click on recieve message button on same page, the particular 
message is shown. That is just a template that I have designed.

Can you please help me for my practical situation and I will be really 
thankful to you.

I am using below Code for my template. Please send me the code to 
implement this or just guide me a path by which I can achieve this.


This is a view page::
   public ActionResult Index()
         {
             MessageService ObjMessageService = new MessageService();
             var model = new MessagingModel();
             model.MessageList = ObjMessageService.GetMessageDetails();
             return View(model);
         }



Below function works when "SendMessage" button is pressed
         public ActionResult SendMessage(MessagingModel ObjModel)
         {
             string Message = string.Empty;
             try
             {

                 //var factory = new ConnectionFactory() { HostName = 
"localhost" };
                 //using (var connection = factory.CreateConnection())
                 //using (var channel = connection.CreateModel())
                 //{
                 //    channel.ExchangeDeclare("logs", "fanout");
                 //    var message = ObjModel.Message;
                 //    var body = Encoding.UTF8.GetBytes(message);
                 //    channel.BasicPublish("logs", "", null, body);

                 //}

                 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);
                         //Save Message To dateBase

                         MessageService ObjMessageService = new 
MessageService();
                         int displayOrder = 
ObjMessageService.GetDisplayOrder();
                         if (displayOrder > 0)
                         {
                             int NewOrder = displayOrder + 1;
                             tblMessageDetail ObjTBL = new 
tblMessageDetail();
                             ObjTBL.Message = ObjModel.Message;
                             ObjTBL.Sent_Date = DateTime.Now;
                             ObjTBL.IsReceived = false;
                             ObjTBL.DisplayOrder = NewOrder;
                             int Result = 
ObjMessageService.SaveSentMessage(ObjTBL);
                             if (Result == 1)
                             {
                                 channel.BasicPublish("", 
"MessageQueue", null, body);
                             }
                         }

                     }
                 }
             }
             catch
             {
                 Message = "An Error has been occurred while sending 
messages.";
             }
             return Json(new { MSG = Message });
         }


and   Below message works for receive message::
      [HttpPost]
         public JsonResult RecieveMessage()
         {
             string Message = string.Empty;
             try
             {
                 //var factory = new ConnectionFactory() { HostName = 
"localhost" };
                 //using (var connection = factory.CreateConnection())
                 //{
                 //    using (var channel = connection.CreateModel())
                 //    {
                 //        channel.ExchangeDeclare("logs", "fanout");
                 //        var queueName = channel.QueueDeclare();
                 //        channel.QueueBind(queueName, "logs", "");
                 //        var consumer = new 
QueueingBasicConsumer(channel);
                 //        var ResultBody = 
channel.BasicConsume(queueName, true, consumer);
                 //        Message = 
Encoding.UTF8.GetString(ResultBody.ToArray()));
                 //    }
                 //}

                 var factory = new ConnectionFactory();
                 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);
                         }
                     }
                 }
             }
             catch
             {
                 Message = "An Error has been occurred while receiving 
messages.";
             }
             return Json(new { MSG = Message });
         }

I have attached a  image of the page which can may be give you an Idea 
of the template that I have designed::
Actually the table just shows the sent messages which I save in databse 
when I click on send message button.








CaptureMessaging.PNG




---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20131211/4d8ee76f/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/png
Size: 30775 bytes
Desc: not available
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20131211/4d8ee76f/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: CaptureMessaging.PNG
Type: image/png
Size: 30775 bytes
Desc: not available
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20131211/4d8ee76f/attachment-0001.png>


More information about the rabbitmq-discuss mailing list