I am trying to develop an asp.net mvc application and also trying to use signalr. The problem is that I have two tables that control user attributes in the project. I have a notification table as well as a NotificationUser table in which there are many notification tables and user tables. I try so that if a user creates a notification to another user in the system, I try to show a pop-up window that confirms the user with a simple message like "Hello! New notification received." The problem is that the javascript function changes the frequency of the signal, so many times. All the steps that I used in the signal below are
stored procedure
ALTER PROCEDURE [dbo].[GetNotifications] @userid int AS BEGIN select n.Ntf_Title,Ntf_Description,n.Ntf_Date from dbo.SysNotifications n INNER JOIN dbo.SysNotificationUser u on n.Ntf_ID =u.NtU_NtfID where NtU_UserID=@userid AND NtU_IsRead=0 END
Hub
[HubName("signalRHub")] public class NtfHub : Hub { [HubMethodName("notifyChanges")] public static void NotifyChanges() { var context = GlobalHost.ConnectionManager.GetHubContext<NtfHub>(); context.Clients.All.notifyChanges(); } }
Class startup
public class Startup { public void Configuration(IAppBuilder app) { app.MapSignalR(); } }
Partial view
[HttpGet] public ActionResult GetNtf() { //NtfRepo rp = new NtfRepo(this.HttpContext); string connectionString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString; int userid =id; using (SqlConnection sqlcon = new SqlConnection(connectionString)) { using (SqlCommand sqlcom = new SqlCommand("[GetNotifications]", sqlcon)) { sqlcon.Open(); sqlcom.CommandType = CommandType.StoredProcedure; sqlcom.Parameters.AddWithValue("@userid", userid); sqlcom.Notification = null; SqlDependency dependancy = new SqlDependency(sqlcom); dependancy.OnChange += dependancy_OnChange; var reader = sqlcom.ExecuteReader(); var ntf= reader.Cast<IDataRecord>() .Select(e => new PopulateNtfBar() { Title = e.GetString(0), Description = e.GetString(1), TimeDiff = FindDifferenceTime(e.GetDateTime(2)) }).ToList(); return PartialView("~/Views/Shared/CheckNotification.cshtml", ntf); } } }
Finally, Script
$(function () { var notification = $.connection.signalRHub; // Create a function that the hub can call to broadcast messages. notification.client.notifyChanges = function () { getData(); toastr.warning("Hey,You have Ntf"); }; // Start the connection. $.connection.hub.start().done(function () { getData(); }).fail(function (e) { }); }); function getData() { var tbl = $("#header_notification_bar") $.ajax({ url: '@Url.Action("GetNtf","Home")', contentType: 'application/html ; charset:utf-8', type: 'GET', dataType: 'html' }).success(function (result) { tbl.empty().append(result); }).error(function () { }); }
notification.client.notifyChanges so many times if the user creates a notification. What is the problem? Any ideas? I can not optimize it
EDIT 1 I call NtfHub.NotifyChanges in the controller.
void dependancy_OnChange(object sender, SqlNotificationEventArgs e) { if (e.Type == SqlNotificationType.Change) { NtfHub.NotifyChanges(); } }