I have a Angular 7 application in which I want to implement push notification using SignlaR (2.4.4 version) with API in C#. Can anyone suggest how to solve my problem?
I have tried everything that I can, but I am not able to establish a connection between Angular SignalR client and SignalR Hub created in C# API.
1) This is my Startup.cs
HubConfiguration cfg = new HubConfiguration();
app.MapSignalR<PersistentConnection>("/ClientHub");
app.MapSignalR(cfg);
2) This is my Hub
public class ClientHub : Hub
{
public Task SendNotificationCount(int chatUserCnt)
{
var context =
GlobalHost.ConnectionManager.GetHubContext<ClientHub>();
return context.Clients.All.SendAsync("Send", chatUserCnt);
}
public Task SendHelloToAll(string message)
{
var context = GlobalHost.ConnectionManager.GetHubContext<ClientHub>();
return context.Clients.All.SendMsgAsync("SendToAll", message);
}
}
3) This is Angular Code where I am trying to Connect to the Hub.
hubUrl = http://localhost:60067/ClientHub
connectToHub(hubUrl:string) {
this.connection = new HubConnectionBuilder().withUrl(hubUrl).build();
this.connection.start().then(() => {
console.log("connected to hub");
this.connection.on('SendToAll', (message) => {
alert(message);
});
}).catch((error) => {
console.log(error);
});
}
I am expecting, whenever there is a change in the database for notification to get alerts through the Hub in realtime. Also I want to send database changes using signalR hub to Angular. But I am not able to connect with hub from Angular.