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();
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().
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.
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.
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"?
The documentation you reference uses npm package "@microsoft/signalr" whereas yours uses "@aspnet/signalr"
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With