1

Hub-code

public class TestHub : Hub
    {
        public void Message(string message)
        {
                Clients.Group("testGroup").displayMessage(message);
        }
        public override Task OnConnected()
        {
            Groups.Add(Context.ConnectionId, "testGroup");
            return base.OnConnected();
        }
    }

Javascript Code with generated Proxy

var myHub = $.connection.testHub;
myHub.on('message', this.displayMessage);

$.connection.hub.start();



function displayMessage(message) {
 console.log(message);
}

If I do the above it seems like the hub.start() is running correctly and it returns some form of object. But when i debug with a breakpoint inside OnConnected I never hit.

Any suggestions?

3 Answers3

4

Basiclly, you can invoke hub methods but the OnConnect won't work if you don't have subscriptions on the hub. It's weird but that's the way it works.

Do it like this:

var myHub = $.connection.testHub;

//add subscriptions
$.extend(myHub.client, {
    stupidLogicSignalR: function () {}
});

myHub.on('message', this.displayMessage);
$.connection.hub.start();

Here's a similar question. Also this issue can help

Community
  • 1
  • 1
Marcus Höglund
  • 16,172
  • 11
  • 47
  • 69
  • $.extend is an alternate way to define method on client (with the generated proxy) it can be like : myHub.client.stupidLogicSignalR=function(){}; – Abdullah Tahan Jul 15 '19 at 06:08
0

Worked with other syntax for the subscribe

myHub.client.displayMessage = () => {console.log('message');};
0

enable logging at front end by(do it after var myHub = $.connection.testHub;):

$.connection.hub.logging = true;

or assign a callback to .start by:

$.connection.hub.start().done(function () {
console.log("connection started")
});
Sahal Tariq
  • 198
  • 1
  • 9