rross
rross

Reputation: 2276

calling SignalR hub from WebAPI controller

I'm using MVC 5, Signal R 2.0.1, and WebAPI 2, and have a simple hub set up named ExportHub

public class ExportHub : Hub
{
    public void Send(string name, string message)
    {
        // Call the addNewMessageToPage method to update clients.
        Clients.All.addNewMessageToPage(name, message);
    }
}

I'm attempting to call this from WebAPI so the UI can be updated.

var hubContext = GlobalHost.ConnectionManager.GetHubContext<ExportHub>();

But within hubContext I don't see any reference to Send or addNewMessageToPage. How do I gain access to the methods within the hub?

Upvotes: 1

Views: 3844

Answers (2)

tzm
tzm

Reputation: 598

Try to make your Send method static, and then call hubContext.Send(string, string)

Upvotes: 0

kkocabiyik
kkocabiyik

Reputation: 4406

You will not be able to call any methods of specified hub. GetHubContext only returns a IHubContext of specified T Hub. Instead of calling Hub methods you should directly invoke method from your web api method to client like

Clients.All.addNewMessageToPage(name, message);

Upvotes: 2

Related Questions