ably / ably-js

Javascript, Node, Typescript, React, React Native client library SDK for Ably realtime messaging service
https://ably.com/download
Apache License 2.0
316 stars 55 forks source link

Allow one concurrent connection for each sessionID #1525

Open karimpanacci opened 10 months ago

karimpanacci commented 10 months ago

Hi, I'm using ably and I don't know how to solve the following situation:

Every user in my app can have multiple sessions And every time a user connects with ably I use as clientId the userId

But I would like a user with the same session (for example if he opens two tabs in the same browser) not be able to connect to ably in case there is already an active websocket connection with the same session...

How can I do that?

┆Issue is synchronized with this Jira Task by Unito

sync-by-unito[bot] commented 10 months ago

➤ Automation for Jira commented:

The link to the corresponding Jira issue is https://ably.atlassian.net/browse/SDK-3973

ttypic commented 10 months ago

Hi @karim2012 ! Apologies we missed this issue.

Could you elaborate on the "session" you mentioned? Do you want to forbid users with the same userId from having more than one connection to Ably? What if it's not two tabs in the same browser; what if it's tabs in different browsers or devices?

If you want to avoid multiple Ably connections for two tabs in the same browser, I recommend looking into libraries that help with that, such as crosstab. Ably doesn't provide this functionality right now.

karimpanacci commented 10 months ago

Hi @ttypic My goal was to prohibit the connection to ably from multiple clients with the same session (session of my auth system), if someone opens tabs in two different browsers the sessionID would be different while if you open it from the same browser the sessionID would be the same...

But in fact I can use crosstab to prevent you from using multiple tabs at the same time... clearly it would be client-side protection and not server-side... but it won't be a problem...

ttypic commented 10 months ago

Thanks for the explanation! Now I understand. Server-side validation can be tricky, with many corner cases. Connections can be disconnected or suspended temporarily. I think client-side validation will be much easier

SimonWoolf commented 10 months ago

You could also consider running ably-js in a webworker, and have individual browser tabs communicate with that webworker instance