Q:

SignalR combined with WebApi

I'm making a proof of concept for SignalR. Basically I followed the tutorial from this website: Tutorial: Getting Started with SignalR 2 and I'm trying to send messages to the frontend from a WebApi in a different project.
The javascript code is as followed:
$(function () {
        // Declare a proxy to reference the hub.
        var chat = $.connection.chatHub;
        // Create a function that the hub can call to broadcast messages.
        chat.client.broadcastMessage = function (name, message) {
            // Html encode display name and message.
            var encodedName = $('<div />').text(name).html();
            var encodedMsg = $('<div />').text(message).html();
            // Add the message to the page.
            $('#discussion').append('<li><strong>' + encodedName
                + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
        };
        // Start the connection.
        $.connection.hub.start().done(function () {
            $('#sendmessage').click(function () {
                // Call the Send method on the hub.
                chat.server.send($('#displayname').val(), $('#message').val());
                // Clear text box and reset focus for next comment.
                $('#message').val('').focus();
            });
        });
    });

The code for my Hub:
public class ChatHub : Hub
{
    public void Send(string name, string message)
    {
        // Call the broadcastMessage method to update clients.
        Clients.All.broadcastMessage(name, message);
    }
}

When I build this the code it works just like the example in the link above. However when I try to send messages from another project with the following code, it won't work:
public class MyMessageHandler : IHandleMessages<MyMessage>
{
    static ILog log = LogManager.GetLogger<MyMessageHandler>();

    public Task Handle(MyMessage message, IMessageHandlerContext context)
    {
        log.Info($"Message received: {message.Name}");

        var hub = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
        hub.Clients.All.Send("Admin", "stop the chat");
        return Task.Delay(0);
    }
}

When I run this code the frontend is running on this link/port: http://localhost:8854/index.html and the webapi on this link/port: http://localhost:8387/api/values.
I feel like I am missing one final thing, but numerous Google results couldn't help me. Does anybody know how I can fix this?

A:

As we outlined from comments, you have two app instances on the server side.
Your hub is in one instance: public class ChatHub : Hub.
And this call is in another: GlobalHost.ConnectionManager.GetHubContext<ChatHub>();.
So second hub instance does not equals instance of the hub with connected clients.
To solve this issue, you need configure backplane for signalr. It allows same hub to be used in different instances of the server. I recommend to use SQL server option instead of Azure/Redis.
Another solution is to stop use server-side hub in the second instance. And use client C# signalR code to connect to the first one instance (and hub).

