Sample application demonstrating use of SignalR from a WebAPI to update clients in real time.
cd <project_folder>
dotnet build
dotnet run
To access SignalR hubs from controllers, pass IHubContext<YourHub>
as a paramter to the controller's constructor and the dependency injector will take care of passing the hub's context during runtime
[Route("api/price")]
[ApiController]
public class ValuesController : ControllerBase
{
private IHubContext<DataHub> dataHubContext;
public ValuesController(IHubContext<DataHub> hubContext )
{
dataHubContext = hubContext;
}
// POST api/price
[HttpPost]
public IActionResult Post([FromBody] double updatedPrice)
{
dataHubContext.Clients.All.SendAsync("ReceiveData", updatedPrice);
return Ok(true);
}
}