title: RabbitMq Implementation Practically
body: <ol>
  <li><p>I am working on rabbitmq and I am confused with some points Like.
      I have just implemented a sample from internet which creates a queue and then it fetch
      the message from that queue thereby showing it on the webpage.
      Now my problem is::
      Suppose My server has RabbitmQ installed and multiple users are accessing this website where I
      have implemented the rabbitmq.Now, first user sends a message but to whome it will send this message?
      To all the users who will open the page because the code will be common for sent message and the name
      of the queue will also be same. </p></li>
  <li><p>Suppose, first user send one message="Hello" on the queue "Queue1"
      now, one other user sends another message="Hello World" on the same queue
      and one more user sends a message="Hello Worl World" on the same queue.
  Now nth user clicks on receive message then which message will be shown to him?
      first ,second or third one?</p></li>
  <li><p>It means we will always have a single queue for my application?</p></li>
  </ol>
  
  <p>Can somebody please guide me. I am pretty much confused...</p>
  
  <p>Below I am pasting the code sample I will be using for my website</p>
  
  <pre><code>//For sending the messge
    protected void btnSendMail_Click(object sender, EventArgs e)
      {
          try
          {
              var factory = new ConnectionFactory();
              using (var connection = factory.CreateConnection())
              {
                  using (var channel = connection.CreateModel())
                  {
                      //  ConnectionFactory factory = new ConnectionFactory() { HostName = "localhost" };
                      // // factory.UserName = txtUserEmail.Text.ToString();
                      ////  factory.Password = "password";
                      //  factory.VirtualHost = "/";
                      //  factory.Protocol = Protocols.FromEnvironment();
                      //  factory.HostName = "localhost";
                      //  factory.Port = AmqpTcpEndpoint.UseDefaultPort;
                      //  IConnection conn = factory.CreateConnection();
                      //  using (var channel = conn.CreateModel())
                      //  {
                      //      channel.QueueDeclare("hello", false, false, false, null);
                      //      string message = "Hello World!";
                      //      var body = Encoding.UTF8.GetBytes(txtUserEmail.Text.ToString());
                      //      channel.BasicPublish("", "hello", null, body);
                      //      conn.Close();
                      //  }
                      //Sending Message
                      channel.QueueDeclare("hello1", false, false, false, null);
  
                      string message = txtUserEmail.Text.ToString();
                      var body = Encoding.UTF8.GetBytes(message);
  
                      channel.BasicPublish("", "hello1", null, body);
                      //Console.WriteLine(" [x] Sent {0}", message);
                      //Console.ReadLine();
                      Label1.Text = Encoding.Default.GetString(body);
                  }
              }
  
          }
          catch
          {
          }
      }
  
  
  //For receiving the message.
   protected void btnReceive_Click(object sender, EventArgs e)
      {
          try
          {
              //var factory = new ConnectionFactory() { HostName = "localhost" };
              //using (var connection = factory.CreateConnection())
              //{
              //    using (var channel = connection.CreateModel())
              //    {
  
              //        channel.QueueDeclare("hello", false, false, false, null);
              //        BasicGetResult result = channel.BasicGet("hello", false);
              //        var consumer = new QueueingBasicConsumer(channel);
              //        channel.BasicConsume("hello", true, consumer);
              //        Console.WriteLine(" [*] Waiting for messages." +
              //                                 "To exit press CTRL+C");
              //        while (true)
              //        {
  
              //            var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
  
              //            var body = ea.Body;
              //            var message = Encoding.UTF8.GetString(body);
              //            Console.WriteLine(" [x] Received {0}", message);
              //            Label1.Text = message.ToString();
              //        }
              //    }
              //}
              var factory = new ConnectionFactory();
              using (var connection = factory.CreateConnection())
              {
                  using (var channel = connection.CreateModel())
                  {
                      bool noAck = false;
                      BasicGetResult result = channel.BasicGet("hello1", noAck);
                      if (result == null)
                      {
  
                      }
                      else
                      {
                          IBasicProperties props = result.BasicProperties;
                          byte[] Body = result.Body;
                          Label1.Text = Encoding.Default.GetString(Body);
  
                      }
                  }
              }
  
  
          }
          catch
          {
          }
      }
  </code></pre>