azure-signalr
azure-signalr copied to clipboard
Is there a way to check if a group has any open connections?
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.
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 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?
We maintain internally a list of client connections, however:
- what groups these connections belong to is not stored there but maintained in ASRS side.
- 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 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?
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 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?
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 No for now, we are considering to enhance serverless, but it not in our plan yet.
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.
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.
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!
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.
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!
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.
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
andserverless
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.
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?
These APIs are now added.