Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure reconnect logic for SignalR within an angular 8 project?

I'm using

    "@aspnet/signalr": "^1.1.4",

in Angular 8 project.

Unfortunately it appears the documentation is out of date?

https://learn.microsoft.com/en-us/aspnet/core/signalr/javascript-client?view=aspnetcore-3.1

The documentation claims there is a method

withAutomaticReconnect()

to be used as

const connection = new signalR.HubConnectionBuilder()
    .withUrl("/chatHub")
    .withAutomaticReconnect()
    .build();

However, when I attempt to enter this in I get

Property 'WithAutomaticReconnect' does not exist on type 'HubConnectionBuilder'

Is this a documentation out of date issue? Or am I misunderstanding the configuration?

Here is my exact code BEFORE trying to add this method

    private _hubConnection: HubConnection;

...

        this._hubConnection = new HubConnectionBuilder()
        .withUrl(this.myAPIUrl + '/myHub', { accessTokenFactory: () => tokenFromCache })
        .configureLogging(signalR.LogLevel.Error)
        .build();
like image 433
jbooker Avatar asked Dec 14 '19 03:12

jbooker


People also ask

When does SignalR stop trying to reconnect?

By default, SignalR will try and reconnect immediately, then 2 seconds later, then again after 10 seconds and then after 30 seconds. At that time, if the connection could still not be reestablished, it will stop trying to reconnect: You can configure the backoff period by passing an array of retry delays to the call to withAutomaticReconnect().

How to configure the JavaScript client for SignalR to automatically reconnect?

We can configure the Javascript client for SignalR to automatically reconnect using the withAutomaticReconnect () method on HubConnectionBuilder. However, it is not the default behavior and we need to call this method explicitly. We can customize the behavior of withAutomaticReconnect () by providing an argument.

How to install SignalR on Visual Studio Code?

We need to install the SignalR library for the client side. To do that, we are going to open the Angular project in the Visual Studio Code and type the following command in the terminal window: That is it for now regarding the client side. Let’s switch back to the server-side project and create a new folder Models.

How does SignalR duplicate events?

I beleive what's happening is the SignalR creates another socket under the same key, and when a message is sent to the specified key - it is broadcasted to all connections with same key hence duplicates the events. My question is - Is there a way to "reconnect" to an existing socket instead of "making an additional connection"?


Video Answer


2 Answers

The documentation you reference uses npm package "@microsoft/signalr" whereas yours uses "@aspnet/signalr"

like image 72
tagada Avatar answered Oct 16 '22 23:10

tagada


you need a more up to date version, I believe it is version 3, just run the following command inn your terminal I am using it with a react front end.

npm install @microsoft/signalr
like image 35
jfletch Avatar answered Oct 17 '22 00:10

jfletch