Where to subscribe to RabbitMq events in SignalR application - rabbitmq

I have a SignalR application that uses RabbitMq for message processing in the background. (.Net Core 2.1)
The process is like this:
Client connects to SignalR hub
Client pushes a message to SignalR hub.
SignalR Hub publishes the message to RabbitMq Queue.
This is critical (I don't know this part)- Something is subscribed to RabbitMq queue
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

Related

How to control subscribing to an exchange via stomp js?

I'm trying to connect to rabbitmq sever from the browser using the stompjs library. Right now every client can subscribe to any exchange. Is there a way to prevent a client from subscribing to a specific exchange?

WCF duplex channel for inter-process communication

I have system like this:
Windows service (WCF, data/events) <-> Web app <-> Web client
I need simultaneous response for clients requests. I have some events from service for clients too. So duplex channel is the way to go. But I need high throughput, because clients calls simultaneously.
Request/reply approach
In order not to serialize channel requests I need more channels for parallel calls, right? But how to handle callback channel then? Ho to keep it still open for receiving events, even on channel errors?
OneWay approach
On channel should be enough (no waiting for data preparation), but how to link data sent to callback with original request, to be able to compose response for client?
What is the way to go? Thank you.
In a simple case, when a web client sends a request to the web app, and web app (possibly) sends a request(s) to WCF service, there's no need in duplex binding at all.
As for events, raised by the service to be fired in Web client, I'd suggest to use a message broker which supports WebSockets - for example RabbitMQ. It has a plugin compatible with WebSockets and WCF binding.
Putting things together, one can create a RabbitMQ server, which accepts messages from WCF service and sends it to Web client, which subscribes to the event feed from Javascript.

Request/response messaging pattern - Azure Service Bus

All ,
I have a doubt on the Request response pattern... Assume the following is my scenario
1.I have a service running on Windows Azure. This service can be called by users to execute a command.
2.I have a client applications that is running on my intranet. This client application will execute the command . The computer in which the client application is running is connected to internet , but does not have a static IP i.e machine cannot be accessed directly via the internet
3.I am planning to use Azure Service Bus through which my service on Windows Azure can communicate with the client application to execute....
In this scenario, can i use Request/response messaging i.e can the service post a message and expect a response from the client
OR
Should i use command queue for each client , the Service will push the command to be executed on a queue , the client will poll the queue and execute a command
Any help is appreciated
Since you are using WCF (based on the tag), you should consider using Service Bus Relay calling the WCF service asynchronously.
I assume you want to use Relaybinding here, using WCF.
Your web service (which is behind NAT, firewall devices, etc) is only opening outbound connections in that case. The service is listening on a registered endpoint in the cloud (that is accessible for him, because of credentials and protocol). All incoming service calls are sent over that port/socket. The response will then be sent back over the outgoing port again.
If the IP Address of your service changes, it wil register itself again (by listening on the same registered endpoint) and you can reach that service transparantly.
Another way you can achieve request/response in an asynchronous fashion, is through queues. This does not require any open connection between your client and your service and can happen fully asynchronous. This can be achieved by sending a message to the request queue for your specific service (with a Correlation Id). And when that service has processed that message, it can send the response to the response queue of your application, using sessions. A good example of this pattern can be found on Alan Smith's blog: http://www.cloudcasts.net/devguide/Default.aspx?id=13051

SignalR - Sending a Message from a WCF Project

I've followed the instructions from https://github.com/SignalR/SignalR/wiki/Hubs
entitled "Broadcasting over a Hub from outside of a Hub".
I got this method working from within an MVC Action in the same project. Requesting the Action sends the update to connected clients.
My problem is that I need to be able to send updates from another project, in particular a WCF Web Services project. My app has an API and a web component and when API users make calls that change things, these updates need to be pushed out to the Web clients via SignalR. And calling a web service with the same code as my Test Action doesn't work.
I also tried the same code inside an nunit unit test that didn't work either.
What do I need to do to make this same method described on the Wiki work for a WCF Project?
The easiest solution is probably to provide an API on your Web Application (use MVC or the new WebAPI) that broadcasts to all connected clients. Any other application (an NT Service, an NUnit test, ...) can call that API if it wants to send a message to the clients.
You can't expect SignalR to do anything if you aren't hosting a Hub either in a Web Application running under IIS, or another application hosting it directly.
If you need two-way communication from your separate application to your clients then simply make your application into a SignalR client too and have it communicate via the Web Application hosted SignalR to the clients and have it listen to messages from them too.
For example, here's how I have configured a complex Service + WebSite + Clients solution (ignore the purple for now):
The Live Web Server allows NT Services to connect and create SignalR Groups. NT Services send to those groups. Web browsers connect to a group and receive messages send to that group. In effect the middle box becomes a pubsubhub.
I cannot get exactly what you aim. But if I understood correctly you're trying to send some kind of notifications raised inside WCF services to SignalR clients.
If that's the case; I can suggest you my approach:
I have some WCF services and a SignalR hub in the same application server. IMHO, the best way to communicate WCF with SignalR hub is by using MSMQ.
When a notification occurs inside a WCF service, it puts the notification payload into MSMQ.
On the other end, SignalR hub listens the same queue. When a message put into the queue, it gets the content and broadcasts to the hub clients. Very easy and straightforward. No extra service/hub call at the server side.
SignalR hub can listen for new queue items by using System.Messaging.MessageQueue#ReceiveCompleted method. When this event raised, SignalR hub gets the queue item and broadcasts to its clients.

How to have Load balanced web services sending messages with nservicebus to multiple subscribers?

I have load balanced web servers, that with the existing code base, handles when a user logs into the site. I would like to send a broadcast message to any applications that have subscribed saying 'hey x logged in' ? So have many web servers and many applications subscribing.
How does discovery work/configuration work with nservicebus ? Should each application know about each web server and subscribe individually or is this where the distributer comes in, so web servers all send to the 1 distributer and all application subscribe to single distributer and the distributer relays the message ?
I've tried to research this, but having troubles.
Thanks
MrT
First, see the guidance about publishing a message from a web application (or more specifically, about NOT publishing a message from a web application).
Taking all that in consideration, I would recommend your webservers Send() a message to a central event aggregator, which could then Publish() events that other applications (or your web applications) could subscribe to.
More specifically:
MyWeb on Webserver1 (with input queue MyWeb#Webserver1) Send()s a UserChangeMessage to queue WebEventBroker#CentralServer
WebEventBroker app, running on CentralServer, with input queue WebEventBroker#CentralServer, receives UserChangeMessage, and publishes UserChangedEvent
The MyWeb application subscribes to events of type UserChangedEvent, so when it is published, it is received by queues MyWeb#Webserver1 and MyWeb#Webserver2 where both web applications can process the message and take appropriate action.