Where to subscribe to RabbitMq events in SignalR application

Viewed 435

I have a SignalR application that uses RabbitMq for message processing in the background. (.Net Core 2.1) The process is like this:

  1. Client connects to SignalR hub
  2. Client pushes a message to SignalR hub.
  3. SignalR Hub publishes the message to RabbitMq Queue.
  4. This is critical (I don't know this part)- Something is subscribed to RabbitMq queue
  5. When the message comes to the queue, something notifies clients by using the SignalR hub.

Now the problem I have is in the step 4. That something, I don't know where to put it.

First I tough to put it into SignalR hub, so SignalR hub would subscribe to RabbitMq queue and handle the messages. But SignalR hubs are short-lived and recreated on each request, so when the RabbitMq message is dequeued, the SignalR hub context is already disposed.

Then I tough to create a singleton service at application start and do the subscription to RabbitMq queues there, but at application start I don't know who are the clients that need's to subscribe to the RabbitMq queue. Requirements are that a subscription is made for each consumer client that connects to the SignalR hub.

Because I use "topic" exchange where the topic and clientId is know only when the consumer client is connected, i need to subscribe to RabbitMq when the consumer client connects to SignalR hub ie. (OnConnected method on SignalR hub)

I need help on this, how to do the subscription to RabbitMq queue on SignalR client connected, and this needs to be long lived.and cliendId

0 Answers
Related