How to prevent publish from re-running when user logs in/out?


#1

Here is my use case:
I need my publish function to rerun whenever user logs-in, but I want to control it through subscribe(). The reason is that I want to display a spinner until the subscription is ready.
The issue is that, whenever user logs in, the publish function is rerun automatically server-side (see doc here) before I can call subscribe() client-side, so my collection begins to change before I can display the spinner.
Any idea?


Caching subscription ids in minimongo to vastly simplify client-side querying
#2

the publish method runs because the subscribe method is being triggered. If you want to subscribe to data that is the same between users logging in and out, don’t include the Meteor.user in your subscribe, because it is reactive and will trigger a re-run


#3

@jamgold, have a look at this link. The publish function reruns whenever user logs in, without any subscribe.


#4

Nice, thanks for posting this, I never noticed this.

Maybe richsilv:dumb-collections does what you need?


#5

Thanks @jamgold, this looks interesting, I will have a look.


#6

I use it for content which is not reactive and hardly ever updates. This gives me the opportunity to keep the content in a MongoDB collection, but the client only needs to download it once.


#7

I don’t find the part of the Meteor source code that reruns publish functions whenever user logs in/out. Please help!
@sashko?


#8

Here it is.


#9

Probably something I did not understand, but why not use subs-manager?


#10

As far as I understand, subs-manager is designed to help you keeping subscriptions alive and having them closed automatically in the future (it is basically a subscription queue).

Here I am talking about a Meteor core feature: when user logs in/out, all publish functions are rerun without the client being informed at all (no stop/ready events are triggered client-side).

This can have devastating effects: for example, if you got CPU-intensive reactive operations plugged on your database, having subscription rerun without previously stopping them will crash your app.

Workarounds to this issue are ugly. They imply:

  1. Hacking into your login library (splendido:useraccounts in my case) to create the equivalent of a “beforeLogin” hook. Indeed, reactive change to Meteor.userId() comes too late: the re-publication process has already begun when it gets called.

  2. Rerun your subscribe function client-side whenever user logs in/out. This means you get 2 calls to the publish function: the automatic one (useless) and yours. I don’t know the exact consequences, but I would definitely like to avoid this.