0
votes

I am using Redis backplane with SignalR. I have a problem but I couldn't find anything about this.

SignalR clients are Windows Forms application.

I'm using OnConnected and OnDisconnected events for handle user connections.

I have a POCO class for connections with ConnectionId property and some aother informations(UserName etc.).

In OnConnected event i insert new record to Redis with ConnectionId and OnDisconnected event remove the entry from Redis. I dont'do anything in OnReconnected event.

But now there are too many records in Redis with same user names and different connectionid s.

Can i check the connection is alive with ConnectionId? If it's possible i want to delete died connections in Redis.

Do you have any idea about this same record problem?

Edit: First, i have a mistake about OnConnected event. I don't use OnConnected event. WinForms client applications fire Join event on the Hub, after SignalR has been connected.

This is UserData POCO Class:

public class UserData
{
    public string Id { get; set; }
    public DateTime Connected { get; set; }
    public string UserName { get; set; }
    public string MachineName { get; set; }
    public string ConnectionId { get; set; }
}

Join Method :

public void Join(UserData userData)
{
  _trackUser.Add(userData);
}

I'm injecting _trackuser object to Hub class in Startup via

GlobalHost.DependencyResolver.Register

OnDisconnected Event :

    public override Task OnDisconnected(bool b)
    {
       _trackUser.Remove(Context.ConnectionId);
        return base.OnDisconnected(b);
    }

TrackUser Add Method :

    public void Add(UserData userData)
    {
        IRedisClient redisClient = Global.RedisClientManager.GetClient();
        using (redisClient)
        {
            IRedisTypedClient<UserData> redisTypedClient = redisClient.As<UserData>();
            UserData store = redisTypedClient.Store(userData);
        }
    }

TrackUser Remove Method :

    public void Remove(string connectionId)
    {
        IRedisClient redisClient = Global.RedisClientManager.GetClient();
        using (redisClient)
        {
            IRedisTypedClient<UserData> redisTypedClient = redisClient.As<UserData>();
            bool removeEntry = redisTypedClient.RemoveEntry("urn:userdata:"+connectionId);
        }
    }
1
u might add some code ?Muds

1 Answers

1
votes

Keep the user connection status in a separate Database table like User table having foreign key in Connections table. Set the connection-id status in connections table from your Hub for OnConnected and OnDisconnected events.This helps to track the user connection status.

Redis backplane is purely for scaling out your application to support users connecting to different nodes over load balance environment.