How can I detect?


#1

Hi,

I have a route defined with FlowRouter, how can I know if a user has loaded the route multiple times in different tabs, browsers, devices?
I want to restrict the page to only one instance open per user to save resources because the page is kind of expensive.

Any easy way to do it?

protectedRoutes.route('/game/:id', {
  name: 'game',
  action() {
    BlazeLayout.render('Layout_protected', { main: 'Page_game' });
  }
});

Thanks


#2

My first thought Is perhaps storing a session variable when the template created and remove when destroyed… On create check if it exists first.


#3

It does not sound bad. I am considering it but I am open to other suggestions server side.


#4

I guess your publications are expensive.

So in your expensive publication function, you could do something like:

const usersUsingPublication = new Set()

Meteor.publish("myexpensivepublication", function() {
  if(usersUsingPublication.has(this.user) {
      // throw error, or do not publish anything with:
     this.ready()
  } else {
    // ...do your expensive publication
    usersUsingPublication.set(this.userId)
    // remove the user id if this publication stops
    this.onStop(()  => usersUsingPublication.delete(this.user))
  }
});

BUT, if you want to scale your app, you have to find out how to make this page less expensive. Spending some time to figure this out might be more valuable (and user friendly).


#5

Thanks,
this solution looks better than using session because
if I use Session to limit the page loads, users may find a way to clear Session.