What is SignalR?
SignalR is an open-source, real-time messaging ASP.NET framework developed by Microsoft that allows developers to build interactive and dynamic apps that instantly push data between servers and connected clients. SignalR provides a seamless and efficient way for developers to build real-time functionality, such as chat applications, collaboration tools, IoT device control, or real-time dashboards, providing an API that handles the underlying plumbing and simplifies the task of implementing real-time communication. It abstracts the complexity of managing connections by supporting multiple transport protocols, such as HTTP, WebSockets, Server-Sent Events, and Long Polling. SignalR uses Hubs for messaging, allowing servers to invoke methods on clients and vice versa. It supports features like automatic reconnection, scaling with backplanes (e.g., Redis or Azure SignalR Service), and is optimized for .NET applications.
What are the Features of SignalR?
Real-time communication
SignalR enables instant, two-way communication between servers and clients like web, mobile, and desktop apps. In addition to point-to-point communication, SignalR allows developers to broadcast messages to multiple clients or specific groups of clients, useful for scenarios like chat rooms or push notifications.
It handles numerous concurrent connections by using scalable backplanes like Redis, SQL Server, or Azure Service Bus. These backplanes distribute messages across multiple servers, enabling SignalR to support high traffic and concurrent users efficiently. Azure SignalR Service, in particular, is a managed solution that automatically scales connections based on demand, optimizing resource usage without manual configuration.
Platform support
SignalR primarily supports .NET applications and is compatible with .NET Framework, .NET Core, and .NET 5, 6, and 7. It provides SDKs for programming laguages of JavaScript, Java, and .NET clients, allowing real-time functionality across web, mobile, and desktop platforms. SignalR’s JavaScript client enables integration with browser-based applications, while Xamarin and .NET clients are supported for mobile development. Azure SignalR Service extends support for scalable cloud-hosted applications, making SignalR flexible for various platforms within the .NET ecosystem.
Client / Server connection:
SignalR maintains a persistent connection between the client and server using WebSockets when available, falling back to Server-Sent Events or Long Polling as needed. This persistent connection enables real-time updates with minimal network overhead, ensuring fast data transmission. SignalR includes automatic reconnection capabilities, attempting to re-establish the connection after network interruptions, server restarts, or connection timeouts. This process is seamless for the user, automatically resuming communication, thus providing enterprise-grade reliability and responsiveness even in unstable network environments. Additionally, SignalR manages connection lifecycles and includes customizable reconnection logic, allowing developers to handle specific scenarios, such as executing custom code on reconnection or notifying users of connection status changes.
Architecture:
SignalR operates on a client-server architecture with a hub-and-spoke pattern for efficient real-time communication. The SignalR server manages active connections, routing messages and handling events like client connection and disconnection.
Hubs: Act as high-level API endpoints, handling Remote Procedure Calls (RPCs) between clients and the server. Clients can invoke methods on Hubs, while Hubs can broadcast messages to specific clients, groups, or all connected clients, enabling efficient communication routing.
Transport Layer Abstraction: SignalR abstracts various transport methods (WebSockets, Server-Sent Events, and Long Polling) to automatically select the best available protocol based on server and client capabilities.
Authentication and Authorization: Supports ASP.NET Identity and other providers, enabling connection and method-level authentication for secure, role-based access to Hub methods.
SignalR Hosting Modes:
Self-Hosting Allows for on-premises or cloud hosting, where developers manage server configuration, scalability, and maintenance.
Azure SignalR Service A managed Microsoft service that handles scalability, availability, and network configuration, letting developers focus solely on application functionality.
Customization and Extensibility:
SignalR is highly extensible and allows developers to customize and extend its functionality. It provides hooks for customizing connection management, message serialization, and other aspects of the communication process. SignalR also provides flexible error handling, allowing developers to implement the appropriate logging or recovery mechanisms. For more in-depth customization, SignalR's SignalR on GitHub is openly available, allowing developers to explore or modify the core framework.
How SignalR works
1. Persistent Connections
SignalR enables real-time communication through persistent connections between the client and the server. This differs from the traditional request-response model where the client must initiate a request to receive data. With SignalR, the server can send data to the client whenever it needs to, and vice versa.
2. Transport Mechanisms
SignalR uses several transport mechanisms to ensure real-time communication. It automatically chooses the best available transport method, depending on what both the server and the client support:
WebSockets communication transport method, it provides full-duplex channels over a single TCP connection. If supported, SignalR will prefer WebSockets.
Server-Sent Events a one-way communication method where the server pushes updates to the client using HTTP protocol.
Long Polling: This is the fallback mechanism where the client repeatedly polls the server for updates. The server holds the request open until there is new information, reducing latency compared to traditional polling.
3. Hubs
SignalR uses a concept called "Hubs" to handle communication between the server and clients. A Hub is a high-level pipeline that allows the server to call methods on connected clients and vice versa. Clients can invoke methods on the server using the Hub, and the server can push updates or messages to connected clients.
Server-side Hubs: These are C# classes where you define methods that can be called by clients. You can also send messages to specific clients or groups of clients.
Client-side Hubs: These are JavaScript (or other languages) implementations that receive calls from the server or invoke server-side Hub methods.
4. Groups and Users
SignalR allows sending messages to specific groups or individual users. You can organize clients into groups and send messages to all clients within a group. Similarly, you can send messages to a specific user identified by a unique user identifier.
5. Scalability
SignalR can scale to handle large numbers of connections. For scenarios where your application needs to support many clients, SignalR can be integrated with a backplane like Azure SignalR Service, Redis, or SQL Server. These backplanes help distribute messages to all connected clients across a web farm or multiple servers.
6. Connection Management
SignalR manages client connections and reconnections automatically. If a client gets disconnected (e.g., due to network issues), SignalR will try to reconnect automatically. It also handles connection lifecycle events, allowing you to execute custom logic when a client connects or disconnects.
7. Security
SignalR supports authentication and authorization for managing connections. You can restrict access to Hubs or specific methods within Hubs to authenticated users or users with specific roles.
Workflow Overview
Client connects to the server:A connection is established between the client and the server using the best transport method available.
Client and server communicate: Both can invoke methods on each other via the Hub.
Messages are transmitted in real-time: Using the selected transport method, messages are pushed between the client and server.
Connection Management: SignalR handles reconnections, disconnections, and load balancing automatically.
SignalR Limitations
Setting up SignalR can be difficult and time-consuming, requiring significant configuration of server infrastructure for real-time communication. Developers new to SignalR library often struggle with its steep learning curve, and mastering the APIs and best practices is essential but challenging. Without deep expertise, creating a scalable, high-performance solution for high-traffic applications can be challenging.
SignalR requires developers to manage the backend infrastructure, including servers and network resources. This can be costly, requiring monitoring and maintaining server health, ensuring sufficient bandwidth for message delivery, and handling potential scaling needs.
Latency issues in High-Traffic Scenarios: While SignalR performs well for most real-time tasks, high-traffic or large data payloads can lead to latency, especially in distributed environments or with fallback transports like Long Polling. SignalR’s default setup can handle around 5,000 WebSocket connections per server in optimized environments (like those with a dedicated backend). However, real-world applications with high user counts often reach this limit quickly, especially in chat, gaming, or IoT contexts. Once the server nears capacity, response times degrade, and new client connections may experience significant latency or fail to connect.
It is primarily designed for use with .NET applications and is limited in its compatibility with other platforms and frameworks. While libraries and plugins are available for other languages, the level of support and functionality may vary, presenting developers unfamiliar with the .NET ecosystem.
Security and Network Overhead: Persistent connections over WebSockets or Server-Sent Events require additional security considerations, as these connections remain open. SignalR implementations need adequate network monitoring, security, and validation to handle vulnerabilities such as unauthorized access and session hijacking.
SignalR Alternatives
SignalR vs PubNub comparison
Both PubNub and SignalR are popular platforms for building real-time solutions, including chat and messaging applications. While the two have many similarities, including abstracting the underlying protocol away from the developer, they also have key differences, so developers should consider carefully when choosing between them.
One key difference is in the architecture between the two solutions:
Diagram comparing the high-level architecture of SignalR (left) with PubNub (right)
SignalR is deployed to servers you are responsible for (or managed with the SignalR Azure Service), and you need to ensure that the solution will scale appropriately. You then implement the required messaging routing logic using the flexible and powerful SignalR API. Clients send and receive messages using the appropriate .NET, Java, or JavaScript API.
PubNub is a managed and hosted solution that will scale automatically as your solution grows. Message routing is handled through PubNub’s flexible channel mechanism, and messages are exchanged with clients using any of the 50+ platform SDKs supported. PubNub also supports Functions, allowing applications to integrate custom message logic (such as translation or sentiment analysis) without worrying about scaling or deployment concerns.
Customers looking for a solution to host themselves, either on-prem or on-cloud, and have a strong background with .NET or specific message routing requirements should consider SignalR. Be sure to plan for the ongoing maintenance costs associated with any self-hosted solution at scale.
For most scenarios, PubNub will be the better choice when developing any real-time solution. It is a ready-made solution that scales automatically, globally, and infinitely without hosting anything yourself. PubNub provides a simple but flexible message routing mechanism through its channel concept, which can be extended using PubNub Functions to intercept and modify messages as required. PubNub supports far more client front-ends integrations; for example, SignalR requires mobile device clients to use Xamarin/Maui, whereas PubNub supports native Android/iOS development and around 50 other SDKs APIs.
If you want to learn more about PubNub or decide between a SignalR vs. PubNub deployment, please contact our dedicated support team or email our developer relations team at devrel@pubnub.com.