0

I am writing an ASP.NET Core MVC app with RabbitMQ. I am able to implement pub/sub patterns successfully. However, I am facing an issue to show subscribe message on the App.

Connection method on HomeController:

    [HttpPost]
    public IActionResult Index(string serverName, string userName, string password, string port)
    {
        RabbitMq rabbitMq = new RabbitMq();
        var pubConnection = rabbitMq.CreateConnection(serverName, userName, password, port);

        if (!pubConnection.IsOpen)
        {
            ViewBag.NotConnected = "Not Connected. Try Again.";
            return View();
        }

        var subConnection = rabbitMq.CreateConnection(serverName, userName, password, port);

        MyClient myClient = new MyClient();
        myClient.CreatePublisher(pubConnection);
        myClient.CreateSubscriber(subConnection);

        return RedirectToAction(actionName: "Index", controllerName: "MyClientXYZ", routeValues: null);
    }

Subscriber method on MyClient class :

public void CreateSubscriber(IConnection pubSubConnection)
{
        var channel = pubSubConnection.CreateModel();

        channel.ExchangeDeclare("MyExchange", ExchangeType.Fanout, durable: true, true);
        channel.ExchangeBind("MyExchange", "ClientExchange", "#", null);
        var slotQueueName = channel.QueueDeclare("my queue", true, autoDelete: true).QueueName;
        channel.QueueBind(slotQueueName, "MyExchange", routingKey: "");

        var consumer = new EventingBasicConsumer(channel);

        consumer.Received += (model, ea) =>
        {
            var body = ea.Body.ToArray();
            var message = Encoding.UTF8.GetString(body);
            Console.WriteLine(" [x] {0}", message);
        };

        channel.BasicConsume(queue: slotQueueName,
            autoAck: true,
            consumer: consumer);
}

I am able to print subscriber messages on the console. But I need to show this message on the UI (View, any JS popup, etc.) I tried many ways like set subscribe message into TempData["subMessage"], redirectToAction(), open JS popup to show subscriber message. But, I couldn't do.

I just want to show the subscriber message on the UI when the callback method executes.

6
  • 1
    I don't quite understand your thought process. You create an RMQ connection in a controller action, return a result, the connection is disposed of.. what provides that enduring connection to the client to push updates? Or is this a polling solution? It feels like you should have your rmq as a constantly running thing somewhere else (not initiated by controller action) that pushes messages, upon their publish, into a SignalR hub to which your javascript client is connected, and that will cause an event at the client side that you can use to update the ui. Commented Apr 18, 2021 at 5:42
  • 1
    Also is there a necessary reason to create multiple connections? Like @Caius says, you usually create one connection for the lifetime of your app and use it to create whatever subscriber/publisher channels you need Commented Apr 18, 2021 at 6:10
  • It might even be possible to have a javascript client subscribe to an RMQ directly (I've never looked/tried) in which case that could hand a boatload off work off to the client<->RMQ and leave the server out of it; anything you want on the client just push it into the queue Commented Apr 18, 2021 at 6:15
  • yea, I need to have two connections. Actually, I am creating a test app for testing another app. Commented Apr 18, 2021 at 6:24
  • We are using signalR for that matter. When event compeltes it calls an signalR event on the client side and updates the data. Might help. Commented Apr 18, 2021 at 9:54

1 Answer 1

2

This is not right. RabbitMQ connections, and consumers, are long lived entities; not something ephemeral that you initiate in a controller action.

The short answer is to use a framework on top of RabbitMQ. Here's two good ones to consider:

The long answer is to roll your own RabbitMQ infrastructure. You do this if you are an expert and have some special need. In that case, maybe start here:

The other answer is to completely re-evaluate what you are trying to achieve.

Good luck.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.