How can I keep attempting to connect to signalR in a safe way that allows a few hundred milliseconds (at least) in between attempts until the connection is established and if so, could someone provide a better way pf dealing with the various stages of connectivity to the web-sockets?
We are using signalR on a .NET backend and I am trying to use it to display real-time push notifications in an Angular 4 UI. It works great when it connects, but there is a problem where sometimes it takes 2-3 times to connect. I also get an error this.startSignalRListener is not a function
if I put it inside the catch block to try to reconnect that way.
Any suggestions would be greatly appreciated.
I am using "@aspnet/signalr-client": "^1.0.0-alpha2-final"
in package.json
Here is some of my code from my service class...
import { HubConnection } from '@aspnet/signalr-client';
@Injectable()
export class RealTimeService implements OnInit {
private hubConnection: HubConnection;
constructor() {}
ngOnInit() {
this.startSignalRListener();
}
public onConnection(): void {
const domain = this.state.domain;
this.hubConnection
.invoke('WatchSomething', [ 'abc.com' ])
.catch(err => console.error('signalR error: ', err));
this.hubConnection.on('some_thing', (res: any) => {
console.log('do stuff...', res);
});
}
public startSignalRListener() {
const connection = `www.realtimeapi.com`;
this.hubConnection = new HubConnection(connection);
this.hubConnection
.start()
.then(() => {
this.onConnection();
})
.catch(err => {
console.log('Error while establishing connection...');
});
}
}
How can I best reconnect when a connection fails? Any suggestion would really help me out as it fails often on the first try.