nolancaster / angular2-actioncable

Integrate ActionCable with Angular2+
25 stars 16 forks source link

Reconnect to channel #13

Open c2ofh opened 2 years ago

c2ofh commented 2 years ago

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.

nolancaster commented 2 years ago

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.