{"id":520,"date":"2013-06-21T14:19:54","date_gmt":"2013-06-21T14:19:54","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=520"},"modified":"2013-07-05T11:33:00","modified_gmt":"2013-07-05T11:33:00","slug":"self-hosting-signalr","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/self-hosting-signalr\/","title":{"rendered":"Self hosting SignalR"},"content":{"rendered":"<p>I&#8217;ve covered the steps needed to host SignalR within a web application, but we&#8217;ve also seen we can connect to SignalR through a .NET client (in my case a simple Console app.). So next we&#8217;ll cover self-hosting SignalR. <\/p>\n<p><strong>Requirements<\/strong> <\/p>\n<p>At the time of writing you&#8217;ll need to install the following NuGet packages from Package Manager Console in Visual Studio<\/p>\n<p>Install-Package Microsoft.AspNet.SignalR<br \/>\nInstall-Package Microsoft.Owin.Hosting -Pre<br \/>\nInstall-Package Microsoft.Owin.Host.HttpListener -Pre<\/p>\n<p>You can delete the scripts folder added by SignalR, we&#8217;re really just after the assemblies<\/p>\n<p>Microsoft.AspNet.SignalR.Core<br \/>\nMicrosoft.AspNet.SignalR.Owin<br \/>\nMicrosoft.Owin.Host.HttpListener<br \/>\nMicrosoft.Owin.Hosting<br \/>\nOwin<\/p>\n<p>So if you feel like only deploying the minimal assemblies you can remove anything else installed from those packages. <\/p>\n<p><em>Edit: Since writing the above a couple of things seems to have changed on those packages, you&#8217;ll now need Microsoft.Owin assembly and WebApplication (in the below code) is now WebApp.<br \/>\n<\/em><\/p>\n<p><strong>The Server<\/strong><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class MyHub : Hub\r\n{\r\n   public void Send(string message)\r\n   {\r\n      Clients.All.sendMessage(message);\r\n   }\r\n}\r\n\r\ninternal class MyStartUp\r\n{\r\n   public void Configuration(IAppBuilder app)\r\n   {\r\n      \/\/ the line below works fine with the console client but not the browser client\r\n      \/\/ app.MapHubs();\r\n      \/\/ use the following to enable the browser client to work with the server\r\n      HubConfiguration config = new HubConfiguration();\r\n      config.EnableCrossDomain = true;\r\n      config.EnableDetailedErrors = true;\r\n      app.MapHubs(config);   \r\n   }\r\n}\r\n\r\nclass Program\r\n{\r\n   \/\/ just for demo purposes, will &quot;beep&quot; every 5 seconds\r\n   private static Timer timer = new Timer(o =&gt;\r\n   {\r\n      var context = GlobalHost.ConnectionManager.GetHubContext&lt;MyHub&gt;();\r\n      context.Clients.All.sendMessage(DateTime.Now.ToLongTimeString() + &quot; beep&quot;);\r\n   });\r\n\r\n   static void Main(string&#x5B;] args)\r\n   {\r\n      using (WebApplication.Start&lt;MyStartUp&gt;(&quot;http:\/\/localhost:9100&quot;))\r\n      {\r\n         \/\/ just for this demo\r\n         TimeSpan ts = TimeSpan.FromSeconds(5);\r\n \t timer.Change(ts, ts);\r\n\r\n\t Console.WriteLine(&quot;Server is running, Press &lt;Enter&gt; to stop&quot;);\r\n\t Console.ReadLine();\r\n      }\r\n   }\r\n}\r\n<\/pre>\n<p>I&#8217;ve listed all the code above, it&#8217;s all pretty self-explanatory. The URL in the start method is the URL for the server and port etc. The timer is solely used to send out a message every 5 seconds for out client to see. I&#8217;ve listed the code for a simple client below (see my post on creating a .NET client for SignalR if you need more info. on this).<\/p>\n<p><strong>Client<\/strong><\/p>\n<p>A simple .NET Console client&#8230;<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nstatic void Main(string&#x5B;] args)\r\n{\r\n   HubConnection connection = new HubConnection(&quot;http:\/\/localhost:9100&quot;);\r\n   IHubProxy proxy = connection.CreateHubProxy(&quot;myHub&quot;);\r\n   connection.Start().ContinueWith(task =&gt;\r\n   {\r\n      proxy.Invoke(&quot;Send&quot;, &quot;.NET Client Connected&quot;);\r\n\r\n      proxy.On&lt;string&gt;(&quot;sendMessage&quot;, (message) =&gt;\r\n      {\r\n         Console.WriteLine(message);\r\n      });\r\n   });\r\n   Console.Read();\r\n}\r\n<\/pre>\n<p>If, you wish to access the server hub via JavaScript then the following is a simple example of the required JavaScript, note we create the proxy to the hub in the same way to the way we do this in the console client.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;script&gt;\r\n   $(function () {\r\n      var connection = $.hubConnection(&quot;http:\/\/localhost:9100&quot;);\r\n      var proxy = connection.createHubProxy('myHub');\r\n\r\n      proxy.on(&quot;sendMessage&quot;, function (message) {\r\n         $('#discussion').append('&lt;li&gt;&lt;strong&gt;' + message + '&lt;\/strong&gt;&lt;\/li&gt;');\r\n      });\r\n\r\n      connection.start().done(function () {\r\n         proxy.invoke(&quot;Send&quot;, &quot;Webpage client&quot;);\r\n      }).fail(function () {\r\n         $('#discussion').append('&lt;li&gt;&lt;strong&gt;Failed to start&lt;\/strong&gt;&lt;\/li&gt;')\r\n      });\r\n   });\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>Note: The at least one proxy.on event handler <strong>must<\/strong> be set before the connection.start or you will be able to send messages but not receive them (see http:\/\/www.asp.net\/signalr\/overview\/hubs-api\/hubs-api-guide-javascript-client#establishconnection for more information).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve covered the steps needed to host SignalR within a web application, but we&#8217;ve also seen we can connect to SignalR through a .NET client (in my case a simple Console app.). So next we&#8217;ll cover self-hosting SignalR. Requirements At the time of writing you&#8217;ll need to install the following NuGet packages from Package Manager [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[47],"tags":[],"class_list":["post-520","post","type-post","status-publish","format-standard","hentry","category-signalr"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/520","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=520"}],"version-history":[{"count":7,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/520\/revisions"}],"predecessor-version":[{"id":522,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/520\/revisions\/522"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}