SignalR is great for cases where:
1) You need a fallback in case websockets aren't available
AND
2) You have control over the implementation of the client (there's a specific protocol that has to be followed by the client)
The easiest way I can think of, from what you tell us about the project (limited control of the implementation of the client, websocket server implementation only, Web API) is Microsoft.WebSockets.
You can install Microsoft.WebSockets from NuGet and be up and running with a websocket server in minutes. There a few tutorials out there (ex.: https://dejanstojanovic.net/aspnet/2014/june/database-change-notifications-in-aspnet-using-websocket/), but essentially:
1) make sure your IIS has websockets enabled
2) create a Web API controller that handles websocket requests. Ex.:
using System;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using Microsoft.Web.WebSockets;
public class SomeController : ApiController
{
// Has to be called something starting with "Get"
public HttpResponseMessage Get()
{
HttpContext.Current.AcceptWebSocketRequest(new SomeWebSocketHandler());
return Request.CreateResponse(HttpStatusCode.SwitchingProtocols);
}
class SomeWebSocketHandler : WebSocketHandler
{
public SomeWebSocketHandler() { SetupNotifier(); }
protected void SetupNotifier()
{
// Call a method to handle whichever change you want to broadcast
var messageToBroadcast = "Hello world";
broadcast(messageToBroadcast);
}
private static WebSocketCollection _someClients = new WebSocketCollection();
public override void OnOpen()
{
_someClients.Add(this);
}
public override void OnMessage(string message)
{
}
private void broadcast(string message)
{
_someClients.Broadcast(msg);
SetupNotifier();
}
}
}
The SetupNotifier()
method should contain logic that catches the change you want to react upon. broadcast(string message)
(can be renamed of course) contains the logic that "returns" data to the client(s) - this example sends the same message to all clients.
Make sure to test this with a proper websocket client (there are Chrome extenstions for this, if you want the ease of use) - you can't do ws://
requests in a browser as-is.