azure-signalr icon indicating copy to clipboard operation
azure-signalr copied to clipboard

Is there a way to check if a group has any open connections?

Open dmitrysamuylovpharo opened this issue 6 years ago • 16 comments

I would like to be able to check if a group I am about to send a message to has any open connections and possibly how many. I see this information is available internally on the client hub in the lifetime manager > client connection manager but i don't see any of this exposed in any way. The reason I want to perform this check is to decide if I should send a SignalR message to the connected clients or send a push notifications (I'm dealing with iOS mobile clients). Currently I don't see any way of doing something like that.

dmitrysamuylovpharo avatar Dec 17 '18 23:12 dmitrysamuylovpharo

No, there is not. The internal client manager does not maintain group info. Groups are maintained in the Service side through Redis pub-sub channels currently.

You may need to store/maintain the info by your own. A globally available storage is needed if you have multiple app servers.

vicancy avatar Dec 21 '18 01:12 vicancy

@vicancy but as I said, I can inspect the private properties of the client hub and under lifetime manager > client connection manager i see a list of connections and as my client connect or disconnects this list of connections is in fact updated, it seems to me that this list of connections would just need to be exposed publicly? Am I missing something? screen shot 2018-12-21 at 8 39 14 am

dmitrysamuylovpharo avatar Dec 21 '18 13:12 dmitrysamuylovpharo

We maintain internally a list of client connections, however:

  1. what groups these connections belong to is not stored there but maintained in ASRS side.
  2. these connections in client connection manager are local info, which means they are client connections connected to this app server. If you have several app server instances, you still need a globally available storage to maintain these connections. This is actually what ASRS does, ASRS helps maintain the info when server scales.

vicancy avatar Dec 24 '18 02:12 vicancy

@vicancy Ok, point taken, question still stands, how to get this information, i am using ASRS (if by that you mean Azure SignalR Service), if it manages all this information for me, how can i get this info back? What good is it's managing this info for me if I can't retrieve it and do something with it? Wouldn't it make sense to have this functionality available and exposed through the Azure SignalR client library? What about smaller deployments that do in fact only need 1 app server (which is the case for me), why not expose these connections and just document it accordingly that this is only local to the current server, this seems like useful information don't you think?

In any case, do you know any resource showing how to retrieve this info from ASRS?

dmitrysamuylovpharo avatar Dec 26 '18 13:12 dmitrysamuylovpharo

So back to the original question, "Is there a way to check if a group has any open connections", no there is no native way to do that.

The Azure SignalR SDK follows the SignalR protocol, so in general the best practices are the same as self-host SignalR. As similar to the self-host SignalR, there is no API for getting a group membership list or a list of groups. SignalR sends messages to clients and groups based on a pub/sub model, and the server does not maintain lists of groups or group memberships.

But you can add some hook into hub's OnConnected to maintain your custom info when the client connects in. When you have 1 app server, store the info in memory should be enough.

public class Chat : Hub
{
    private IConnectionManager _manager;
    public Chat(IConnectionManager manager)
    {
        _manager = manager;
    }

    public override Task OnConnectedAsync()
    {
       // Add connectionId and any other info you want to your connectionManager
        _manager.Add(Context.ConnectionId, Context.User, Context.GetHttpContext());
    }

    public override Task OnDisconnectedAsync(Exception exception)
    {
       _manager.Remove(Context.ConnectionId);
    }
   ...
}

vicancy avatar Dec 28 '18 03:12 vicancy

@vicancy I've seen basic examples like this, thank you, the best example i have found so far is this: https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/mapping-users-to-connections#permanent-external-storage but really, this is very basic and common functionality anyone would want to make use of using signalR I believe and it would really be a huge benefit for it to be built into the SDK. Is there a feature in the works for this type of functionality or can i submit a feature suggestion/request somewhere?

dmitrysamuylovpharo avatar Dec 28 '18 14:12 dmitrysamuylovpharo

Is there any way to get the OnConnected / Disconnected in a serverless ASRS implementation? (Would need some sort of post back I assume on the REST surface?)

bbowman avatar Mar 20 '19 03:03 bbowman

@bbowman No for now, we are considering to enhance serverless, but it not in our plan yet.

vwxyzh avatar Mar 20 '19 05:03 vwxyzh

So if I understand correctly in a simple scenario when there is only one SignalR server. The internal lifetime manager already maintains a collection of connected clients. But the suggested way is to replicate that collection and maintain it for myself just because the API does not provide a readonly way of querying it.

I think it should be opened as a readonly collection because for debug purposes it is just way more convenient than writing a whole infrastructure in order to create something that is already there.

danielleiszen avatar Mar 26 '20 21:03 danielleiszen

We plan to add an existence check in the service REST API to check "if a group has any open connections", according to the deployment schedule it would be available in late April, and ManagementAPI will be updated accordingly after the service is ready.

vicancy avatar Mar 27 '20 09:03 vicancy

We plan to add an existence check in the service REST API to check "if a group has any open connections", according to the deployment schedule it would be available in late April, and ManagementAPI will be updated accordingly after the service is ready.

That's a good starting point. Thank you!

danielleiszen avatar Mar 27 '20 16:03 danielleiszen

REST API for this check is added https://github.com/Azure/azure-signalr/blob/dev/docs/swagger/v1.md#get-check-if-there-are-any-client-connections-inside-the-given-group, Management API update is still in progress.

vicancy avatar Aug 24 '20 05:08 vicancy

Hi @vicancy - will said methods be added to the the SignalR .NET Core SDK?

I am using the SignalR service in a 'serverful' environment and would need to check for the existence of any connections for a user, for the exact same reason as the topic's creator. Is the API you linked to only for serverless implementations, or can I call into it even when using a server, until the API is added to the SDK?

Thanks!

Tommigun1980 avatar Aug 26 '20 19:08 Tommigun1980

will said methods be added to the the SignalR .NET Core SDK

No, it will be in Management SDK

can I call into it even when using a server

Yes you can. REST API/management SDK/function binding can be used in both modes. The key difference between default and serverless mode is whether you have hub servers and how client connections are routed.

vicancy avatar Aug 27 '20 07:08 vicancy

will said methods be added to the the SignalR .NET Core SDK

No, it will be in Management SDK

can I call into it even when using a server

Yes you can. REST API/management SDK/function binding can be used in both modes. The key difference between default and serverless mode is whether you have hub servers and how client connections are routed.

Thank you. Any reason why the .NET Core API is lacking most of the REST API calls? It would be convenient if the .NET Core API contained more than a handful of methods.

Thanks.

Tommigun1980 avatar Aug 27 '20 07:08 Tommigun1980

It seems that these APIs (like check if any connections in group, check connection exists) are still missing in Management SDK. Are you still planning to add them?

standsed avatar Mar 02 '21 07:03 standsed

These APIs are now added.

vicancy avatar Sep 11 '23 06:09 vicancy