title: Implementing WebSockets using SignalR
body: <p>I'm trying to use websockets into my ASP.NET MVC web-app but I can't implement, so here I'm trying to display each database update on the end-user web-page without any need to refresh.</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;span id="nbAlertes"&gt;&lt;/span&gt;
  &lt;ul id="listeAlertes"&gt;&lt;/ul&gt;
  </code></pre>
  
  <p>Javascript / SignalR / jQuery</p>
  
  <pre><code>&lt;!--Reference the SignalR library. --&gt;
  &lt;script src="Scripts/jquery.signalR-2.0.2.min.js"&gt;&lt;/script&gt;
  &lt;!--Reference the autogenerated SignalR hub script. --&gt;
  &lt;script src="signalr/hubs"&gt;&lt;/script&gt;
  &lt;script&gt;
   $(function () {
          // Declare a proxy to reference the hub.
          var alertes = $.connection.AlerteHub;
          // Create a function that the hub can call to broadcast messages.
          alertes.client.broadcastMessage = function (nbAlertes, listeAlertes) {
              // Html encode display name and message.
              var nbA = $('&lt;div /&gt;').text(nbAlertes).html();
              var lstA = $('&lt;div /&gt;').text(listeAlertes).html();
              // Add the message to the page.
              $('#nbAlertes').text(nbA);
              lstA.forEach(function(item) {
                  $('#listeAlerte').append(item.nomPoste);
              });
          };
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>class AlerteHub:</p>
  
  <pre><code>public class AlerteHub : Hub
      {
          public void GetAll()
          {
              var nbAlertes = new CalculAlertesUtilitaire().compter();
              var listeAlertes = new CalculAlertesUtilitaire().lister(5);
  
              // Call the broadcastMessage method to update clients.
              Clients.All.broadcastMessage(nbAlertes, listeAlertes);
          }
  
  MonitoringNDataContext _db = new MonitoringNDataContext();
  
  public string compter()
      {
          var compte = _db.Alertes.ToList().Count();
          return (compte == 0) ? "" : compte.ToString();
      }
  
      public ICollection&lt;AlerteModel&gt; lister(int nb)
      {
          return (ICollection&lt;AlerteModel&gt;)_db.Alertes.ToList().Take(nb).ToArray();
      }
      }
  </code></pre>
  
  <p>class Startup</p>
  
  <pre><code>public class Startup
      {
          public void Configuration(IAppBuilder app)
          {
              // Any connection or hub wire up and configuration should go here
              app.MapSignalR();
          }
      }
  </code></pre>
  
  <p>How do I to make it work, please ?</p>