How to connect multiple hubs in SignalR and ASP.NET Core

Viewed 167

I tried following code. I have chathub and msghub, in the client code I don't know how to configure 2 hubs in asp.net core.

// Client code

const connection = new signalR.HubConnectionBuilder()
.withUrl("https://localhost:44364/chathub" , {accessTokenFactory: () => this.loginToken `
}).build();
connection.start()
          .then(() => console.log('connected!'))
connection.invoke('SendMessage', message);
messageBox.value = '';
});

// start up configuration

app.UseEndpoints(endpoints =>
{
    endpoints.MapRazorPages();
    endpoints.MapHub<ChatHub>("/chathub");
    endpoints.MapHub<MsgHub>("/msghub");
});

// hub class

public class ChatHub : Hub
{
    public async Task SendMessage(string message)
    {
        await Clients.All.SendAsync("ReceiveMessage", message);
    }
}
public class MsgHub : Hub
{
    public async Task SendMessage(string message)
    {
        await Clients.All.SendAsync("ReceiveMessage", message);
    }
}
0 Answers
Related