Real-Time Applications with ASP.NET SignalR


Real-Time Applications with ASP.NET SignalR

In today’s internet technologies, user expectations are increasing day by day. Especially real-time web applications that offer instantaneous data flow have become quite popular. Real-time communication is critically important in many areas, such as chat applications, live notification systems, instant data dashboards, and online games. At this point, the ASP.NET SignalR library developed by Microsoft for the .NET ecosystem offers great convenience. Developing real-time applications with ASP.NET SignalR makes it possible to produce solutions that are both high-performing and scalable.

What is ASP.NET SignalR?

ASP.NET SignalR is an advanced framework that allows bi-directional (two-way) persistent communication between the server and the client. It automatically manages different communication techniques such as WebSockets, Server-Sent Events, or Long Polling. It allows developers to focus solely on asynchronous messaging, without having to deal with complex connection management. Thanks to its lightweight API structure and strong performance, SignalR is ideal for real-time application requirements.

Advantages of Using SignalR

  • Real-time data synchronization
  • Comprehensive client and server support (.NET, JavaScript, etc.)
  • Simplified API and easy scalability
  • Automatic management of various transport protocols

Creating a Simple SignalR Application

Creating a SignalR Hub

At the center of SignalR applications is a Hub class. The Hub manages communication between clients and the server. In the example below, a SignalR Hub is defined for a simple messaging application:


using Microsoft.AspNetCore.SignalR;

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

Configuring Startup Settings

To activate SignalR on the server side, services must be added in Startup.cs or Program.cs:


public void ConfigureServices(IServiceCollection services)
{
    services.AddSignalR();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapHub<ChatHub>("/chatHub");
    });
}

SignalR Connection with HTML and JavaScript

On the client side, to connect with SignalR, you can add the JavaScript library and establish a connection:


<script src="https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/7.0.5/signalr.min.js"></script>
<script>
const connection = new signalR.HubConnectionBuilder()
    .withUrl("/chatHub")
    .build();

connection.on("ReceiveMessage", (user, message) => {
    const msg = user + ": " + message;
    document.getElementById("messages").textContent += msg + "\n";
});

connection.start().catch(err => console.error(err.toString()));

function sendMessage() {
    const user = document.getElementById("userInput").value;
    const message = document.getElementById("messageInput").value;
    connection.invoke("SendMessage", user, message);
}
</script>

Conclusion: The Right Choice for Real-Time Applications

Offering a real-time experience in modern web projects has almost become a necessity. Developing real-time applications with ASP.NET SignalR is one of the best solutions in terms of both performance and scalability. With its simple setup and powerful APIs, SignalR can be used for anything from simple notifications to complex multi-user games. You can easily bring your interactive real-time applications to life with ASP.NET SignalR.