title: Why SignalR change function hitting many many times?
body: <p>I try to develop an asp.net mvc application and also trying to use signalr. The problem is that i have two tables that control user notificitaions in project. I have a Notification table and also NotificationUser table which is many to many table of notification and user tables. I am trying achive that if a user create a notification to another user in system I try to show a pop-up that confirm user with a simple message like 'Hey!New notification received'. The problem is javascript change function of signalr hitting so many times.All step i used in signalR listed below</p>
  
  <p>the stored procedure</p>
  
  <pre><code>    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
  </code></pre>
  
  <p>The Hub</p>
  
  <pre><code> [HubName("signalRHub")]
   public class NtfHub : Hub
   {
      [HubMethodName("notifyChanges")]
      public static void NotifyChanges()
      {
          var context = GlobalHost.ConnectionManager.GetHubContext&lt;NtfHub&gt;();
          context.Clients.All.notifyChanges();
      }
  
  
  }
  </code></pre>
  
  <p>The StartUp Class</p>
  
  <pre><code>public class Startup
  {
      public void Configuration(IAppBuilder app)
      {
          app.MapSignalR();
  
      }
  }
  </code></pre>
  
  <p>The Partial View</p>
  
  <pre><code> [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&lt;IDataRecord&gt;()
                     .Select(e =&gt; new PopulateNtfBar()
                     {
                         Title = e.GetString(0),
                         Description = e.GetString(1),
                         TimeDiff = FindDifferenceTime(e.GetDateTime(2))
                     }).ToList();
                  return PartialView("~/Views/Shared/CheckNotification.cshtml", ntf);
              }
          }
      }
  </code></pre>
  
  <p>At Last,The Script</p>
  
  <pre><code>  $(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 () {
  
          });
  
  
      }
  </code></pre>
  
  <p>notification.client.notifyChanges hitting so many times if a user create a notification.Where is the problem? Any idea? i cannot optimize it</p>
  
  <p>EDIT 1 
  I am calling NtfHub.NotifyChanges in controller.</p>
  
  <pre><code>  void dependancy_OnChange(object sender, SqlNotificationEventArgs e)
      {
          if (e.Type == SqlNotificationType.Change)
          {
              NtfHub.NotifyChanges();
          }
      }
  </code></pre>