Home » Home » SignalR in C#


SignalR is a real-time application framework developed by Microsoft that allows for bi-directional communication between a server and a client over a persistent connection. With SignalR, developers can create highly interactive and responsive web applications, making it a powerful tool for building modern web applications. In this article, we will explore the basics of SignalR in C# and how it can be used to create real-time applications.

What is SignalR?

SignalR is an open-source real-time application framework developed by Microsoft. It allows for real-time communication between a server and a client, making it possible to create highly interactive web applications. SignalR provides a set of libraries and APIs that developers can use to build real-time applications in C#.

How does SignalR work?

SignalR uses web sockets as the underlying technology to enable real-time communication between the server and the client. Web sockets provide a bi-directional, full-duplex communication channel over a single TCP connection, which allows for low-latency, high-throughput communication between the server and the client.

SignalR abstracts away the complexity of web sockets and provides a simple programming model that allows developers to build real-time applications without having to worry about the underlying plumbing.

SignalR in C#

SignalR is available as a NuGet package that can be installed in any C# project. Once installed, developers can use SignalR to build real-time applications using the familiar syntax and constructs of the C# programming language.

SignalR provides a set of APIs that developers can use to create real-time hubs. A hub is a class that exposes a set of methods that can be called by the client. When a client connects to the hub, it establishes a persistent connection to the server. The client can then call methods on the hub, and the hub can call methods on the client.

Here is an example of a simple SignalR hub in C#:

using Microsoft.AspNetCore.SignalR;

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

In this example, we define a hub called ChatHub that has a method called SendMessage. When a client calls this method, it sends a message to all connected clients using the Clients.All.SendAsync method.

To use this hub in a client application, we can create a SignalR connection and call the SendMessage method:

var connection = new HubConnectionBuilder()
.WithUrl("https://localhost:5001/chatHub")
.Build();

await connection.StartAsync();

await connection.InvokeAsync("SendMessage", "Alice", "Hello, World!");

In this example, we create a SignalR connection to the ChatHub hub, start the connection, and then invoke the SendMessage method with the parameters “Alice” and “Hello, World!”.

Conclusion

SignalR is a powerful tool for building real-time applications in C#. With its simple programming model and support for web sockets, it makes it easy to create highly interactive and responsive web applications. Whether you are building a chat application, a game, or a financial dashboard, SignalR can help you create a real-time experience for your users

Related Posts

Leave a Reply

%d bloggers like this: