'How can I call all the signal R events without explicitly invoking them using hubconnection.On method
We have a API with SignalR implementation for chat, reminder and so on. I want to invoke all of methods on my client portal that's on angular without passing hub method names explicitly through hub connection "On" method like below.
this.hubConnection.on('SignalMessageReceived', (payload: any) => {
// business logic goes here
});
Solution 1:[1]
You have to do declare an interface for your methods and a hub:
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;
namespace MyApp.Hubs
{
public interface IChat
{
Task SignalMessageReceived(string message);
}
public class ChatHub : Hub<IChat>
{
// here you can put methods that can be called from angular
}
}
Then in Startup.cs:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHub<MyApp.Hubs.ChatHub>("api/hubs/chat");
});
Now when you want to invoke the method you get a reference to IHubContext<ChatHub, IChat> hubcontext through dependency injection. For example in a controller you can do:
IChat hub = hubcontext.Clients.All;
await hub.SignalMessageReceived("Hello");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | bgman |