2

Suppose I have a ChatHub class defined along with a Broadcast method.

I know how to broadcast messages to all clients if one of them sends a message, however how can I send a message to all the clients from Global.asax ?

In other words how do I get access to ChatHub from another class?

Here's a basic sample:

    public class ChatHub : Hub
    {
      public void Broadcast(String reqMessage)
      {
        Clients.broadcast(reqMessage);
      }
    }

Your help is much appreciated.

MaYaN
  • 5,900
  • 4
  • 48
  • 94

2 Answers2

4

Since SignalR 0.5 you can do this using GlobalHost.ConnectionManager.GetHubContext

Sample

// get gub context
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
// broadcast to all clients in this hub
context.Clients.broadcast("Hello World");

More Information

Community
  • 1
  • 1
dknaack
  • 56,707
  • 23
  • 140
  • 187
1

Use ConnectionManager, as described here: https://github.com/SignalR/SignalR/wiki/Hubs. Btw, your question is most likely a duplicate of this one.

Community
  • 1
  • 1
Piotr Szmyd
  • 13,291
  • 6
  • 42
  • 61