SignalR in Blazor

SignalR is a real-time web communication library that allows server-side code to send asynchronous notifications to client-side web applications, and it can be used with Blazor to create interactive and responsive web applications. Here’s an overview of how you can use SignalR with Blazor.

Create your app

  1. Start Vs and select Create a new Project

2. Create a new project And Search Blazor Server App in search box

3.Choose a name of the app

4.Configure your new project

5. Select .net 6 in the Framework and click Create button

Project is Created Successfully

  1. In Solution Explorer, right-click the ChatApp project and select Manage NuGet Packages.
  2. In the Manage NuGet Packages dialog, confirm that the Package source is set to nuget.org.
  3. With Browse selected, type Microsoft.AspNetCore.SignalR.Client in the search box.
  4. In the search results, select the Microsoft.AspNetCore.SignalR.Client package. Set the version to match the shared framework of the app. Select Install.
  5. If the Preview Changes dialog appears, select OK.
  6. If the License Acceptance dialog appears, select I Accept if you agree with the license terms.
Add a SignalR hub

Create a Hubs  folder and add the following ChatHub class

Add services and an endpoint for the SignalR hub

  1. Open the Program.cs file.
  2. Add the namespaces for Microsoft.AspNetCore.ResponseCompression and the ChatHub class to the top of the file:

Create a Models folder and create class UserMessage

Add Razor component code for chat

  1. Open the Pages/Index.razor file.
  2. Replace the markup with the following code:

Run the app