How to join a group using SignalR
-------------------------In Javascript (ReactJs)---------------------------------
const connection = new signalR.HubConnectionBuilder()
.withUrl("connectionUrl")
.build();
connection.start().then(res => {
connection.invoke("JoinGroup", "groupName") //JoinGroup is C# method name
.catch(err => {
console.log(err);
});
}).catch(err => {
console.log(err);
});;
----------------In C# (.Net Core)-----------------
public class NotificationHub : Hub
{
public Task JoinGroup(string groupName)
{
return Groups.AddToGroupAsync(Context.ConnectionId, groupName);
}
}
You can't. If you could join a group from javascript then anyone may use your code to join any group which breaks security. If you really need to do that - create a method on the server side that takes a group name as parameter and adds the client to the group.
public void JoinGroup(string groupName)
{
this.Groups.Add(this.Context.ConnectionId, groupName);
}
Afterwards, call it from JS like that
eventHub.server.joinGroup("my-awsm-group");
Just in case you come across this question now (like I did), here is an example for how to implement an azure function to support groups.
https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-signalr-service#2x-c-group-management-output-examples