DEV Community

Cover image for What is SignalR Core? How does it work?
Yunus Emre KAŞ
Yunus Emre KAŞ

Posted on

What is SignalR Core? How does it work?

Hello everyone, What is SignalR Core in the third part of the series How to communicate with React with SignalR Core using JWT? How does it work ? I will try to explain their subjects.

The subheadings of our topic will be as follows:

  • What is SignalR Core?
  • Where do we use it?
  • How does it work ?
  • Data Transfer Types
  • Hubs
  • Benefits

What is SignalR Core?

SignalR is an open source library with which you can create real time applications. It contains APIs that allow us to run a client-side code from the server side.

Where Do We Use It?

We can use the fast and simultaneous transfer of data wherever we want.

For example:

  • Games
  • Social Media (comments, messaging, notification)
  • Auction
  • Voting
  • Financial transactions

How Does SignalR Core Work?

Let’s open a little bit about the phrase “It allows us to run a client-side javascript code from the server side”. In a normal scenario, the client throws an HTTP Request to the server, the server processes the request and returns a Response to the client. But SignalR is not like that either. It happens but not exactly. 😂 So how is it happening?

The client sends an HTTP Request to the server, SignalR processes this request and by default determines the ideal transfer type. If the server and client support Websocket, Websocket protocol is selected as the transfer method. Because the best SignalR transfer method is the Websocket method.

So let’s say that our server or browser does not support Websocket, then what will happen? Can’t I use SignalR?

SignalR: Wait, it’s not over, nephew. You should first see transport methods.

SignalR Core Data Transport Methods

There are 3 different transfer types in SignalR Core, among which are the WebSocket, Server-Sent Events and Long Polling methods.

SignalR Core Transports

By default, SignalR Core proceeds by trying the above methods in order to find the best transfer protocol, and when it finds the best method, it establishes the connection through this transfer method. Most modern browsers support Websocket, which is the best method for SignalR.

What is WebSocket? How does it work?

websocket

It is a protocol that you can transfer bi-directional data over TCP between client and server. Like JWT, it is an RFC6455 standard. It is examined in two parts as Websocket, Handshake (Handshake / Agreement) and Data Transfer.

As can be understood from above, our first request is sent as an HTTP Request. This first request and the process of raising the connection is called handshake. View of a handshake request sent by the client:

GET /chat HTTP/1.1  
Host: server.example.com  
Upgrade: websocket  
Connection: Upgrade  
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==  
Origin: http://example.com  
Sec-WebSocket-Protocol: chat, superchat  
Sec-WebSocket-Version: 13  
Enter fullscreen mode Exit fullscreen mode

View of a handshake response sent by the presenter:

HTTP/1.1 101 Switching Protocols  
Upgrade: websocket  
Connection: Upgrade  
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=  
Sec-WebSocket-Protocol: chat
Enter fullscreen mode Exit fullscreen mode

Websocket connections are made over ws (WebSocket) and wss (WebSocket Secure / SSL) URI schemes.

If the conditions are suitable, the connection is upgraded and turns into a ws connection. Thus, a two-way asynchronous transfer path over TCP is formed between the server and the client.

Requirements for Websocket connection

WebSocket requires the server to use Windows Server 2012 or Windows 8 and .NET Framework 4.5. Latest versions of modern browsers generally accept Websocket connections. For detailed information.

How Does Server-Sent Events Work ?

server-sent-event

In this type, the client sends a request to the server, and the server returns a response containing Content-Type: text / event-stream. The connection is formed in this way. Event-stream starts listening to the server. An events consisting of a server are broadcast over the Event-stream. You can pull data with the EventSource object via Javascript. For more detailed information.

How Does Long Polling Work?

Long Polling

This method is an example of coyote. When the client makes a request, the server receives the request and holds it until there is a change. As soon as there is any change in the data requested, it returns to the client. The client makes a request again.

“What did this server do to you bro?” is a method you can tell.

Hubs

SignalR communicates between the client and the server through hubs. Hub, on the other hand, is a high-level pipeline that enables server and client to use each other’s methods. You can use parameter and model binding properties in these methods. SignalR provides two built-in hub protocols: a text protocol based on JSON and a binary protocol based on MessagePack. MessagePack usually creates smaller messages compared to JSON.

Browsers must support XHR level 2 to provide MessagePack protocol support.

Advantages

  • Automatically chooses the data transfer type
  • It is easy to understand and write because it is on .Net
  • Since it is open source, it is always open to development
  • It doesn’t use JQuery like the old SignalR, so it’s straight forward

Thanks you for reading. You can reach me on Linkedin for your comments and suggestions.

Top comments (0)