signalr
  1. signalr-persistent-connections

Persistent Connections - SignalR Persistent Connections

SignalR provides a framework for creating real-time applications by enabling two-way communication between the client and the server. SignalR persistent connections allow a long-running connection between a client and server, meaning that communication can be maintained for an extended period and will not be terminated after a request is sent.

Syntax

Creating a SignalR persistent connection involves the following code syntax:

public class MyConnection : PersistentConnection
{
    protected override Task OnConnected(IRequest request, string connectionId)
    {
        // Code to execute when a client connects
    }

    protected override Task OnReceived(IRequest request, string connectionId, string data)
    {
        // Code to execute when data is received from the client
    }

    protected override Task OnDisconnected(IRequest request, string connectionId, bool stopCalled)
    {
        // Code to execute when a client disconnects
    }
}

Example

Here is an example of how to create and use a SignalR persistent connection:

public class ChatConnection : PersistentConnection
{
    protected override Task OnConnected(IRequest request, string connectionId)
    {
        // Add the current connection ID to the group that represents the chat room.
        return Groups.Add(connectionId, "chatRoom");
    }

    protected override Task OnReceived(IRequest request, string connectionId, string data)
    {
        // Broadcast the received message to all clients in the chat room.
        return Connection.Send("chatRoom", $"{connectionId}: {data}");
    }
}

Output

When the above example is implemented, any message sent by a client will be broadcasted to all the clients in the chat room in real-time.

Explanation

Persistent connections facilitate communication between clients and servers by allowing long-term connections to be made. This eliminates the need to open and close connections every time data is exchanged, thereby saving resources and reducing network overhead. In the example above, a SignalR persistent connection is created to facilitate real-time communication in a chat room. When a client connects, they are added to the "chatRoom" group, and any message sent by them is broadcasted to all other clients in the group.

Use

Persistent connections can be used in a wide range of real-time applications, such as chat rooms, stock market applications, online gaming, and more. With SignalR, you can easily create and utilize a persistent connection to enable real-time communication between the client and server.

Important Points

  • SignalR persistent connections enable long-running connections between clients and servers.
  • SignalR provides a framework for real-time web applications that leverage persistent connections.
  • Persistent connections eliminate the need to open and close connections every time data is exchanged.
  • Persistent connections increase efficiency and reduce network overhead by maintaining a connection throughout the lifetime of an application.

Summary

SignalR persistent connections are a powerful tool for enabling real-time web applications. By allowing long-running connections, they can facilitate efficient communication between clients and servers, while also reducing network overhead. The above examples and explanations highlight the ease with which SignalR persistent connections can be implemented in practical, real-world applications.

Published on: