Creating the SignalR real-time API endpoint

Before we can use our SignalR real-time API, we need to enable SignalR and define the path for the endpoint. Let's carry out the following steps in Startup.cs:

  1. We'll start by referencing our SignalR hub with the following using statement:
using QandA.Hubs;
  1. Add SignalR to our ASP.NET app by using the AddSignalR method in the services parameter in the ConfigureServices method:
public void ConfigureServices(IServiceCollection services)
{
...

services.AddSignalR();
}
  1. The next step is to configure the request pipeline so that we can pass SignalR requests to our SignalR hub:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHub<QuestionsHub>("/questionshub");
});
}

So, SignalR requests to the /questionshub path will be handled by our QuestionHub class.

That's it—nice and simple! We are ready to interact with our SignalR real-time API from our frontend React app, which we'll do in the next section.

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

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