Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join a group using SignalR

Tags:

I am new to using SignalR (started today), Pretty simple to send a message to ALL clients connected, but now I want to just send to a group. I cannot find simple documentation on how to join on the client side. If anyone can help, how can I SIMPLY join a group on the javascript side. Thanks for any help.

public class EventHub : Hub
{
    public void SendNewMedia(MediaInfoViewModel model,Guid eventId)
    {
        Clients.Group(eventId.ToString()).setupmedia(model);
    }
}
//Controller that is sending client new data
var eventHub = GlobalHost.ConnectionManager.GetHubContext<EventHub>();
              var result = eventHub.Clients.Group(eventId.ToString()).setupmedia(eventViewer);

//Finally the javascript. Not sure how to setup just for a group
$(function () {
    var event = $.connection.eventHub;
    event.client.setupmedia = function (newMedia) {

        $('#photolist').prepend('<li><img src="' + newMedia.MediaUrl + '" class="img-polaroid span2"/></li>');
    };
    $.connection.hub.start(function() {
        event.server.create(eventID);//I know this is wrong but not sure how to connect
    }).done(function () {
        alert('conntected. Ready to retrieve data!');
    });
});
like image 645
user516883 Avatar asked Jun 26 '13 00:06

user516883


People also ask

How do I add a user to group SignalR?

Adding and removing users To add or remove users from a group, you call the Add or Remove methods, and pass in the user's connection id and group's name as parameters. You do not need to manually remove a user from a group when the connection ends.

How do I send a message to a group in SignalR?

When user click on send button, the message to be posted to server side using signalR connection hub. Thus whenever you post any message after clicking the join group button, the message will appear to all the clients who has joined the group.

How many groups can SignalR handle?

Yes, it will work since the Microsoft documentation says that you can create 1 group per user/connection, so it depends by user limit and not group limit. So if your SignalR service can handle 40k+ users, it will handle 40k+ groups.

How do I send a SignalR to a specific user?

In SignalR 2.0, this is done by using the inbuilt IPrincipal.Identity.Name , which is the logged in user identifier as set during the ASP.NET authentication. However, you may need to map the connection with the user using a different identifier instead of using the Identity.Name.


1 Answers

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");
like image 189
Nikola Dimitroff Avatar answered Sep 19 '22 16:09

Nikola Dimitroff