Jamil
Jamil

Reputation: 930

Hub method from client is not invoking in ASP.NET Core SignalR

I'm adding SignalR to my app. I can send messages from server to client, but can't call hub methods from client. Here's my hub interface:

public interface IGeneralHub
{
    Task BroadcastMessage(HubMessage msg); //string type, string payload);
    Task JoinHub(List<int> ids);
}

and hub client:

public class AuctionHub : Hub<IGeneralHub>
{
    public void Broadcast(HubMessage msg)
    {
        Clients.All.BroadcastMessage(msg);
    }
    public void JoinHub(List<int> ids)
    {
        foreach (var id in ids.Distinct())
            Groups.AddToGroupAsync(Context.ConnectionId, id.ToString());
    }
}

And client side:

this.hubConnections = new signalR.HubConnectionBuilder()
                    .withUrl(`${environment.hubHost}/document/`)
                    .build();

this.hubConnections.start()
                    .then(() => console.log('Connection started'))
                    .catch(err => console.log('Error while starting connection: ' + err));

this.hubConnections.invoke('joinGroup', JSON.parse(localStorage.getItem('ws-document')));

I receive messages, but joinGroup is never called. What am I doing wrong?

Upvotes: 3

Views: 2775

Answers (1)

TanvirArjel
TanvirArjel

Reputation: 32059

I receive messages, but joinGroup is never called. What am I doing wrong?

This is because there is no JoinGroup method in your AuctionHub class. It should JoinHub instead as follows:

this.hubConnections.invoke('joinHub', JSON.parse(localStorage.getItem('ws-document')));

Upvotes: 4

Related Questions