5

I am trying to get a rabbitMQ queue set up that sits on one pc, and recieves messagess from other computers giving it tasks. I have followed all the tutorials on the rabbit website but these only apply to local host. Can someone explain how I get this same code to communicate across 2 computers, not just from the same computer.

I have the following code:

Sender.cs

class Send
{
    static void Main(string[] args)
    {
        Console.WriteLine("------------------");
        Console.WriteLine("RabbitMQ Test");
        Console.WriteLine("------------------");

        var factory = new ConnectionFactory() { HostName = "localHost" };

        try
        {
            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare("abc", false, false, false, null);

                    Console.WriteLine("Enter the messages you want to send (Type 'exit' to close program)...");

                    string message = null;
                    while (message != "exit")
                    {
                        message = Console.ReadLine();
                        var body = Encoding.UTF8.GetBytes(message);
                        channel.BasicPublish("", "abc", null, body);
                    }

                }
            }
        }
        catch (Exception e)
        {
            string message = e.ToString();
        }
    }

Reciever.cs

    class Recieve
{
    static void Main(string[] args)
    {
        ConnectionFactory factory = new ConnectionFactory()
        {
            HostName = "localhost"
        };

        using (IConnection connection = factory.CreateConnection())
        {
            using (IModel channel = connection.CreateModel())
            {
                channel.QueueDeclare("abc", false, false, false, null);

                QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
                channel.BasicConsume("abc", 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("[Recieved]: {0}", message);
                }
            }
        }

    }
}

Is the idea to get these communicating across 2 computers to change the ConnectionFactory's hostname to the IP of the other computer or something to that extent? I have installed rabbit correctly on both computers, and this code runs correctly on each computer individually. I just need the communication to happen across the computers.

Any help would be greatly appreciated. I can't find any examples of this anywhere on the internet.

user3043883
  • 127
  • 3
  • 5

2 Answers2

3

RabbitMQ is a centralized message queue. You would only install it on your server machine (or a cluster of server machines), and not on each publishing or subscribing client. Clients would then subscribe or publish to queues on the centralized machine.

In your development environment you should decide which of your two machines you want to act as the server. Then pass that hostname and password each client. Both clients should be working towards the same hostname.

var server = "http://127.0.0.1"; // An install of RabbitMQ reachable from both clients
var password = "guest";
var username = "guest";
var connectionFactory = new ConnectionFactory { HostName = server, Password = password , Username = username};

If you want to do message passing without installing something on a server you should take a look at ZeroMQ

AndersNS
  • 1,597
  • 16
  • 24
  • Thanks for your reply. I am very new to this and am unsure if I fully understand. So say I treat computer A as the host and computer B that will send messages. Should computer a have { ConnectionFactory factory = new ConnectionFactory() { HostName = "localhost" }; as the connectionfactory. And computer B would have var server = "Computer A's Ip address" var password = "Computer A's password" var connectionFactory = new ConnectionFactory { HostName = server, Password = password }; Is this the correct way to be thinking about it, or an I off? – user3043883 May 20 '14 at 08:14
  • Yes. Both of your clients should work with the same hostname. – AndersNS May 20 '14 at 08:19
  • I have entered var factory = new ConnectionFactory() { HostName = "192.168.199.95", Password = "psswrd" }; And var factory = new ConnectionFactory() { HostName = "http://192.168.199.95", Password = "psswrd" }; And I get the exception "None of the specified endpoints were reachable". And I doing the ip address wrong from what you can see? – user3043883 May 20 '14 at 08:25
  • Can't help you there. You need to investigate why it's not reachable. Is the IP correct, does it answer to ping from both machines, etc. – AndersNS May 20 '14 at 08:44
  • Thanks for all your replies, I believe I'm getting closer. I can ping the ip. and I get a different exception message to that if I entered in a completly random ip. So it can make some sort of connection. The error I am getting is rabbitmq ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. Do you know anything about this? I have the username and password as the login details for the computer. Is this the correct username and password. Sorry to bother you, but I cannot get this to work. Thank you for any help – user3043883 May 21 '14 at 00:37
  • RabbitMQ has it's own username and password. The default is "guest" for both. :) – AndersNS May 21 '14 at 12:46
0

You can check out shovel plugin - it is built to take messages messages from queue on one node of rabbit and publishes these messages on other one, while taking care of poor networking (WAN). Check the official description

gelmanet
  • 89
  • 2