Raffaeu
Raffaeu

Reputation: 7003

SignalR Web Application receives messages from Console Hub

I have setup a small project composed by a Console Application (my messages Hub) and a Web Application (my messages receiver). The idea is that the Console Application listen to a RabbitMQ queue and every time a message is received, it broadcast the message to all SignalR Clients connected.

I initialize the Console App in this way:

// start Mass Transit Bus
var busControl = BuildBus();
busControl.Start();

// Start SignalR
string url = "http://localhost:9090";
using (WebApp.Start(url))
{
    Console.WriteLine("SignalR Server running on {0}", url);
    Console.ReadLine();
}

Then I have my Startup class and my Hub Class as following:

class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseCors(CorsOptions.AllowAll);
        app.MapSignalR();
    }
}
public class MyHub : Hub
{
    public void Send(string name, string message)
    {
        Clients.All.addMessage(name, message);
    }
}

Now is where I get lost.

Question 1 I want from my Web Application to receive messages, so I initialize the proxy and then?

<script type="text/javascript">
        $(function () {
        //Set the hubs URL for the connection
            $.connection.hub.url = "http://localhost:9090/signalr";

            // Declare a proxy to reference the hub.
            var chat = $.connection.myHub;

            // Declare a Message handler

        });
</script>

Question 2 From the Console Application, how do I broadcast a message to all Clients?

Upvotes: 2

Views: 914

Answers (1)

Sattar Imamov
Sattar Imamov

Reputation: 572

Answer 1 You should define client methods as chat.client.someMethod = function(someParams). In your case this client method is chat.client.addMessage = function (name, message) {}.

Answer 2 If you want broadcast some message without connection to your hub from .NET application, then you can do this in this way: GlobalHost.ConnectionManager.GetHubContext<MyHub>().Clients.All.addMessage(/*method params*/).

Upvotes: 2

Related Questions