Meteor resets subscriptions on login/logout

Why does meteor reset subscriptions on login / logout?
It would be quite useful have some control over this situation if anyone knows how / if this is possible.

My application has a lot of custom subscriptions because its actually backed by an SQL database rather than a Mongo database.
Today looking at the logs I realised that my subscriptions are being completely refreshed when a user logs in or out. I have subscriptions that are not dependent on the user Id but meteor seems to want to delete each subscription and create new instances of them.

After some investigation I eventually found that this is intended behaviour indicated by the comments in liveddata_server.js where it says:

Sets the current user id in all appropriate contexts and reruns all subscriptions

Currently my new users subscribe to chat. Create an account, then subscribe to chat (and loads of other things) again.

Its actually not a problem for the backend due to the way we have merged our data but its putting unnecessary load on the front end

I’d need to see some code, but if your subscriptions are in an autorun block that uses Meteor.userId() or any of the login status methods this would be the cause. I don’t think subscriptions depend on userId directly

Example here:

Video of example here:

The video shows 2 subscriptions
CollectionSub and CustomSub

CollectionSub returns a cursor. On every login/logout onStop is called (technically removing all the data from the subscription) then the cursor is returned (adding all the data back to the subscription)
The server has done all the work of tearing down and recreating the subscription but on the client you see no change.

CustomSub has a latch to show the data is changing. On every login/logout onStop is called (removing all the data from the subscription). When latch == 0 there is a call to this.added which adds the data. As a result on the client you can see the subscription data only appears on the 3rd login or logout

1 Like