What is a SignalR Hub? Basic Concepts and Usage
What is a SignalR Hub? Basic Concepts and Usage
SignalR Hub is the most important building block of the SignalR library offered by Microsoft for developers of real-time web applications. To the question of what is SignalR Hub; it is a center that facilitates two-way communication between client and server, managing messaging and data flow. It is quite common to use SignalR Hub for needs such as instant notifications, chat, or live data flow in modern web applications.
What is SignalR Hub and What Does It Do?
SignalR Hub provides an API that enables function calls between client and server. Via the hub, the server can send messages to clients and clients can also send messages to the hub. One of the most important advantages here is that the developer does not have to deal with low-level technologies such as WebSocket, Long Polling, or SSE. SignalR Hub automatically manages connection management and messaging.
Basic Usage of SignalR Hub
A simple example of how to create and call a SignalR Hub:
using Microsoft.AspNetCore.SignalR;
public class ChatHub : Hub
{
public async Task SendMessage(string user, string message)
{
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
}
In the C# code above, a SignalR Hub named ChatHub is defined. Through this hub, messages are sent to all connected clients. After setting up the SignalR Hub, it can be used on the front-end side (for example, with JavaScript) as below:
const connection = new signalR.HubConnectionBuilder()
.withUrl("/chatHub")
.build();
connection.on("ReceiveMessage", (user, message) => {
console.log(user + ': ' + message);
});
connection.start()
.then(() => connection.invoke("SendMessage", "Kullanıcı1", "Merhaba, SignalR Hub nedir?"));
Features and Advantages of SignalR Hub
The main advantages of using SignalR Hub are as follows:
- Multi-way connection: Provides bi-directional communication with multiple clients.
- Offers automatic reconnection and message ordering management.
- Easily integrates real-time interactions with backend applications.
Conclusion: Real-Time Applications with SignalR Hub
The question of what is SignalR Hub has become a critical issue for live data transmission and interaction in modern web applications. For example; SignalR Hub can be used in many different scenarios such as instant chat, notification systems, live scores, or games. In this article, you can find the main points you need to know about the basics of SignalR Hub, how to use it, and its advantages.

Yorum Gönder