Skip to content

cyberpirate92/SignalR-WebAPI-Demo

Repository files navigation

SignalR-WebAPI-Demo

Sample application demonstrating use of SignalR from a WebAPI to update clients in real time.

How to run

cd <project_folder>
dotnet build
dotnet run

Using SignalR hubs in Controllers

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

Example:

[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);
    }
}

Demo video:

Video

About

Sample application demonstrating use of SignalR from a WebAPI to update clients in real time

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published