Pushing data to SignalR clients from an API controller

Arguably the most valuable piece of our real-time API is pushing new answers to subscribed clients. In this section, we are going to learn how to do this. If we think about it, the ideal place to do this is in the questions API controller, which is where an answer is posted. So, when an answer is posted, we want SignalR to push the updated question with the saved answer to all the clients that are subscribed to the question. Let's implement this by carrying out the following steps in QuestionsController.cs:

  1. We'll start by referencing SignalR and our SignalR hub with using statements:
using Microsoft.AspNetCore.SignalR;
using QandA.Hubs;
  1. We are going to inject the context of the hub into the API controller using dependency injection:
[Route("api/[controller]")]
[ApiController]
public class QuestionsController : ControllerBase
{
private readonly IDataRepository _dataRepository;
private readonly IHubContext<QuestionsHub>
_questionHubContext;


public QuestionsController(
IDataRepository dataRepository,
IHubContext<QuestionsHub> questionHubContext
)
{
_dataRepository = dataRepository;
_questionHubContext = questionHubContext;
}

...
}

We have a class-level variable that is set as a reference to the hub context in the constructor. The IHubContext interface allows us to interact with SignalR clients.

  1. Let's enhance the PostAnswer method so that it pushes the question with the new answer to clients that have subscribed to the question:
[HttpPost("answer")]
public ActionResult<AnswerGetResponse>
PostAnswer(AnswerPostRequest answerPostRequest)
{
var questionExists =
_dataRepository.QuestionExists(
answerPostRequest.QuestionId.Value);
if (!questionExists)
{
return NotFound();
}
var savedAnswer =
_dataRepository.PostAnswer(new AnswerPostFullRequest
{
QuestionId = answerPostRequest.QuestionId.Value,
Content = answerPostRequest.Content,
UserId = "1",
UserName = "[email protected]",
Created = DateTime.UtcNow
});

_questionHubContext.Clients.Group(
$"Question-{answerPostRequest.QuestionId.Value}")
.SendAsync(
"ReceiveQuestion",
_dataRepository.GetQuestion(
answerPostRequest.QuestionId.Value));


return savedAnswer;
}

We get access to the SignalR group through the Group method in the Clients property in the hub context by passing in the group name. Remember that the group name is the word "Question", followed by a hyphen and then the question ID.

Then, we use the SendAsync method to push the question with the new answer to all the clients in the group. A handler called ReceiveQuestion will be invoked in the client, with the question being passed in as the parameter after we have got it from the data repository.

That's all the changes we need to make to our API controller in order to push updated questions to subscribed clients.

In the next section, we are going to define our real-time API endpoint.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.195.204