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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With