title: Asp.net core - How to implement realtime using signalR
body: <p>I am adding a comments part in my project, I would like to use real-time using signalR.
  I'm using Ajax for adding comments, and I want to refresh data for all users after the comment inserts into the database.</p>
  <p><strong>This is my code in Razor view :</strong></p>
  <pre><code>  &lt;form asp-action=&quot;SendComment&quot; asp-controller=&quot;Home&quot; asp-route-subId=&quot;@Model.Subject.Id&quot;
                  asp-route-AccountName=&quot;@User.Identity.Name&quot; onsubmit=&quot;return jQueryAjaxPost(this);&quot;&gt;
                              &lt;textarea name=&quot;comment&quot; id=&quot;myTextbox&quot; required class=&quot;form-control mb-3&quot; rows=&quot;3&quot; cols=&quot;1&quot; placeholder=&quot;اكتب هنا&quot;&gt;&lt;/textarea&gt;
                              &lt;div class=&quot;d-flex align-items-center&quot;&gt;
                                  &lt;button type=&quot;submit&quot; id=&quot;myBtn&quot; class=&quot;btn bg-blue-400 btn-labeled btn-labeled-right ml-auto&quot;&gt;&lt;b&gt;&lt;i class=&quot;icon-paperplane&quot;&gt;&lt;/i&gt;&lt;/b&gt; ارسال&lt;/button&gt;
                              &lt;/div&gt;
                          &lt;/form&gt;
  </code></pre>
  <p><strong>Ajax code :</strong></p>
  <pre><code>jQueryAjaxPost = form =&gt; {
  try {
      $.ajax({
          type: 'POST',
          url: form.action,
          data: new FormData(form),
          contentType: false,
          processData: false,
          success: function (res) {
              if (res.isValid) {
                  $('#view-all').html(res.html)
              }
              else
                  $('#form-modal .modal-body').html(res.html);
          },
          error: function (err) {
              console.log(err)
          }
      })
      //to prevent default form submit event
      return false;
  } catch (ex) {
      console.log(ex)
  }
  </code></pre>
  <p>}</p>
  <p><strong>signalR code (<em>Not finished</em>)</strong></p>
  <pre><code>&lt;reference path=&quot;../lib/signalr/browser/signalr.js&quot; /&gt;
  </code></pre>
  <p>$(() =&gt; {
  let connection = new signalR.HubConnectionBuilder().withUrl(&quot;/signalServer&quot;).build();</p>
  <pre><code>connection.start();
  
  connection.on(&quot;refreshData&quot;, function () {
      loadData();
  });
  
  loadData();
  
  function loadData() {
      debugger;
  
      $.ajax({
          type: 'GET',
  
          url: '@Url.Action(&quot;refreshComments&quot;,&quot;Home&quot;)',
          success: function (res) {
              $('#view-all').html(res);
          }
      })
  
  }
  </code></pre>
  <p>});</p>
  <p><strong>Code-behind :</strong></p>
  <pre><code> var newComment = new CourseComment
                      {
                          Comment = comment,
                          Date = DateTime.Now,
                          ApplicationUser = user,
                          SubjectId = subId,
                          CreatedDate = DateTime.Now
                      };
                      _courseCommnt.Entity.Insert(newComment);
                      await _courseCommnt.SaveAsync();
  
                      await _signalR.Clients.All.SendAsync(&quot;refreshData&quot;);
  
                      _toastNotification.AddSuccessToastMessage(&quot;تم ارسال التعليق بنجاح&quot;);
  
                      var courseComments = await _courseCommnt.Entity.GetAll().Include(a =&gt; a.ApplicationUser)
                          .Where(a =&gt; a.SubjectId == subId).OrderByDescending(a =&gt; a.Date).AsNoTracking().ToListAsync();
  
                      var vm = new HomeViewModel
                      {
                          CourseComments = courseComments
                      };
  
                      return Json(new
                      {
                          isValid = true,
                          html = Helper.RenderRazorViewToString(this, &quot;_SubjectComments&quot;, vm)
                      });
  </code></pre>