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

Connection heartbeat #85

Open
jwoertink opened this issue Nov 10, 2023 · 3 comments
Open

Connection heartbeat #85

jwoertink opened this issue Nov 10, 2023 · 3 comments

Comments

@jwoertink
Copy link
Collaborator

We have this WebSocketPinger that pings the client every 3 seconds, but there's not really a way on the backend to know that things are still alive. I have a case where when a user joins a chat, we add their ID to an array, and when they leave, we remove their ID from that array. There's an edge case though where they join on desktop, then join on mobile, then leave desktop and now their ID is no longer in the array. This is a tricky problem to solve with how our app works. What would be nice is when the ping happens, we can see they're still in chat and just re-add them to the array.

To do this, I think instead of passing the socket in to the pinger, we just pass the connection. The connection has access to the socket and can still send the message, but then we can also call a method on the connection like ping. If you override this method in your connection class, you'll receive that ping on the backend too. And it shouldn't be a breaking change since the logic will all be the same.

@jwoertink
Copy link
Collaborator Author

Actually, I just started to implement a test and realized that this also won't solve my issue because I was confusing Connection and Channel in my head 😝 I still think this might be helpful for some though, just doesn't solve what I was wanting.

@rmarronnier
Copy link
Contributor

I'm going to piggyback on this issue :

  • How reliable is Cable::Connection#closed?, ie, can there be false negatives ? false positives ?

If I need to check if there are active connections with a specific token, I couldn't find a better way than :

    Cable.server.connections.values.any? { |connection| connection.identifier == secret_token && !connection.closed? }

It gets really ugly and hackish, when checking also if a channel was subscribed to :

   if active_auth_connection = Cable.server.connections.values.find { |connection| connection.identifier == secret_token && !connection.closed? }
      Cable.server.@channels.values.flatten.sum.any? { |channel| channel.connection.connection_identifier == active_auth_connection.connection_identifier }
    else
      false
    end

Maybe, we could benefit from public and documented class methods global helpers like :

Cable::Server
def self.active_connections_for(token : String) : Array(Cable::Connection)

def self.subscribed_channels_for(token : String) : Array(Cable::Channel)

wdyt?

@jwoertink
Copy link
Collaborator Author

I like that @rmarronnier 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants