I hawe two type of clients connecting my signalR server (ASP.NET Core). Some of them are senders, and some of them are receivers. I need to route messages from senders to th
If I understood well, you want to defer SignalR message sending by using something like a synchronized call in some IHostedService. Here is what I managed to achieve so far.
So here the Queues
class:
public class Queues {
public ConcurrentQueue>> MessagesQueue { get; set; }
}
ConnectionId
of the caller so a call can get an answer later. SendMessage
enqueue the necessary action delegate to perform a call against a hub instance as a parameter.As an example SendMessage
will trigger an answer back to the caller, and BroadcastMessage
will send a message to all clients.
Using a captured hub instance instead would lead to an exception here because the hub will be disposed, quickly. That's why it would be injected later in another class. Have a look on SendMessage_BAD
Here is the MyHub
class and the corresponding IMyEvents
interface:
public interface IMyEvents {
void ReceiveMessage(string myMessage);
}
public class MyHub : Hub {
Queues queues;
public MyHub(Queues queues) {
this.queues = queues;
}
public void SendMessage(string message) {
var callerId = Context.ConnectionId;
queues.MessagesQueue.Enqueue(hub => hub.Clients.Client(callerId).ReceiveMessage(message));
}
// This will crash
public void SendMessage_BAD(string message) {
this.callerId = Context.ConnectionId;
queues.MessagesQueue.Enqueue(_ => this.Clients.Client(callerId).ReceiveMessage(message));
}
public void BroadcastMessage(string message) {
queues.MessagesQueue.Enqueue(hub => hub.Clients.All.ReceiveMessage(message));
}
}
IHostedService
but it is does not appear here). This class has to be injected as a singleton.Here the DeferredMessageSender
class:
public class DeferredMessageSender {
Queues queues;
IHubContext hub;
public DeferredMessageSender(Queues queues, IHubContext hub) {
this.queues = queues;
this.hub = hub;
}
public void GlobalSend() {
while(queues.MessagesQueue.TryDequeue(out var evt)) {
evt.Invoke(hub);
}
}
}
Hope it helps.