The purpose of this repository is to show the different SignalR implementations available for an OWIN WebApi.
NuGet Packages used:
- Microsoft.Owin.SelfHost
- Microsoft.AspNet.SignalR.SelfHost
- And all related dependencies
Follow tutorial here
In the Startup class, where the OWIN configuration is set, we must include the SignalR configuration to run in the same pipeline.
I have created 2 hubs purely to show the difference of how they will be used in the front-end.
There is nothing special in the setup between them, except for the IHubContext in the Generated Proxy Hub which is only used when calling from a controller. So the IHubContext can be removed safely without breaking the normal functionality.
The 2 front-end implementations are: (best explained here)
- Generated Proxy
- Manual Proxy (without generated proxy)
The major difference being:
- if you want to get a .js script generated by SignalR on page load via <script> tags (see index.html) (Generated Proxy)
- or only accessing the hubs when needed (Manual Proxy)
Other small differences include dynamic function calls vs event binding and invoking methods.
As the request to the ApiController has a HTTP Context, we need to get the context for the Hub's WebSocket protocol.
For ease of use I have created a static IHubContext which can be used when calling the Hub's methods from the controllers, as seen here
http://www.asp.net/signalr/overview/deployment/tutorial-signalr-self-host
http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client