How to use SignalR hub instance outside of the hubpipleline

Nitin Agrawal picture Nitin Agrawal · Feb 28, 2013 · Viewed 36.7k times · Source

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.

Answer

halter73 picture halter73 · Feb 28, 2013

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.