C# tutorials > Frameworks and Libraries > ASP.NET Core > SignalR for real-time communication (hubs, clients)
SignalR for real-time communication (hubs, clients)
This tutorial explores SignalR, a powerful library for building real-time web applications with ASP.NET Core. We'll cover the fundamental concepts of SignalR, focusing on hubs and clients, and demonstrate how to create a basic real-time application.
Introduction to SignalR Hubs
SignalR hubs are the core of a SignalR application. They act as a central point for clients to connect and communicate with each other and the server. Think of a hub as a class that exposes methods that clients can call, and that the server can call on connected clients.
Creating a SignalR Hub
This code defines a simple ChatHub
. It inherits from Hub
, the base class for all SignalR hubs. The SendMessage
method is an asynchronous method that clients can call to send messages. Clients.All.SendAsync
sends the message to all connected clients, calling a method named ReceiveMessage
on each client with the user
and message
as arguments.
using Microsoft.AspNetCore.SignalR;
namespace SignalRChat.Hubs
{
public class ChatHub : Hub
{
public async Task SendMessage(string user, string message)
{
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
}
}
Registering the Hub in Startup.cs
To use the hub, you need to register it in your Startup.cs
file. In the ConfigureServices
method, add services.AddSignalR()
. In the Configure
method, use app.UseEndpoints
to map the hub to a specific URL (in this case, /chatHub
). This URL will be used by clients to connect to the hub.
// In ConfigureServices method:
services.AddSignalR();
// In Configure method:
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<ChatHub>("/chatHub");
});
Client-Side Implementation (JavaScript)
This JavaScript code demonstrates a simple client-side implementation. It first creates a connection to the hub using signalR.HubConnectionBuilder
, specifying the URL of the hub. The connection.on
method registers a handler for the ReceiveMessage
event. When the server sends a message to the clients (via Clients.All.SendAsync
), this handler will be executed, appending the message to a list on the page. The connection.start()
method starts the connection. Finally, an event listener is attached to the "send" button, which calls the SendMessage
method on the hub when clicked.
const connection = new signalR.HubConnectionBuilder()
.withUrl("/chatHub")
.build();
connection.on("ReceiveMessage", (user, message) => {
const msg = user + ": " + message;
const li = document.createElement("li");
li.textContent = msg;
document.getElementById("messagesList").appendChild(li);
});
connection.start().catch(err => console.error(err.toString()));
document.getElementById("sendButton").addEventListener("click", event => {
const user = document.getElementById("userInput").value;
const message = document.getElementById("messageInput").value;
connection.invoke("SendMessage", user, message).catch(err => console.error(err.toString()));
event.preventDefault();
});
Client-Side Implementation (HTML)
This HTML code provides the structure for the chat interface. It includes an unordered list (ul
) to display the messages, input fields for the user and message, and a button to send the message. It also includes the necessary JavaScript files: signalr.js
(the SignalR client library) and chat.js
(the client-side code shown above).
<ul id="messagesList"></ul>
<div>
<input type="text" id="userInput" placeholder="User"/>
<input type="text" id="messageInput" placeholder="Message"/>
<button id="sendButton">Send</button>
</div>
<script src="/js/signalr/dist/browser/signalr.js"></script>
<script src="/js/chat.js"></script>
Concepts Behind the Snippet
The key concepts are:
Real-Life Use Case Section
SignalR is ideal for real-time applications like:
Best Practices
Consider these best practices when working with SignalR:
Interview Tip
When asked about SignalR in an interview, be prepared to discuss the following:
When to use them
Use SignalR when:
Memory footprint
SignalR's memory footprint can be influenced by the number of concurrent connections and the amount of data being transmitted. Consider the following to minimize memory usage:
Alternatives
Alternatives to SignalR for real-time communication include:
Pros
Pros of using SignalR:
Cons
Cons of using SignalR:
FAQ
-
What is the difference between SignalR and WebSockets?
WebSockets is a low-level protocol for full-duplex communication, while SignalR is a higher-level library that builds on top of WebSockets (or other transports) and provides features like automatic reconnection, transport negotiation, and a simple API for sending and receiving messages.
-
How do I handle authentication in SignalR?
You can use standard ASP.NET Core authentication mechanisms (like JWT tokens or cookies) to authenticate users in your SignalR hub. You can then access the user's identity through the
Context.User
property in your hub methods. -
How do I scale a SignalR application?
You can scale a SignalR application by using a backplane, such as Redis or Azure SignalR Service. A backplane allows multiple server instances to coordinate and distribute messages to all connected clients.