DEV Community

Cover image for Building Real-Time Web Applications with Blazor SignalR
Bhavin Moradiya
Bhavin Moradiya

Posted on

Building Real-Time Web Applications with Blazor SignalR

Blazor is a modern and powerful framework for building web applications using C# and .NET. With the release of Blazor Server and Blazor WebAssembly, developers now have two options for building web applications with Blazor. However, real-time web applications that require immediate updates and interactions between clients and servers can be a challenge. This is where SignalR comes into play.

SignalR is a real-time communication library that allows developers to add real-time web functionality to their applications. In this post, we will explore how to integrate SignalR into a Blazor application to create real-time web applications.

First, let's create a new Blazor Server application using Visual Studio or the .NET CLI. Then, add the SignalR package to the project using the NuGet package manager.

Next, create a new SignalR hub by adding a new class to the project that derives from the Microsoft.AspNetCore.SignalR.Hub class. This hub will handle incoming client connections and messages.

public class ChatHub : Hub
{
    public async Task SendMessage(string user, string message)
    {
        await Clients.All.SendAsync("ReceiveMessage", user, message);
    }
}
Enter fullscreen mode Exit fullscreen mode

The SendMessage method will be called from the client and will broadcast the message to all connected clients using the ReceiveMessage method.

In the Blazor component, add the SignalR JavaScript library to the project by adding the following script tag to the index.html file:

<script src="_content/Microsoft.AspNetCore.SignalR.Client/js/signalr.js"></script>
Enter fullscreen mode Exit fullscreen mode

Then, inject the SignalR hub into the component and establish a connection to the server:

@inject Microsoft.AspNetCore.SignalR.HubConnection hubConnection

@code {
    protected override async Task OnInitializedAsync()
    {
        await hubConnection.StartAsync();
    }
}
Enter fullscreen mode Exit fullscreen mode

Finally, add a form to the component that allows the user to enter a username and message. When the form is submitted, call the SendMessage method on the SignalR hub:

<form>
    <input type="text" @bind-value="username" />
    <input type="text" @bind-value="message" />
    <button @onclick="SendMessage">Send</button>
</form>

@code {
    private string username;
    private string message;

    private async Task SendMessage()
    {
        await hubConnection.SendAsync("SendMessage", username, message);
    }
}
Enter fullscreen mode Exit fullscreen mode

Now, when a user submits a message, it will be broadcast to all connected clients in real-time. This is just a simple example of how to use SignalR in a Blazor application, but the possibilities are endless.

SignalR is a powerful tool that can be used to create real-time web applications with Blazor. By integrating SignalR into a Blazor application, developers can create responsive and interactive web applications that provide a seamless user experience.

Top comments (1)

Collapse
 
bizcad profile image
Nicholas Stein

You can even get ChatGPT to help you out. https://www.youtube.com/watch?v=SP0A_Nf96ps&t=614s