Clients are null when trying to send a message through signalR

I have the following hub in my MVC application, from where I would like to send a simple message to my client-side code:

using SignalR.Hubs; public class Progress : Hub { public void Send(string message) { // Call the addMessage method on all clients Clients.addMessage(message); } public Progress() { Clients.addMessage("Starting to analyze image"); } } 

And the following javascript in my opinion

  <script src="/Scripts/jquery.signalR.js" type="text/javascript"></script> <script src="/signalr/hubs" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function () { // Proxy created on the fly var connection = $.connection('/signalr/hubs/progress'); // Declare a function on the chat hub so the server can invoke it connection.addMessage = function (message) { $('#messages').append('<li>' + message.Content + '</li>'); }; // Start the connection connection.start(); }); </script> } 

My problem is that the code calls the constructor or submit method, the Clients object is null.

Everything looks fine when I debug client side code. / Signalr / hubs / route returns javascript code, and there are no errors when running javascript.

I can add that the backend code runs on top of the Umbraco 5 CMS environment, which, I'm not sure, causes any violations.

Any suggestions on how I can debug / solve this?

+4
source share
2 answers

It looks like you are trying to broadcast a message from the server-side code by creating an instance of the hub. Unfortunately, this is not so. You can see an example of sending messages from the server side here: https://github.com/SignalR/SignalR/wiki/Hubs . Take a look at the section “Broadcasting through a hub from outside the hub”.

On the server side, where you want to broadcast from

the following will be used:
 using SignalR.Infrastructure; string message = "Test Message"; IConnectionManager connectionManager = AspNetHost.DependencyResolver.Resolve<IConnectionManager>(); dynamic clients = connectionManager.GetClients<MyHub>(); clients.addMessage(message); 

This is consistent with your Send() method, however, if you are trying to set up a progress indicator, you probably want to send messages only to the caller. In this case, you need to update the Progress method to Caller.addMessage("Starting to analyze image"); . Doing this from the outside of the hub is a bit more complicated, as you will need to keep track of the client ID for the connection you want to update. Once you find out that the above change:

 clients[clientId].addMessage(message); 
+4
source

You need to read the documents. Everything in your example looks wrong. Start here:

https://github.com/SignalR/SignalR/wiki/QuickStart-Hubs

+1
source

Source: https://habr.com/ru/post/1397297/


All Articles