title: How can I keep trying to subscribe to signalR from Angular service if connection fails?
body: <p>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?</p>
  
  <p>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 <code>this.startSignalRListener is not a function</code> if I put it inside the catch block to try to reconnect that way.</p>
  
  <p>Any suggestions would be greatly appreciated.</p>
  
  <p>I am using <code>"@aspnet/signalr-client": "^1.0.0-alpha2-final"</code> in package.json</p>
  
  <p>Here is some of my code from my service class...</p>
  
  <pre><code>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 =&gt; console.error('signalR error: ', err));
  
          this.hubConnection.on('some_thing', (res: any) =&gt; {
              console.log('do stuff...', res);
          });
      }
  
      public startSignalRListener() {
          const connection = `www.realtimeapi.com`;
  
          this.hubConnection = new HubConnection(connection);
  
          this.hubConnection
              .start()
              .then(() =&gt; {
                  this.onConnection();
              })
              .catch(err =&gt; {
                  console.log('Error while establishing connection...');
              });
      }
  }
  </code></pre>
  
  <p>How can I best reconnect when a connection fails? Any suggestion would really help me out as it fails often on the first try. </p>