title: Azure, SignalR and Web Api not sending message to client
body: <p>I have been inspired by the video "<a href="http://channel9.msdn.com/Events/Build/2013/3-502" rel="nofollow">Scaling the Real-time Web with ASP.NET SignalR</a>" at   the 56 min an 11 sec section.</p>
  
  <p><em>Imagine a web based chat client using SignalR to communicate with the server. When the client connects, its endpoint information is stored in an Azure table.</em></p>
  
  <p>A chat client can send a message to another chat client via SignalR which looks up the end point of the destination client of interest (maybe on a different instance), then using Web API sends the message to the other instance to the client via SignalR.</p>
  
  <p>To demonstrate I have <a href="https://github.com/daveapsgithub/AzureSignalRWebApi2" rel="nofollow">uploaded a sample application to github</a>.</p>
  
  <p>This all works when there is a single Azure instance.  However if there are MULTIPLE azure instances the very final call to the SignalR from the server to the client silently fails.  Its like the dynamic code just doesn't exist or its coming off a 'bad' thread or the message has been somehow sent to the wrong instance or I have just made a dopey mistake.  </p>
  
  <p>Any ideas would be greatly appreciated.</p>
  
  <p>The web page is set up with this </p>
  
  <pre><code>&lt;input type="radio" name='ClientId' value='A' style='width:30px'/&gt;Chat client A&lt;/br&gt;
  &lt;input type="radio" name='ClientId' value='B' style='width:30px'/&gt;Chat client B&lt;/br&gt;
  &lt;input type='button' id='register' value='Register' /&gt;
  &lt;input type='text' id='txtMessage' size='50' /&gt;&lt;input type='button' id='send' value='Send' /&gt;
  
  &lt;div id='history'&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>and the JS is</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      $(function () {
  
          // Declare a proxy to reference the hub. 
          var chat = $.connection.chatHub;
  
          chat.client.sendMessageToClient = function (message) {
              $('#history').append("&lt;br/&gt;" + message);
          };
  
  
          // Start the connection.
          $.connection.hub.start().done(function () {
  
              $('#register').click(function () {
  
                  // Call the Send method on the hub. 
                  chat.server.register($('input[name=ClientId]:checked', '#myForm').val());
  
              });
  
  
              $('#send').click(function () {
  
                  // Call the Send method on the hub. 
                  chat.server.sendMessageToServer($('input[name=ClientId]:checked', '#myForm').val(), $('#txtMessage').val());
              });
  
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>The hub is as follows. (I have a little storage class to store the end point information in a Azure table).  Notice the static method SendMessageToClient.  This is what ultimately fails.  It is called from the Web Api class (below)</p>
  
  <pre><code>public class ChatHub : Hub
  {
      public void Register(string chatClientId)
      {
          Storage.RegisterChatEndPoint(chatClientId, this.Context.ConnectionId);
      }
  
      /// &lt;summary&gt;
      /// Receives the message and sends it to the SignalR client.
      /// &lt;/summary&gt;
      /// &lt;param name="message"&gt;The message.&lt;/param&gt;
      /// &lt;param name="connectionId"&gt;The connection id.&lt;/param&gt;
      public static void SendMessageToClient(string message, string connectionId)
      {
          GlobalHost.ConnectionManager.GetHubContext&lt;ChatHub&gt;().Clients.Client(connectionId).SendMessageToClient(message);
  
          Debug.WriteLine("Sending a message to the client on SignalR connection id: " + connectionId);
          Debug.WriteLine("Via the Web Api end point: " + RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["WebApi"].IPEndpoint.ToString());
  
      }
  
  
      /// &lt;summary&gt;
      /// Sends the message to other instance.
      /// &lt;/summary&gt;
      /// &lt;param name="chatClientId"&gt;The chat client id.&lt;/param&gt;
      /// &lt;param name="message"&gt;The message.&lt;/param&gt;
      public void SendMessageToServer(string chatClientId, string message)
      {
          // Get the chatClientId of the destination.
          string otherChatClient = (chatClientId == "A" ? "B" : "A");
  
          // Find out this other chatClientId's end point
          ChatClientEntity chatClientEntity = Storage.GetChatClientEndpoint(otherChatClient);
  
          if (chatClientEntity != null)
              ChatWebApiController.SendMessage(chatClientEntity.WebRoleEndPoint, chatClientEntity.SignalRConnectionId, message);
      }
  }
  </code></pre>
  
  <p>Finally the ChateWebApiController is this</p>
  
  <pre><code>public class ChatWebApiController : ApiController
  {
      [HttpGet]
      public void SendMessage(string message, string connectionId)
      {
          //return message;
          ChatHub.SendMessageToClient(message, connectionId);
      }
  
      /// &lt;summary&gt;
      /// This calls the method above but on a different instance via Web API
      /// &lt;/summary&gt;
      /// &lt;param name="endPoint"&gt;The end point.&lt;/param&gt;
      /// &lt;param name="connectionId"&gt;The connection id.&lt;/param&gt;
      /// &lt;param name="message"&gt;The message.&lt;/param&gt;
      public static void SendMessage(string endPoint, string connectionId, string message)
      {
          HttpClient client = new HttpClient();
          client.BaseAddress = new Uri("http://" + endPoint);
  
          // Add an Accept header for JSON format.
          client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  
          string url = "http://" + endPoint + "/api/ChatWebApi/SendMessage/?Message=" + HttpUtility.UrlEncode(message) + "&amp;ConnectionId=" + connectionId;
  
          client.GetAsync(url);
      }
  
  }
  </code></pre>