Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR + posting a message to a Hub via an action method

I am using the hub- feature of SignalR (https://github.com/SignalR/SignalR) to publish messages to all subscribed clients:

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

This works fine when calling "Send" via Javascript, but I would also like the web application to publish messages (from within an ASP.NET MVC action method). I already tried instantiating a new object ob NewsFeedHub and calling the Send method, but this results in an error (as the underlying "Connection" of the Hub is not set). Is there a way to use the Hub without a connection?

like image 433
ollifant Avatar asked Sep 25 '11 22:09

ollifant


People also ask

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).

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.

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.

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.


1 Answers

Please note that the SignalR API has changed multiple times since this question was asked. There is a chance that some answers will become out of date. This does not mean that they should be down-voted as they were correct at the time of writing

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

c#

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. Follow http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-server for changes.

If you are using DI container

If you are using a DI container to wire up your hubs, get IConnectionManager from your container, and call GetHubContext on that connectionManager.

like image 109
Tim B James Avatar answered Sep 19 '22 17:09

Tim B James