7

We currently use SignalR to receive realtime messages from our backend on our UI Client. The UI client receives messages while it is online and connected to SignalR and misses the messages while it was disconnected (example: user closed the page and SignalR disconnected the client). However, now we need to show all messages to the user, including the ones that were sent by SignalR when the UI Client was offline. Can SignalR support this scenario? The requirement is similar to a persistent queue for UI client mesages, but we use SignalR to broadcast messages to all clients.

Dhruv Joshi
  • 123
  • 1
  • 6

1 Answers1

3

SignalR does not support this scenario, you need to do it on your own. You need to store the the messages and implement a hub method that will send the pending data to the connected client. So what you need to do is:

  • Save the data on some volatile storage with a readby option, so you can see data that was already send to the client and delete it.
  • Hub method that will sent data to the client and the client responds that received the data.
  • Hub method that will send all data that was not sent by hub when client was disconnected.

Code example, on the client side, connect and get previous data:

/**
* Connect signalR and get previous data
*/
private async connectSignalR() {
  await this.hubMessageConnection.start()
    .then(() => {
      // Register application
      this.GetDataForThisClientAsync();
    }).catch(() => {
      this.onError.emit(WidgetStateEnum.connectError);
    });
}

And hub method to get data:

public async Task<OperationResult> GetNotificationsAsync(Groups groups)
{
    IList<MyData> data = await this.DataManager.GetDataForThisClientAsync(groups).ConfigureAwait(false);

    if (data.Count != 0)
    {
        // Send the notifications

        foreach (MyData data in datas)
        {
            await this.BroadcastDataToCallerAsync(data).ConfigureAwait(false);
        }
    }

    return OperationResult.Success();
}
Kiril1512
  • 3,231
  • 3
  • 16
  • 41
  • so how would you deal with your client disconnecting, but your server not knowing yet? I.E. your client disconnects (your server will wait for another few minutes before marking it as disconnected), your server sends a message without realizing your client is already gone. Now your server is happy because the message has been sent, but the client will never receive that message even after it comes back online – Jeremias Rößner Mar 20 '23 at 01:48
  • @JeremiasRößner you can check how to do that here: https://stackoverflow.com/questions/22197129/how-to-do-guaranteed-message-delivery-with-signalr – Kiril1512 Mar 21 '23 at 00:11