SignalR
Real-time Application Development with SignalR
Hello everyone. In this article, we will explore the SignalR library, one of the libraries used for developing real-time applications. Real-time web and mobile applications now constitute a significant part of our daily lives. Therefore, the popularity of real-time applications in the technology sector continues to thrive. This mechanism, used in various sectors such as instant messaging, currency tracking, match result tracking applications, and games, is actually based on WebSocket and RPC technologies. Hence, let's start by discussing WebSocket and RPC structures before delving into the topic of SignalR.
What are WebSocket and RPC?
WebSocket is an advanced technology that enables bidirectional interactive communication between the user's computer and the server. However, it does not directly utilize the request and response feature of the HTTP protocol. Therefore, it employs a method called Handshake for its own communication. To better understand bidirectional communication, let's examine it in comparison to the structure of the HTTP protocol.
As you can see, in an HTTP connection, a request is sent from the client to the server. The server generates a response corresponding to the incoming request and sends it back to the client. Once the response reaches the client, the connection terminates. In a WebSocket connection, on the other hand, a request goes from the client to the server, and in response, the server sends a handshake. This response, which we can refer to as a handshake, ensures the initiation of a secure, compatible, and effective connection between the server and the client. This connection persists until the client or server decides to disconnect. WebSocket technology varies based on the programming languages commonly used. For example, SignalR for .Net Core, Socket.IO or ws for Node.js, WebSockets or ws4py for Python, and Ratchet or Phpws for PHP can be utilized. Click here for all supported languages.
What is RPC?
RPC is a communication protocol that enables a client to invoke the code of a process on a remote server and receive the results. Let's examine its operation logic through a diagram.
- The client creates an RPC request to access the server. This request consists of the name of a specific operation on the remote server and the arguments being invoked. The client uses an RPC client to transmit the RPC request to the server.
- The RPC request generated by the client is organized by a specific RPC protocol. This protocol provides a language-independent structure for communication between the client and server (shown as Transport in the diagram).
- The server receives the incoming RPC request and parses the name of the operation and its arguments that need to be processed. The server performs the operation and generates the result.
- The server packages the result of the operation as an RPC response and sends it to the client. The packaged RPC response is also structured by a specific RPC protocol. This allows the client to understand the response.
- The client receives the RPC response from the remote server. The client parses the RPC response and uses the processing results, thereby completing the operation.
What is SignalR?
SignalR is an open-source .NET library created for developing real-time applications. Instead of using Request and Response, it employs the WebSocket feature. This allows for making server calls from the client side using JavaScript, and when there is a change in data, the server informs clients by invoking the relevant JavaScript method. As a result, bidirectional real-time communication starts within the application, eliminating the need to refresh the page. Let's examine the diagram below to understand the working logic of SignalR.
Let's assume our application is a chat application. Clients connect to our ChatHub to chat with each other. Within our ChatHub, we use the GetMessages method to retrieve messages in the chat room. When Client1 enters the chat room, it makes a call to access the Hub. This process completes the "handshake" mentioned above. After the connection is established, the client invokes the GetMessages method within ChatHub and subscribes to the ReceiveMessages method. Once the operations within the method on the Hub side are completed, it notifies the ReceiveMessages method on the client side. As a result, the client starts receiving messages from the chat room. When Client1 starts typing messages in the chat room, other clients see Client1's messages because they have subscribed to the ReceiveMessages method.
Advantages/Disadvantages of SignalR
We can list the advantages of SignalR as follows:
- It enables the automatic management of connections, handling situations such as connection interruptions and changing network conditions automatically.
- It assists in establishing real-time communication between the server and clients, making it suitable for scenarios like live chat applications, instant notifications, and interactive games.
- It has the ability to work across different client and server platforms. This flexibility is crucial for reaching a broader audience with your application.
However, its disadvantages include:
- Maintaining constant connections in real-time communication can increase server load. Therefore, high user traffic or complex operations can impact server performance.
- While SignalR generally works well with modern browsers, compatibility issues may arise with older browsers or customized browsers.
- When you start using SignalR, your application may become dependent on this technology, making it challenging to make changes later on.