Base solution for your next web application
Open Closed

implementation of IOnlineClient in aspnetboilerplate #9240


User avatar
0
BobIngham created

I have a SignalR hub in my solution for devices. I'm having a problem finding which devices are connected. I implemented an ITypedCache (GetNcDeviceOnlineCache) to handle connections which I thought would do the job. But the cache does not allow me to get a list of all connected devices when I want to display a devices grid. For now I persist the value "IsOnline" in my database on connection and disconnection to the hub. At the moment this works but if SignalR and the database get out of sync (and I figure they will on account of both being agnostic of the other) I have a problem. I am worried about the supportability of this, I just don't believe it will be 100% accurate as I scale up.

For online users the solution is simple, there is no database persistence and I initially show connected users using IOnlineClient which allows me to get a list of all uses connected to the hub for each individual tenant. I then subscribe to, and override abp's version of SignalR's OnConnectedAsync() and OnDisconnectedAsync(Exception exception) methods. The server code is below:

       public List<IOnlineClient> GetOnlineUsers()
        {
            return this.OnlineClientManager.GetAllClients().Where(c => c.TenantId == AbpSession.TenantId).ToList();
        }

        public override async Task OnConnectedAsync()
        {
            await base.OnConnectedAsync();
            await Clients.All.SendAsync("userConnected", Convert.ToInt64(Context.UserIdentifier));
            Logger.Debug("A client connected to NcChatHub: " + Context.ConnectionId);
        }

        public override async Task OnDisconnectedAsync(Exception exception)
        {
            await base.OnDisconnectedAsync(exception);
            await Clients.All.SendAsync("userDisconnected", Convert.ToInt64(Context.UserIdentifier));
            Logger.Debug("A client connected to NcChatHub: " + Context.ConnectionId);
        }

The key piece of code here is this.OnlineClientManager.GetAllClients().Where(c => c.TenantId == AbpSession.TenantId).ToList(); I have searched the github code for abpboilerplate and cannot find the implementation of OnlineClientManager.

I really appreciate the fact that you guys are the wizards and I am very much an apprentice. But can anyone tell me how to get a list of connected devices without having to persist the value in the database? i.e. something like the elusive implementation of OnlineClientManager?

Your attention, advice and direction would be most appreciated.


2 Answer(s)
  • User Avatar
    0
    musa.demir created

    Hi @bobingham

    OnlineClientManager comes from aspnet boilerplate. https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp/RealTime/OnlineClientManager.cs

    It uses IOnlineClientStore to store clients. Here is it's implementation: https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp/RealTime/InMemoryOnlineClientStore.cs

    It uses ConcurrentDictionary to store all connected clients not database.

    What is the problem of this.OnlineClientManager.GetAllClients()? Isn't it work ?

  • User Avatar
    0
    BobIngham created

    Hi @demirmursa,

    There is no problem with this.OnlineClientManager.GetAllClients(), it works perfectly. Thanks for the links, I was able to build an OnlineDeviceStore and OnlineDeviceManager along with all the supporting code (and there's a lot of it) to implement something similar for online devices. That now allows me track devices connected to the system using IP addresses, geolocation and user information. Thanks a lot!