Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SignalR hub instance outside of the hubpipleline

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.

like image 334
Nitin Agrawal Avatar asked Feb 28 '13 05:02

Nitin Agrawal


People also ask

Is SignalR asynchronous?

SignalR is an asynchronous signaling library for ASP.NET that our team is working on to help build real-time multi-user web application.

How does SignalR hub work?

What is a SignalR hub. The SignalR Hubs API enables you to call methods on connected clients from the server. In the server code, you define methods that are called by client. In the client code, you define methods that are called from the server.

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.


1 Answers

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.

like image 133
halter73 Avatar answered Sep 24 '22 10:09

halter73