title: How to communicate to specific client using SignalR?
body: <p>I am new to SignalR. I need to communicate to specific client</p>
  
  <p>Here is what I have done so far</p>
  
  <p><strong>EmployeeHub.cs</strong></p>
  
  <pre><code>using Microsoft.AspNet.SignalR;
  using Microsoft.AspNet.SignalR.Hubs;
  
  namespace WebApplication1
  {
      public class EmployeeHub : Hub
      {
          [HubMethodName("NotifyClients")]
          public static void NotifyCurrentEmployeeInformationToAllClients(string connectionID)
          {
              IHubContext context = GlobalHost.ConnectionManager.GetHubContext&lt;EmployeeHub&gt;();
  
              // update the specific connected client         
               context.Clients.Client(connectionID).updatedClients();
          }
      }
  }
  </code></pre>
  
  <p><strong>HomeController.cs</strong></p>
  
  <pre><code>using System.Collections.Generic;
  using System.Data.Entity;
  using System.Linq;
  using System.Web.Mvc;
  using WebApplication1.Models;
  
  namespace WebApplication1.Controllers
  {
      public class HomeController : Controller
      {
          List&lt;Employee&gt; empList;
  
          //Fetch Employee Records
          public ActionResult Index()
          {
              return View();
          }
          [HttpGet]
          public ActionResult GetAllEmployeeRecords()
          {
              using (var context = new EmployeeContext())
              {
                  empList = context
                  .Employees
                  .ToList();
              }          
              return PartialView("_EmployeeList", empList);
          }
  
  
          //Insert Employee Record
          public ActionResult Insert()
          {
              return View();
          }
          [HttpPost]        
          public ActionResult Insert(DataPacket dataPacket)
          {
              if (ModelState.IsValid)
              {
                  //Insert into Employee table 
                  using (var context = new EmployeeContext())
                  {
                      context.Employees.Add(dataPacket.Employee);
                      context.SaveChanges();
                  }
              }
  
              //Once the record is inserted , then notify(Clients)
              EmployeeHub.NotifyCurrentEmployeeInformationToAllClients(dataPacket.ConnectionID);
              return RedirectToAction("Index");
          }       
  }
  </code></pre>
  
  <p><strong>DataPacket.cs</strong></p>
  
  <pre><code>public class Employee
      {
          [Key]
          public int EmployeeID { get; set; }
          public string EmployeeName { get; set; }
          public string EmailAdress { get; set; }
          public string MobileNumber { get; set; }      
      }
  
      public class DataPacket
      {
          public Employee Employee { get; set; }
          public string ConnectionID { get; set; }
      }
  </code></pre>
  
  <p>and finally my <strong>Index.cshtml</strong></p>
  
  <pre><code>@model IList&lt;WebApplication1.Models.Employee&gt;
  @{
      ViewBag.Title = "Index";
  }
  &lt;link href="~/Content/bootstrap/css/bootstrap.min.css" rel="stylesheet" /&gt;
  
  &lt;div&gt;    
          &lt;h1 style="color: green"&gt;CRUD using SignalR,MVC and Entity Framework&lt;/h1&gt;
          &lt;table border="1"&gt;            
              ................................
          ................................
          &lt;/table&gt;
      &lt;br /&gt;&lt;br /&gt;       
          &lt;div id="dataTable"&gt;&lt;/div&gt;
      &lt;/div&gt;
  
  @section JavaScript{
      &lt;script src="~/Scripts/jquery.signalR-2.2.0.min.js"&gt;&lt;/script&gt;
      &lt;script src="/signalr/hubs"&gt;&lt;/script&gt;
      &lt;script type="text/javascript"&gt;
  
          $(function () {
              // Create a proxy to signalr hub on web server. It reference the hub.
              var notificationFromHub = $.connection.employeeHub;
  
              // Connect to signalr hub
              $.connection.hub.start().done(function () {
                  FetchEmployees();
              });
  
              // Notify to client with the recent updates
              notificationFromHub.client.updatedClients = function () {
                  FetchEmployees();
              };
          });
  
          function FetchEmployees() {
              var model = $('#dataTable');
              $.ajax({
                  url: '/home/GetAllEmployeeRecords',
                  contentType: 'application/html ; charset:utf-8',
                  type: 'GET',
                  dataType: 'html'
              })
                  .success(function (result) { model.empty().append(result); })               
          }
  
          // Insert Employee Record
          function InsertEmployee()
          {
  
  
                      var employee = {
                          EmployeeID: $('#txtEmployeeId').val(),
                          EmployeeName: $('#txtEmployeeName').val(),
                          EmailAdress: $('#txtEmail').val(),
                          MobileNumber: $('#txtMobile').val(),                      
                      };
  
                      var dataPacket = {
                          Employee: employee,
                          ConnectionID: GenerateRandomNumbers()
                      }
  
                      $.ajax({
                      url: '/home/Insert',
                      type: 'POST',
                      data: JSON.stringify(dataPacket),
                      contentType: "application/json;charset=utf-8",
                      success: function (data) {
                          alert('Employee added Successfully');                       
                      },
                      error: function () {
                          alert('Employee not Added');
                      }
                  });
          }        
  
          //Generate RandomNumbers
          function GenerateRandomNumbers() {
              var min = 1;
              var max = 10;
              return Math.floor(Math.random() * (max - min + 1) + min);
          }
      &lt;/script&gt;
  }
  </code></pre>
  
  <p>I am at present using a Random Number for generating the Client ID for Testing purpose. The problem is that, the record is added into the database but the notification to the client from SignalR is not happening until the Page loads.</p>
  
  <p>E.g. This is the situation when I click on the "Add New Employee" Button.</p>
  
  <p><a href="https://i.stack.imgur.com/F6Jgh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/F6Jgh.png" alt="enter image description here"></a> </p>
  
  <p>Only the data gets reflected in the table when I do a page refresh like</p>
  
  <p><a href="https://i.stack.imgur.com/NbUmT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NbUmT.png" alt="enter image description here"></a></p>
  
  <p>I have refered to <a href="https://stackoverflow.com/questions/28711919/how-can-i-send-message-to-specific-user-with-signalr">this SO Q/A</a> also but I think I am missing something.</p>
  
  <p>What is that I am missing ?</p>