> For the complete documentation index, see [llms.txt](https://dailyjournal.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dailyjournal.gitbook.io/notes/web-frameworks/asp.net-core/signalr.md).

# SignalR

{% embed url="<https://dotnet.microsoft.com/en-us/apps/aspnet/signalr>" %}

### What is SignalR?

* SignalR provides the consistent or persistent connection between the server and the client.
* It is used to develop real-time applications.
* it bridges the gap between the server and the client so the information can flow in and out in real-time without any restriction or barrier.
* it is bi-directional meaning server can send data to the client and vice versa.
* Eg. Chat Applications, Social Applications.

### How does SignalR works?

* SignalR uses only one of the four components / technologies.

  **`Web sockets`**` ``->`` `**`Event Source`**` ``->`` `**`Forever Frame`**` ``->`` `**`Long Polling`**&#x20;
* First it goes to Web Sockets if not available then Event Source and so on as it goes further towards the end the communication efficiency between server and client decreases.

### Why use SignalR?

* implementing SignalR means getting all the 4 components in a single package.
* if any of the 4 components gets any updates, SignalR is gonna handle those and make the application work as it was expected to do.
* SignalR is an abstraction means we don't have to go into details how it works.
* it is cross-application compatible and easy to use.
* it can be used to make broadcast groups.
* scalable

> AJAX
>
> * AJAX provides the server validation and it can also check for the new data.
> * It is not an open/persistent connection .

### Long Polling

* AJAX requests the server for the new data if it got any and keep on requesting.
* While Long Polling will request the server once for any new data that the client request, the server will keep the request as soon as it gets the data it will respond.
* Disadvantage
  * simulates the persistent connection&#x20;
  * one way means server cannot act on its own behind there is always request from client

{% embed url="<https://www.youtube.com/watch?v=VccZkG_qWeM>" %}

### Forever Frame

* it uses IFrame&#x20;
* server pushes the data to the client&#x20;
* Disadvantages
  * high memory usage
  * client should support IFrames
  * one-way persistent connection means from server to client.

### Event Source

* it uses content-type (text/event-stream)
* one-way persistent connection means server to client&#x20;
* asynchronous
* think of it as subscribe option when you subscribe to the channel you get the updates not the another way around

### Web sockets

* bi-directional persistent connection
* multiuser
* full -duplex - communication between server and client can happen at the same time
* asynchronous&#x20;

### SignalR

* inherits the Hub class
* RPC (Remote Procedure Call)
  * server can call the functions at client side and vice versa&#x20;
* Hubs vs Persistent Connections&#x20;
  * Hubs are at higher level than Persistent Connections
  * both of these are classes which can be inherited
  * Persistent Connection get instantized when connection is established and stays persistent until connection is closed. While Hubs are instantized for every new request.

{% embed url="<https://www.youtube.com/watch?v=vnKDZqP1gRA>" %}

### Installing

{% code title="StartUp.cs" %}

```
app.mapSignalR();
ConfigureAuth(app);
```

{% endcode %}

{% code title="\_Layout.cshtml" %}

```
<script src="~/Scripts/jquery.signalR-2.2.0.min.js></script>
<script src="~/signalr/js"></script> //this will be created 

@RenderSection("scripts", required: false)
```

{% endcode %}

{% code title="SignalR.js" %}

```
$.connection.hub.start()
    .done(function(){ 
        console.log("")
        $.connection.myHub.server.Announce("Connected");
    })
    .fail(function(){ alert() })
    
    
@.connection.myHub.client.Announce = function(message){alert(message)}
```

{% endcode %}

```
public class MyHub: Hub{
    public void Announce(string message){
        Clients.All.Announce(message);
    }
}   
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://dailyjournal.gitbook.io/notes/web-frameworks/asp.net-core/signalr.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
