title: Calling SignalR hub from Web API controller without any view
body: <p>I am a beginner in SignalR. I have found a sample that is client and server project, client is wpf and server is ASP.NET Core.</p>
  <p>But the server part in the JavaScript file defines the hub and receives or sends the message. I want to have the part written in JavaScript inside the controller, but I do not know how.</p>
  <p>In my own server I don't have any view, my server is an ASP.NET Core Web API - I just want to send or receive messages from clients and do something in server, for example I want to receive an specific message from client1 and after process something send a specific message to client2, so I don't need any views and js file.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>    //Hub class is :
      public class ChatHub : Hub
      {
          public async Task SendMessage(string user, string message)
          {
              await Clients.All.SendAsync("ReceiveMessage", user,message);
          }
      }</code></pre>
  </div>
  </div>
  </p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>//and js is :
  
      
  var connection = new signalR.HubConnection("/chathub", { logger: signalR.LogLevel.Information });
  
  connection.on("ReceiveMessage", function (user, message) {
      var encodedMsg = user + " says " + message;
      var li = document.createElement("li");
      li.textContent = encodedMsg;
      document.getElementById("messagesList").appendChild(li);
  });
  
  document.getElementById("sendButton").addEventListener("click", function (event) {
      var user = document.getElementById("userInput").value;
      var message = document.getElementById("messageInput").value;
      connection.invoke("SendMessage", user, message).catch(function (err) {
          return console.error;
      });
      event.preventDefault();
  });
  
  connection.start().catch(function (err) {
      return console.error;
  });</code></pre>
  </div>
  </div>
  </p>