Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if ActionCable subscription request failed

I've recently deployed a Rails app, and have heard back from a few users that it doesn't work in their work environments. It's clear that Websockets are blocked for them.

I've hunted through the Rails documentation on this but can't find any information on how to go about detecting if this has happened. My best guess would be to send an AJAX request out for an ActionCable broadcast, and when that broadcast is not received after a certain timeout, to conclude that Websockets must be blocked.

Are there any easy answers here, perhaps already part of the Rails API, to determine Websocket connectivity?

like image 596
El Tea Avatar asked Nov 26 '25 11:11

El Tea


1 Answers

I have a workaround that isn't great, but is better than anything else I've seen. Rails provides no interface, but you can get down to the native WebSocket and handle the error.

consumer.subscriptions.create("ChatChannel", { ... });

consumer.connection.webSocket.onerror = function () {
  console.log('Websocket connection error!');
  // Error handling goes here. Fallback to polling, perhaps.
};

ActionCable will keep trying to reconnect, and this only grabs the first failure, but that's enough to cover many cases.

like image 55
Tim Jasko Avatar answered Nov 29 '25 15:11

Tim Jasko