I had a chance to work with the latest SignalR library for ASP.NET Core 2 which is in Alpha as I write this. My customer is looking at Azure and I thought it would be good to showcase a PaaS workload using Azure Functions and Azure App Services. You can see the application on Github here.
The Hub class
I needed to create a class that inherits from Hub and register an endpoint for clients to connect to when they want to start listening for messages. The Startup class is used to set up the endpoint "/gps".
The LocationController receives the request from the Azure Function so it needs to be able to broadcast to the SignalR clients. Ideally, it would have been great to configure my Function to broadcast to all clients itself but my Function is .NET 4.6.1 so it was a whole lot simpler just to call an API endpoint in my website. Once Functions are fully .NET Core I'll see if I can make that happen but for now I'll just take the hit of an extra call to the website's LocationController.
In this example we are using Web Sockets but that is transparent to the controller. In fact all the transport and protocol concerns are taken away and all I have to care about is the object I want to send which is nice. Everything seems clean and simple with the new SignalR code.
You can see here how the IHubContext is used to invoke the client and trigger a a "Send" method...
In the browser I need to set up a connection to my SignalR hub and listen for calls to "send" at which point a function is triggered, "track", that pulls out the pilot name, latitude and longitude from the message and sets the new location for the map's pin. I support a couple of pilots which is just the same track but with different pilot name. The GetMap() function is just a callback used to start updating the map once the Bing Map loads.
In case anyone has a similar problem I will make a quick mention of a problem I had. Not looked into it that much but I think a change in ASP.NET Core 2 affected the way in which file providers are dealt with so it took a little searching to figure it out how to inject an IFileProvider into my controller. I had to implement the following in Startup.