Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling SignalR hub clients from elsewhere in system

I've set up a SignalR hub to communicate between the server and client. The hub server side code is stored in a class called Hooking.cs. What I want is to be able to call a method defined in Hooking.cs to allow me to broadcast messages to any connected clients from anywhere in my application. It seems that a new instance of Hooking.cs is created for every client/server call, so I had hoped that I would be able to use something like

var hooking = new Hooking(); hooking.Test(); 

with the method Test() defined in Hooking.cs such as

public static void Test() {     Clients.test() } 

and with a the client side javascript

var hooking = $.connection.hooking; hooking.test = function() { alert("test worked"); }; $.connection.hub.start() 

Unfortunately it isn't that simple, as Clients is not static, so not accessible from a static method.

Looking through the SignalR source code I came across a method that looked promising, Hubs.Invoke(string hubName, string method, params object[] args), so I would hope I could use something such as Hubs.Invoke("Hooking", "Test") but I can't make it work.

Any help with this would be hugely appreciated

like image 801
Jordan Wallwork Avatar asked Oct 03 '11 11:10

Jordan Wallwork


People also ask

Is SignalR bidirectional?

ASP.NET SignalR is a new library for ASP.NET developers that makes developing real-time web functionality easy. SignalR allows bi-directional communication between server and client. Servers can now push content to connected clients instantly as it becomes available.

Is SignalR two way communication?

SignalR the newest member of the ASP.NET family, that enables developers to write incredibly simple real-time, two-way websites and apps. Two-way communication means that both the server and client share an open channel in which they can initiate contact with each other.

How do I send a message to a specific signal SignalR?

SignalR allows messages to be sent to a particular client connection, all connections associated with a specific user, as well as to named groups of connections. => await Clients. User(userId).


2 Answers

This is the correct way for SignalR 2.x:

var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>(); context.Clients.All.addMessage(message); 

Basically, you can use the dependency resolver for the current host to resolve the IConnectionManager interface which allows you to get ahold of the context object for a hub.

Further information can be found in the official documentation.

like image 56
Paolo Moretti Avatar answered Oct 21 '22 01:10

Paolo Moretti


Hub.GetClients has disappeared in version 0.4.0.

From the wiki you can now use:

IConnectionManager connectionManager = AspNetHost.DependencyResolver.Resolve<IConnectionManager>(); dynamic clients = connectionManager.GetClients<MyHub>(); 
like image 44
Greg Ennis Avatar answered Oct 21 '22 02:10

Greg Ennis