SignalR + sending a message to the hub through the action method

I use the SignalR hub function ( https://github.com/SignalR/SignalR ) to post messages to all subscribers:

public class NewsFeedHub : Hub public void Send(string channel, string content) { Clients[channel].addMessage(content); } 

This works fine when calling Submit through Javascript, but I would also like the web application to post messages (from within the ASP.NET MVC action method). I already tried to create an instance of a new ob object NewsFeedHub and call the submit method, but this leads to an error (since the base β€œconnection” of the hub has not been established). Is there a way to use a hub without connecting?

+54
asp.net-mvc message action signalr signalr-hub
Sep 25 2018-11-22T00:
source share
4 answers

Please note that the SignalR interface has changed several times since this question was asked. It is likely that some answers will become obsolete. This does not mean that they should be skipped, as they were correct at the time of writing.

There is another updated answer for this, as shown in the SignalR Wiki

FROM#

 Public ActionResult MyControllerMethod() { var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>(); context.Clients.All.methodInJavascript("hello world"); // or context.Clients.Group("groupname").methodInJavascript("hello world"); } 

vb.net

 Public Function MyControllerMethod() As ActionResult Dim context = GlobalHost.ConnectionManager.GetHubContext(Of MyHub)() context.Clients.All.methodInJavascript("hello world") '' or context.Clients.Group("groupname").methodInJavascript("hello world") End Function 

Update

This code has been updated. For changes, run http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-server .

If you are using a DI container

If you use a DI container to connect hubs, get the IConnectionManager from your container and call GetHubContext in this connection manager.

+79
Jun 19 2018-12-12T00:
source share
β€” -

February 2017, a short and simple solution

To solve this, I usually design my hubs as follows:

 public class NewsFeedHub : Hub { private static IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<NewsFeedHub>(); // Call this from JS: hub.client.send(channel, content) public void Send(string channel, string content) { Clients.Group(channel).addMessage(content); } // Call this from C#: NewsFeedHub.Static_Send(channel, content) public static void Static_Send(string channel, string content) { hubContext.Clients.Group(channel).addMessage(content); } } 

So it's easy to call from Javascript and from inside code. These two methods result in the same event on the client.

+35
Jun 26 '15 at 0:53
source share

updated : this answer is also old! It appears that the public SignalR API is in a constant stream. Tim B James has had a new, correct use of the API since July 2012.

Mike's currently accepted answer is old and no longer works with the latest version of SignalR.

Here's an updated version showing how to send a message to a hub from an MVC action:

 public ActionResult MyControllerMethod() { // Important: .Resolve is an extension method inside SignalR.Infrastructure namespace. var connectionManager = AspNetHost.DependencyResolver.Resolve<IConnectionManager>(); var clients = connectionManager.GetClients<MyHub>(); // Broadcast to all clients. clients.MethodOnTheJavascript("Good news!"); // Broadcast only to clients in a group. clients["someGroupName"].MethodOnTheJavascript("Hello, some group!"); // Broadcast only to a particular client. clients["someConnectionId"].MethodOnTheJavascript("Hello, particular client!"); } 
+8
Apr 04 2018-12-12T00:
source share

After the response from DDan, you can create an overloaded static method and save the general logic in one way - I use this template

 public class ChatHub : Hub { private static IHubConnectionContext<dynamic> GetClients(ChatHub chatHub) { if (chatHub == null) return GlobalHost.ConnectionManager.GetHubContext<ChatHub>().Clients; else return chatHub.Clients; } // Call from JavaScript public void Send(string message) { Send(message, this); } // Call from C# eg: Hubs.ChatHub.Send(message, null); public static void Send(string message, ChatHub chatHub) { IHubConnectionContext<dynamic> clients = GetClients(chatHub); // common logic goes here clients.All.receiveSend(message); } } 

Then from your controller you can use

 Hubs.ChatHub.Send(arg1, null); 
+1
May 16 '17 at 9:52
source share



All Articles