Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActionCable: FE Disconnection Reason #46952

Closed
DosCervezas opened this issue Jan 10, 2023 · 4 comments
Closed

ActionCable: FE Disconnection Reason #46952

DosCervezas opened this issue Jan 10, 2023 · 4 comments

Comments

@DosCervezas
Copy link

Sorry for breaking your issue pattern but I thought it would be a bit of overkill for my simple issue.

Expected behavior

I would like to specify a specific disconnect reasons, that the FE could catch and react on it.
Therefore I defined Standard error handled it in the handle_open method

class Connection < ActionCable::Connection::Base
...
def handle_open
  super
rescue MultiConnectionError => exception
  close(reason: exception.message, reconnect: false) if websocket.alive?
end
...
end

It works like a charm, the message is broadcasted to the FE

image

But I cannot catch the reason.. after debugging I found the culprit

The reason is not passed to the close of the websocket
image

Actual behavior

I would like to catch the reason, why the connection is disconnected. Of course I could do it on the serverside but the reason seems to me a valid other approach.

System configuration

Rails version:
Rails 7.0.4
"@rails/actioncable": "^7.0.4"

Ruby version:
ruby 3.1.2p20

@hahmed
Copy link
Contributor

hahmed commented Jan 14, 2023

Hey @DosCervezas, could you upload a repo to demonstrate the issue? This makes it so much easier to take a look into the issue, thank you!

@hahmed hahmed added the more-information-needed When reporter needs to provide more information label Jan 14, 2023
@DosCervezas
Copy link
Author

thanks for your reply hahmed but I rerolled my own branch days ago sorry
but the lines I mention to pass the reason to the close event of the websocket are in this file available
https://github.com/rails/rails/blob/main/actioncable/app/javascript/action_cable/connection.js

the reason is logged in line 138 but not passed further. Thats my problem.. and globaly overwritting the log function to get the reason is not the right way I guess...

@rails-bot rails-bot bot removed the more-information-needed When reporter needs to provide more information label Jan 16, 2023
@hahmed
Copy link
Contributor

hahmed commented Feb 2, 2023

I'm not sure that's the way to go, I mean you can use the logger for logging too, but maybe you want something like this https://guides.rubyonrails.org/action_cable_overview.html#server-side-components-channels-exception-handling

Handle the exception then broadcast the error message to another channel that displays errors in a modal/alert/banner or something? (been a while since I used action cable, so I would need to test it)

@hahmed
Copy link
Contributor

hahmed commented Feb 3, 2023

I did a quick test and managed to broadcast an error message to another channel, something along the lines of ErrorAlertChannel.broadcast_to("error", exception.message) this will allow the subscriber to handle the error client side.

I'm closing this because I don't think this is an issue.

@hahmed hahmed closed this as completed Feb 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants