Azure SignalR Service provides REST API to support server to client communication scenarios, such as broadcasting. You can choose any programming language that can make REST API call. You can post messages to all connected clients, a specific client by name, or a group of clients.
In most cases, REST is faster. And the SignalR is faster than the REST.
Objective: Use SignalR for notification between Web API, and TypeScript/JavaScript based Web App, where Web API and the Web App is hosted in different domain. Enabling SignalR and CORS on Web API: Create a standard Web API project, and install the following NuGet packages: Microsoft.
Take a look at the video from this blog post. It explains exactly how you can use WebAPI with SignalR.
Essentially, Web API + SignalR integration consists in this class:
public abstract class ApiControllerWithHub<THub> : ApiController
where THub : IHub
{
Lazy<IHubContext> hub = new Lazy<IHubContext>(
() => GlobalHost.ConnectionManager.GetHubContext<THub>()
);
protected IHubContext Hub
{
get { return hub.Value; }
}
}
That's all. :)
SignalR is actually already incorporated into WebAPI source vNext (4.1).
If you use not the RTM build, but instead grab a build off Codeplex, you'd see there is a new project there called System.Web.Http.SignalR
which you can utilize. It was added a couple of days ago with this commit - http://aspnetwebstack.codeplex.com/SourceControl/changeset/7605afebb159
Sample usage (as mentioned in the commit):
public class ToDoListController : HubController<ToDoListHub>
{
private static List<string> _items = new List<string>();
public IEnumerable<string> Get()
{
return _items;
}
public void Post([FromBody]string item)
{
_items.Add(item);
// Call add on SignalR clients listening to the ToDoListHub
Clients.add(item);
}
}
If you do not want to switch to vNext for now, you could always just use that code for reference.
This implementation is very similar (a bit more polished, includes tests etc.) to what Brad Wilson showed at NDC Oslo - http://vimeo.com/43603472
Here is a video showing an integration of the two technologies http://channel9.msdn.com/Events/TechDays/Belgium-2013/25 and here there is a NuGet package for the integration https://www.nuget.org/packages/Microsoft.AspNet.WebApi.SignalR/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With