Lucas
Lucas

Reputation: 376

Signalr not sending to specific users

I have inside my hub a method that is used to send to specific clients that a ticket was updated. However I want it to send it to the server inside the controller and to specific users, you can see how I am doing it below:

I am using the following method to send to the specific clients:

public void NotifyUpdatedTicket(Ticket ticket, string note, params string[] aliases)
{
    foreach (string a in aliases)
    {
        foreach (string connectionId in _allConnections.GetConnections(a))
        {
            Clients.Client(connectionId).notifyUpdatedTicket(ticket, note);
        }
    }
}

The _allConnections is the same as the class in http://www.asp.net/signalr/overview/guide-to-the-api/mapping-users-to-connections#inmemory

However instead of passing 1 user I passed a list of aliases and then get their connections and send to their connection ids.

However it does not seem to be sending properly it sends it to all clients. Here is the command I am using to call the method in my controller:

GlobalHost.ConnectionManager.GetHubContext<TicketHub>().Clients.All.notifyUpdatedTicket(viewModel.Current, viewModel.NewNote, viewModel.Current.OpenedBy, viewModel.Current.AssignedTo);

When I send the above command with the specific OpenedBy and AssignedTo, it sends it all users when it shouldn't.

Am I missing something from calling it in the controller that sends it only to specific users or something?

Even when I put in the persons alias directly into the hub method it still sends to everyone. I assume this has to do with the controller method.

Upvotes: 3

Views: 858

Answers (1)

Lars H&#246;ppner
Lars H&#246;ppner

Reputation: 18402

This

Clients.All.notifyUpdatedTicket

will send a message to all clients. It won't call your method with this signature (I assume that's what you're trying to do):

public void NotifyUpdatedTicket(Ticket ticket, string note, params string[] aliases)

I assume this is a method you added to your hub class. It's not a good idea to put methods in a hub class if their sole purpose is to be called from the server-side.

Anyway, if you do, you need to use GetHubContext<TicketHub>() from within that method even if it is part of the hub class, because the hub context is set up as part of the hub pipeline when a request to that hub arrives. Without that, the hub is just a regular class and you don't have access to dynamic members like Clients.

So, all in all, it seems like you're confused about some fundamentals. I'd suggest moving NotifyUpdatedTicket to a separate class that also caches the relevant hub context, then call that method when needed (e.g. from a MVC controller method if that is your intent).

Upvotes: 1

Related Questions