title: How to notify database changes with signalR
body: <p>I've just started with SignalR and would like to try out the real time notifications. The objective is to keep displaying the updated message on web page. 
  There is a database table - <code>DummyData</code> with a column <code>Message</code>. This table has only one record - <code>Hello</code>
  When the page loads, "Hello" is displayed.</p>
  
  <p>I then manually run the command  in sql server 2012   </p>
  
  <p><code>update DummyData set Message='hello world'</code>, but the message isn't updated in the webpage.</p>
  
  <p><strong>aspx:</strong></p>
  
  <pre><code>&lt;script&gt;
      $(function () {
          var notify = $.connection.notificationsHub;
  
          $.connection.hub.start().done(function () {
              notify.server.notifyAllClients();
          });
  
          notify.client.displayNotification = function (msg) {               
              $("#newData").html(msg);
          };
  
          notify.client.stopClient = function () {
              $.connection.hub.stop();
          };
      });
  &lt;/script&gt;
   &lt;span id="newData"&gt;&lt;/span&gt;
  </code></pre>
  
  <p><strong>aspx.cs:</strong></p>
  
  <pre><code>public string SendNotifications()
      {
        string message = string.Empty;
        using (SqlConnection connection = new SqlConnection(conStr))
         {
          string query = "SELECT [Message] FROM [dbo].[DummyData]";
  
          SqlCommand command = new SqlCommand(query, connection)
          command.Notification = null;
          SqlDependency dependency = new SqlDependency(command);
                  dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
          connection.Open();
          SqlDataReader reader = command.ExecuteReader();
  
          if (reader.HasRows)
          {
           reader.Read();
           message = reader[0].ToString();
          }
         }            
          return message;
      }
  
      private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
      {
          if (e.Type == SqlNotificationType.Change)
          {
              SendNotifications();
          }            
      }
  </code></pre>
  
  <p><strong>NotificationsHub.cs</strong></p>
  
  <pre><code>public class NotificationsHub : Hub
  {
   Messages obj = new Messages();
   public void NotifyAllClients(string msg)
    {
     IHubContext context = GlobalHost.ConnectionManager.GetHubContext&lt;NotificationsHub&gt;();
     context.Clients.All.displayNotification(msg);
    }
  
   public override System.Threading.Tasks.Task OnConnected()
    {
     NotifyAllClients();
     return base.OnConnected();
    }
  
   public override System.Threading.Tasks.Task OnDisconnected(bool stopCalled)
   {
    NotifyAllClients();
    return base.OnDisconnected(stopCalled);
   }
  }
  </code></pre>
  
  <p><strong>global.asax:</strong></p>
  
  <pre><code>protected void Application_Start(object sender, EventArgs e)
          {
              SqlDependency.Start(Constr);
          }
  </code></pre>
  
  <p>When I run the tsql update command, the break point is first hit at <code>dependency_OnChange</code> and I can see the new updated text being returned from <code>SendNotification</code>. But it isn't seen reflected on page. Feels like I'm almost there but something is missing.</p>