9

I am trying to implement Azure SignalR Service to facilitate two way messaging between a desktop, asp.net-core and a xamarin.ios apps.

I have created a Hub as per Microsoft's documentation here: https://learn.microsoft.com/en-us/aspnet/core/signalr/hubs?view=aspnetcore-2.2

Hub:

public class ChatHub : Hub
{
    public Task SendMessage(string user, string message)
    {
        return Clients.All.SendAsync("ReceiveMessage", user, message);
    }
}

I add users's connections to a group when they connect to the hub as shown here: https://learn.microsoft.com/en-us/aspnet/core/signalr/groups?view=aspnetcore-2.2

Add to Group:

public async Task AddToGroup(string groupName)
{
    await Groups.AddToGroupAsync(Context.ConnectionId, groupName);

    await Clients.Group(groupName).SendAsync("Send", $"{Context.ConnectionId} has joined the group {groupName}.");
}

public async Task RemoveFromGroup(string groupName)
{
    await Groups.RemoveFromGroupAsync(Context.ConnectionId, groupName);

    await Clients.Group(groupName).SendAsync("Send", $"{Context.ConnectionId} has left the group {groupName}.");
}

When a message is sent it has a group name as a parameter, I would like to check if the provided group has any connections before sending the message to any registered clients, if there are no connections, I would like to send a push notification (already have working code for the push notification)

Send Message with Notification Fallback:

public class ChatHub : Hub
{
    public Task SendMessage(string groupName, string user, string message)
    {
        var group = Clients.Group(groupName);

        // todo: how to check if we have any open connections in this group?
        if(group.Conections.Count > 0)
        {
            return group.SendAsync("ReceiveMessage", user, message);
        }
        else
        {
            // todo: run code to send push notification or anything else you might want to do
        }
    }
}

Problem: I don't see any way to check for the number of connections currently in a group via the available api (correct me if I'm wrong)

I see the group coming back as Microsoft.AspNetCore.SignalR.Internal.GroupProxy<ChatHub> at runtime with no public methods. Internal private variables do include _groupName and _lifeTimeManager and within lifetime manager there is a _clientConnectionManager which i can see has client connections when they are connected but I have no access to any of these, I'm using the Microsoft.Azure.SignalR (1.0.4) Nuget package. Does anyone know if what I'm trying to do possible with this SDK and if so how I can do this?

Dmitry Samuylov
  • 1,554
  • 2
  • 14
  • 37
  • looks like a REST Api endpoint was just recently added to be able to do such a check as described here https://github.com/Azure/azure-signalr/blob/dev/docs/swagger/v1.md#get-check-if-there-are-any-client-connections-inside-the-given-group – Dmitry Samuylov Aug 24 '20 at 21:35
  • Probably need to do a mapping exercise. https://learn.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/mapping-users-to-connections – Netferret Jan 23 '23 at 11:58

0 Answers0