Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RabbitMQ: Sending messages from one pc to another

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.

like image 622
user3043883 Avatar asked Oct 21 '22 08:10

user3043883


1 Answers

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

like image 115
AndersNS Avatar answered Oct 22 '22 23:10

AndersNS