using Microsoft.AspNetCore.SignalR; using SignalR5.BusinessLogic; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Threading.Tasks; namespace SignalR5.Hubs { public class CallNotificationHub : Hub { // // https://docs.microsoft.com/en-us/aspnet/core/signalr/hubs?view=aspnetcore-5.0 // https://docs.microsoft.com/en-us/aspnet/core/signalr/hubs?view=aspnetcore-5.0 // private SqlDependency dependency; // public Task OnConnected() public override async Task OnConnectedAsync() { //return base.OnConnectedAsync(); await base.OnConnectedAsync(); } //[HubMethodName("sendCallNotifications")] public async Task SendCallNotifications() { try { string connectionString = "server=(local);uid=sa;pwd=;database=myDB"; SqlDependency.Start(connectionString); SqlConnection connection = new SqlConnection(connectionString); connection.Open(); SqlCommand command = new SqlCommand(); command.CommandText = "SELECT [ID], [TimeEntered], [CallFrom], [CallTo] FROM [dbo].[tbl_Log_InboundTwilioCalls]"; command.Connection = connection; command.CommandType = CommandType.Text; dependency = new SqlDependency(command); dependency.OnChange += new OnChangeEventHandler(dependency_OnChange); DataTable dt = new DataTable(); dependency.AddCommandDependency(command); var reader = command.ExecuteReader(); SendEmail sendEmail = new SendEmail("SendCallNotifications Reached", "New SignalR5 SendCallNotifications at " + DateTime.Now.ToString(), ""); //await Clients.All.SendAsync("ReceiveMessage", "user", "message"); } catch (Exception ex) { SendEmail sendErrEmail = new SendEmail("SendCallNotifications Error", ex.ToString(), ""); } //await Clients.All.SendAsync("RecieveNotification", "Changed"); await Clients.All.SendAsync("RecieveNotification", "Changed at " + DateTime.Now.ToString()); } private void dependency_OnChange(object sender, SqlNotificationEventArgs e) { SendEmail sendEmail = new SendEmail("Dependency Change", e.Type.ToString() + " at " + DateTime.Now.ToString(), ""); if (e.Type == SqlNotificationType.Change) { CallNotificationHub nHub = new CallNotificationHub(); nHub.SendCallNotifications(); } } } } And the client code "use strict"; var connection = new signalR.HubConnectionBuilder().withUrl("/CallNotificationHub").build(); connection.on("RecieveNotification", (result) => { //var li = document.createElement("li"); //li.textContent = result; //document.getElementById("messagesList").appendChild(li); console.log(result); alert("Changed"); }); connection.start().then(function () { connection.invoke("SendCallNotifications").catch(err => console.error(err)); }).catch(function (err) { return console.error(err.toString()); });