title: Push Data To Client Using SignalR
body: <p>I'm sorry for potentially creating a duplicate thread here, but I simply cannot get my web application to do what I need by following the other examples I've found.</p>
  
  <p>My goal is to do one of the following:</p>
  
  <p><strong>OPTION 1 - IDEAL SOLUTION</strong><br>
  Fetch data from a database and update the UI on a web page ONLY when changes are made to the data being displayed on the web page. For example, if a user is viewing a service ticket, I don't want to update the UI on that page unless that ticket is changed.</p>
  
  <p><strong>OPTION 2 - ACCEPTABLE SOLUTION</strong><br>
  Fetch data from a database every x seconds and use that data to update the UI on a web page.</p>
  
  <p>My current implementation of <strong>Option 2</strong> is below. It involves sending an asynchronous HTTP request every 60 seconds to fetch the data:</p>
  
  <pre><code>// start checking for new messages every 60 seconds
  setInterval(function () {
      $.ajax({
          async: true,
          type: "POST",
          contentType: "application/json; charset=utf-8;",
          url: "/AJAX_Handlers/CheckForNewMessages.ashx",
          dataType: "json",
          success: function (Result) {
              var new_message_received = Result[0]["NewMessageReceived"];
  
              if (new_message_received) {
                  $("#DIVMessageReminder").html("&lt;strong&gt;You have " + num_new_messages + " new message(s).&lt;/strong&gt;");
                  $("#DIVMessageReminder").show();
              }
              else {
                  $("#DIVMessageReminder").hide();
              }
          }
      });
  }, 60000);
  </code></pre>
  
  <p>Rather than sending an HTTP request every 60 seconds, I would like to use SignalR to push that data to the client every 60 seconds.</p>
  
  <p>As a simple example, I have created the following Hub with a method to get the current time on the server:</p>
  
  <pre><code>Imports Microsoft.AspNet.SignalR
  
  Public Class ServerTimeHub
      Inherits Hub
  
      Public Sub GetServerTime()
          Dim current_time As String = Now.ToString()
          Clients.All.updateTime(current_time)
      End Sub
  
  End Class
  </code></pre>
  
  <p>And a basic textbox:</p>
  
  <pre><code>&lt;input id="TXTLongPollingTest" type="text" class="form-control" /&gt;
  </code></pre>
  
  <p>And my client-side code:</p>
  
  <pre><code>var hub = $.connection.serverTimeHub;
  
  hub.client.updateTime = function (new_time) {
      $("#TXTLongPollingTest").val(new_time);
  }
  
  $.connection.hub.start().done(function () {
      alert("connected to the SignalR hub");
      hub.getServerTime();
  }).fail(function (err) {
      alert("failed to connect to SignalR hub: " + err);
  });
  </code></pre>
  
  <p>At first I tried getting it to fetch the server time just once. My code will successfully connect to the hub, but then it throws an error saying "Uncaught TypeError: hub.getServerTime is not a function". That's the first problem I haven't been able to overcome.</p>
  
  <p>The second problem is: How can I get the hub to send the current time to the client on a regular interval such as every 1 second?</p>