Base solution for your next web application
Open Closed

Send signalR notification to clients #2832


User avatar
0
bilalhaidar created

Hello, In my app I am caching locally some reference data using localForage.

Ive implement that as a JS service where Angular controllers use it. When data is changed, different controllers fire different events that got handled by the service to refresh data and bring in to cache new updated data from server.

Now, all this happens on the same machine. What if, I want also to refresh data stored in cache on other machines? I need a way from the server-side that when an entity changes, to send a signalR notification so that other cllients could refresh their data.

Can you guide me to a sample code on how to do so with Abp?

Thanks


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    First you can use OnlineClientManager's GetAllClients to get all online clients, see <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/src/Abp/RealTime/OnlineClientManager.cs#L92">https://github.com/aspnetboilerplate/as ... ger.cs#L92</a>.

    Then, you can use such method to trigger client side function

    foreach (var client in clients)
    {
        var signalRClient = GetSignalRClientOrNull(client);
        if (signalRClient == null)
        {
            continue;
        }
    
        signalRClient.refreshCache();
    }
    

    But using this approach might not be effective when you have many users online.

    Thanks.

  • User Avatar
    0
    bilalhaidar created

    So, when I call refreshCache() that would translate to client side call? Can I see a concrete sample for some running code to just get the idea of how it works?

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    This is an example usage in AspNet Zero <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero/blob/master/src/MyCompanyName.AbpZeroTemplate.Web/Chat/SignalR/SignalRChatCommunicator.cs">https://github.com/aspnetzero/aspnet-ze ... nicator.cs</a>.

    You can search for client side methods to see their codes as well.