'Cannot send message to group in SignalR
So I have the below code, which I am trying to make it send the message when I join the group, but it is not working
public async Task JoinGroup()
{
await Groups.AddToGroupAsync(Context.ConnectionId, "Hello");
await Clients.Group("Hello").SendAsync("SEND", $"{Context.ConnectionId} has been added");
}
When I try it like this
public async Task JoinGroup()
{
await Groups.AddToGroupAsync(Context.ConnectionId, "Hello");
await Clients.All.SendAsync("SEND", $"{Context.ConnectionId} has been added");
}
It works, but it sends the message to all the connected clients, which I do not want to happen.
I got the first code block from the docs. https://docs.microsoft.com/en-us/aspnet/core/signalr/groups?view=aspnetcore-6.0
Anyone have any idea why it is not working?
Edit: I have a listener in the front
connection.on("SEND", message => alert(message))
Solution 1:[1]
For sending message in group, should use .Group instead .All.
await Clients.Group.SendAsync("SEND", $"{Context.ConnectionId} has been added");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hassan Rasouli |
