dotnet / dotnet/AspNetCore.Docs
SignalR send to users and groups in one call
- Dominant language
- C#
- Stars
- 13.1k
- Forks
- 24.6k
- Avg merge
- 1d 3h
- Merged PRs (30d)
- 97
Description
### Is your feature request related to a problem? Please describe.
I am trying to send a SignalR Message to a List of Groups and Users. For example, all Admins and Managers should get this message as well as a certain user who triggered an API call in the program.
To do this we are using the following code.
```
[CapSubscribe(EventNames.SignalRUpdateEventName)]
public async Task Handle(SignalREvent @event)
{
//Adds a message ID to each message to give the client the option to discard duplicates
@event.JsonPayload.Add("MessageId", Guid.NewGuid().ToString());
if (!@event.Groups.IsNullOrEmpty())
{
await _hubContext.Clients.Groups(@event.Groups).SendAsync(@event.MethodName, new object[] {@event.JsonPayload});
}
if (!@event.Users.IsNullOrEmpty())
{
await _hubContext.Clients.Users(@event.Users)
.SendAsync(@event.MethodName, new object[] {@event.JsonPayload});
}
}
```
This results in multiple Messages being sent by SignalR and our client receiving multiple messages if the user is, for example, the one who initiated the call and is part of the group Admin and Manager.
### Describe the solution you'd like
My Solution would be that I could call something like this:
`
_hubContext.Clients.Groups(@event.Groups).Users(@event.Users).SendAsync(@event.MethodName, new object[] {@event.JsonPayload});
`
And in the background singalR would aggregate all the connection Ids so that only one message per connection would be sent.
Contributor guide
Assessment
This issue has not been assessed yet.