Rails ActionCable "subscribe" race condition & fix

Occasionally, TurboStreams will attempt to hook up a stream but the stream “doesn’t work.” Rails logs show a broadcast to the identifier, but your client doesn’t receive anything…

This may happen when the ActionCable server receives an unsubscribe command rapidly followed by a subscribe command for the same channel identifier. If the subscribe is handled first, it is ignored (since the client is already subscribed). The unsubscribe then kills the channel and your stream is borked.

One scenario where you may encounter this: when two pages have a turbo-cable-stream-source with the same channel identifier and the user navigates from one to the other. The unsubscribe sent when the first TurboCableStreamSourceElement is removed will be followed by a subscribe when the TurboCableStreamSourceElement is loaded for the new page.

Submitted an ActionCable patch to address this client-side: ActionCable client ensures subscribe command is confirmed. by spinosa · Pull Request #41581 · rails/rails · GitHub