SignalR - Broadcasting over a Hub in another Project from outside of a Hub

14,320

This will only work, as far as I am aware, when you are calling the hub from within the web application.

In order to interact with the hub from outside of the web application, e.g. from a Windows Service, you will need to take a look at the SignalR Client Hubs documentation

  1. Add the following NuGet package to your project: Microsoft.AspNet.SignalR.Client

  2. Add the following statement to the top of your page: using Microsoft.AspNet.SignalR.Client;

  3. You would need to create a connection to the hub, and then start the connection.


var connection = new HubConnection("http://mysite/");
IHubProxy myHub = connection.CreateHubProxy("MyHub");

connection.Start().Wait(); // not sure if you need this if you are simply posting to the hub

myHub.Invoke("addNewMessageToPage", "Hello World");  

In your hub you would then need to have a method for AddNewMessageToPage which accepts the hello world string and from here call Clients.All.addNewMessageTopage(message)

Share:
14,320
Mithrilhall
Author by

Mithrilhall

Web Developer

Updated on July 17, 2022

Comments

  • Mithrilhall
    Mithrilhall almost 2 years

    I have two projects in my solution:

    Project 1: "SignalRChat" (MVC) - Works fine
    Project 2: "DatabaseWatcherService" Windows Service - Works fine

    I'm trying to make a call to my SignalRChat Hub from my Windows Service and it doesn't appear to be working.

    This is where I call my Hub from my windows service (https://github.com/SignalR/SignalR/wiki/Hubs#broadcasting-over-a-hub-from-outside-of-a-hub):

    void PerformTimerOperation(object sender, EventArgs e)
        {
            eventLog1.WriteEntry("Timer ticked...");
    
            var message = "test";
    
            var context = GlobalHost.ConnectionManager.GetHubContext<SignalRChat.ChatHub>();
            context.Clients.All.addNewMessageToPage(message);
        }
    

    I'm getting the following error when attempting to connect:

    Message=The remote server returned an error: (500) Internal Server Error.

    I'm trying to connect via var connection = new HubConnection("http://localhost:2129");

    Port 2129 is what my MVC project is running on.

  • Mithrilhall
    Mithrilhall about 11 years
    I had a typo on my side..incorrect Hub name "MyHub". Once I changed it to my actual Hub name it worked fine. Thank you.
  • xan
    xan over 10 years
    This was exactly what I was looking for, thanks! :-)
  • JobaDiniz
    JobaDiniz almost 8 years
    this is bad,ins't it? You exposed addNewMessageTopage to all clients, so any connected client can call this method, and this is clearly not the intent. I want to publish a message to clients from WinService, but didn't find a good way to do it.
  • Tim B James
    Tim B James almost 8 years
    yeah it is exposed to all. it answers the question asked, but your question is somewhat different.
  • Divyang Desai
    Divyang Desai over 4 years
    @TimBJames: Is this possible with ASP.NET Core 2.2? or any other work around. Here is my question: stackoverflow.com/q/59840085/4753489