What is real-time communication and why is it so important? Imagine you are waiting for an urgent message. Would you want to keep refreshing the page to see if it has arrived? You would rather be notified as soon as it arrives in your inbox. We can get new information from the server via real-time web technologies. We would have to refresh our page every time we wanted to see new chat messages or emails. We need to first understand what a traditional internet connection looks like in order to understand how and why it works.
HTTP is a stateless protocol that is used by most web pages. Most commonly, a request is sent by the server when we navigate to a URL. The server receives the request and packages the data needed to send back to the client. Finally, the server returns a response. This is how the page you are currently viewing is rendered. The server will then disconnect the client’s connection.
It is not stateless and doesn’t remember which client it was linked to. This is fine for many web apps, but it means that if the server contains more current data for the client than the client, it will not be able to re-send it to the client even though it was only connected to it. To check if there is any new content on the server, the client would need to send it again.
This led to the need for technologies that could establish a connection to allow updates to be sent as they become available. There are many options available for establishing real time connections. Today, we will be focusing on SignalR, an ASP.NET real-time library.
What is SignalR?
SignalR is an ASP.NET library that implements many common real-time connections technologies and chooses the one that is most appropriate based on the client it’s connecting to.
WebSocket is one of these technologies and is widely used for real-time connectivity. WebSocket is a persistent, bi-directional connection. This allows the client to send updates to the server immediately and vice versa. It can also transmit data simultaneously to both the server and client. This makes it ideal to use for chat applications and games.
SignalR uses Server-Sent Events to replace WebSocket in the event that it fails. SSE is the predecessor of WebSocket and establishes a persistent link. It is however only a one-way connection. Although the server can send new information to clients at any time, the client would need to request new data to send to the server. This would not work well for games, but it would be great for news feeds, data display, and other broadcasting applications.
Next is the Iframe. The Iframe is similar to SSE in that it establishes an unidirectional connection. It allows the server and client to send updated information to each other without the client having refresh the page. It creates iframe content blocks in the HTML that the server can push information into. This is particularly useful if you only need a section of your webpage to be updated and can make changes easily, such as ads, news feeds, or live reporting.
Long polling is the last option. It uses the classic idea that a request-response cycle is used and emulates a consistent connection. Instead of setting up a directional link, the client sends a request to the server. The client sends another request immediately after the server has responded. The server does not send data back but holds the request until it has more information. Once it has received the data, it sends it back. The client then processes it and sends another request to wait. It is not a flashy, efficient technology but it can be used as a backup in cases where other technologies aren’t available.
SignalR’s versatility and its inclusion in ASP.NET’s framework make it a popular tool for C# developers.
To learn more about different programming language and other frameworks, visit our website at