3

We're having some issues where the Azure hosted SignalR frequently does not receive client-sent messages. When testing locally with a self-hosted SignalR, we never have any issues.

What we want to achieve is the following:

  • Whenever a users opens a 'case'-page, we want to add that user to this specific case group.
  • In the front-end (React) we do the following:
// When the 'Case'-page gets rendered

useEffect(() => {
  if (caseId && currentTeam?.id) {
    dispatch(joinCaseGroup(caseId, currentTeam.id));
  }

  return () => {
    const cleanup = () => {
      dispatch(leaveCaseGroup(caseId));
    };
    cleanup();
  };
}, [caseId, currentTeam.id, dispatch]);

// Function triggered by `joinCaseGroup`
connection.invoke('JoinCaseGroup', caseId, teamId))
  • This gets handled in our Hub:
public class EventsClientHub : Hub
{
    /// <summary>
    /// Contains all the users that are in a case group
    /// </summary>
    private static readonly Dictionary<Guid, List<string>> UserCaseGroups = new();

    public async Task JoinCaseGroup(string caseId, string team)
    {
        // Here we check if the team has access to the case
        if (Guid.TryParse(caseId, out var parsedCaseId) && Guid.TryParse(team, out var teamId) && await _caseQueryHandler.TeamHasAccessToCase(teamId, parsedCaseId))
        { 
            // Remove from previous case groups, because a user can only be in 1 case group
            await RemoveUserFromCaseGroups();

            await AddUserToCaseGroup(parsedCaseId);
        }
    }

    private async Task AddUserToCaseGroup(Guid caseId)
    {
        // Keep track of all users in a case
        string userId = Context.User!.FindFirst("sub")!.Value;
        if (!UserCaseGroups.ContainsKey(caseId))
        {
            UserCaseGroups.TryAdd(caseId, new List<string>());
        }
        UserCaseGroups[caseId].Add(userId);

        // Add the user to the case group
        await Groups.AddToGroupAsync(Context.ConnectionId, $"case_{caseId}");
    }
}

Now, when debugging this, we see that the client does send the request and gets a response: Console logs in the client

But when we check the live trace logs on Azure, we only see that the user gets added to his team group, but not to the case group: enter image description here

The weird thing is, when we refresh a couple of times, the user sometimes does get added to the group and can receive live updates.

Any idea what the issue could be here?

Thanks in advance!

K. L.
  • 231
  • 2
  • 12
  • 1
    Probably a race condition. I see it all of the time with my hub. If you don't have to iterate through the groups, use a SignalR group. If you have to iterate, use a concurrent dictionary instead of dictionary because it's thread-safe. Well, the key is thread-safe, not the value. – GH DevOps Sep 26 '22 at 12:58

0 Answers0