angular2-actioncable icon indicating copy to clipboard operation
angular2-actioncable copied to clipboard

Reconnect to channel

Open c2ofh opened this issue 4 years ago • 1 comments

HI,

I have a subscription to get informed, when a channel subscription was disconnected. The subscription is really importend, so I want to reconnect. But I cam't find a way, to reconnect. The this.channel.connected()-Subscription is not getting fired.

init(token: string) {
  this.connect(token);
  this.subscribe(token)
}

connect(token) {
  this.channel = this.cableService.cable(environment.cable_url, {token: token}).channel('GuestappChannel')
  this.cable = this.channel.cable;
}

subscribe(token) {
  this.channel.connected().subscribe(() => {
    this.globals.log('Websocket connected', LogType.info, true)
  })
  
  this.channel.disconnected().subscribe(() => {
    this.globals.log('Websocket disconnected', LogType.error, true)
    this.connect(token);
  })
}

When I load the page I have the log.

[12:07:55-541]: Websocket connected

For testing, I peform this.cable?.disconnect(); after 20 seconds.

[12:08:07-896]: Websocket disconnected

But then I can't establish a new connection.

c2ofh avatar Oct 06 '21 10:10 c2ofh

Hi @c2ofh , I haven't had a chance to test out your example, but are you sure you aren't seeing it reconnect in the console? If you call your connect method against it's going to override your channel/cable variables and I think give you a completely new WS connection. The subscription to the 'connected' stream you initially established won't fire anymore because it was for the previous WS connection.

nolancaster avatar Oct 20 '21 17:10 nolancaster