Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor: Allow only one client instance per user

Tags:

meteor

Is there a way to prevent the user to open the Meteor website in two tabs or browsers?

I am trying to create a card game in Meteor, and I would like to "catch" if the user tries to open the page in another tab or browser and just warn him that another client instance is already opened and maybe give him/her the option to logout in all instances, etc.

I've googled already, but no luck.

like image 603
Predrag Stojadinović Avatar asked Nov 20 '25 05:11

Predrag Stojadinović


1 Answers

If the users are required to log in:

  • Save the connection to a Connections collection, along with user id. (or the roomId to the profile itself, depending on your schema)
  • Remove the connection on disconnect
  • on each new connection, see if the user id already exists in the Connections collection

If the users do not have to log in:

  • Save the connection to a Connections collection (along with user IP)
  • Remove the connection on disconnect
  • on each new connection, see if the IP address already exists in the Connections collection

An example of using onConnection in the wild (note I don't disallow multiple connections, but that'd just be another 5 lines of code): https://github.com/mattkrick/react-tac-toe/blob/master/server/gameState.jsx#L13

The docs (ie how to capture ip address): http://docs.meteor.com/#/full/meteor_onconnection

like image 169
Matt K Avatar answered Nov 22 '25 04:11

Matt K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!