signalr
  1. signalr-best-practices-for-performance

Best Practices for Performance - SignalR Best Practices

Introduction

SignalR provides real-time web functionality to applications using three different modes: WebSockets, Server-Sent Events (SSE), and Long Polling. However, like any technology, SignalR comes with its own set of best practices for optimal performance.

This document outlines some of the best practices to be followed when using SignalR in your applications.

Syntax

The syntax for using SignalR in your application is as follows:

// SignalR Hub Definition
public class MyHub : Hub
{
    public void SendMessage(string message)
    {
        Clients.All.SendAsync("ReceiveMessage", message);
    }
}

// JavaScript client connection
var connection = new signalR.HubConnectionBuilder()
    .withUrl("/myHub")
    .build();

connection.on("ReceiveMessage", function (message) {
    console.log(message);
});

connection.start();

Example

To test the performance of SignalR, we can create a simple chat application, where users can send messages to each other in real-time.

// SignalR Hub Definition
public class ChatHub : Hub
{
    public void SendMessage(string user, string message)
    {
        Clients.All.SendAsync("ReceiveMessage", user, message);
    }
}

// JavaScript client connection
var connection = new signalR.HubConnectionBuilder()
    .withUrl("/chatHub")
    .build();

connection.on("ReceiveMessage", function (user, message) {
    var li = document.createElement("li");
    li.textContent = user + " says: " + message;
    document.getElementById("messagesList").appendChild(li);
});

connection.start();

Output

The output of the chat application would be a real-time list of chat messages sent by users.

Alice says: Hi there!
Bob says: How are you?

Explanation

When using SignalR in your applications, it is important to keep in mind the following best practices for optimal performance:

  1. Use a CDN for SignalR. This reduces the load on your server and ensures that your users always get the latest version of SignalR.
  2. Use WebSockets if possible. This allows for real-time communication with lower latency and fewer network requests.
  3. Use the Hub APIs to send and receive messages. This simplifies the client-side code and ensures that messages are delivered efficiently.
  4. Use server-side groups to reduce network traffic. This allows you to send messages to a specific set of clients instead of broadcasting to all clients.
  5. Use the PersistentConnection API instead of the Hub API for high-throughput scenarios. This is more efficient for sending large amounts of data.

Use

SignalR can be used in various scenarios such as real-time chat applications, dashboard updates, game applications, etc.

Important Points

Some important points to keep in mind when using SignalR in your applications:

  1. SignalR should not be used for long-running operations. Long-running operations should be handled using a separate service or background job.
  2. SignalR should not be used for secure communication. Security should be handled at the application level using SSL/TLS, authentication, and authorization.
  3. SignalR should be used only for real-time communication. It should not be used as a replacement for HTTP requests for non-real-time data.

Summary

In summary, SignalR is a powerful tool for adding real-time functionality to your applications. By following the best practices outlined above, you can ensure optimal performance and a seamless user experience.

Published on: