How to use SignalR hub instance outside of the hubpipleline

signalrsignalr-hub

I am using SignalR to broadcast messages to all my clients. I need to trigger the broadcasting outside of my hub class i.e. something like below:

var broadcast = new chatHub();
broadcast.Send("Admin","stop the chat");

I am getting error message as:

Using a Hub instance not created by the HubPipeline is unsupported.

Best Answer

You need to use GetHubContext:

var context = GlobalHost.ConnectionManager.GetHubContext<chatHub>();
context.Clients.All.Send("Admin", "stop the chat");

This is described in more detail at http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-server#callfromoutsidehub.

Related Topic