I only have very few pub/sub left but two of them are currently in the FlowRouter.subscriptions function.
As that function gets executed a lot of times, I feel it’s not the right place. IMO, a (general - meaning its needed all the time) sub to a pub should only be executed once during startup.
What is the best practice for such a general sub to a pub? Where did you put it in your project and when is it executed during startup (should be as early as possible IMO as one of them for example is checking if the backend system is up & running or currently down for maintenance or other reasons).
I might use a method for stuff like this where ur just checking on or pulling data arbitrarily like that. U can use a subscription I guess but a method might serve ur use case better.
In those two cases I need the reactivity. As I’ve written above, one is for the Status collection which I use at the backend to force a logout to all users when I need to take the Backend server down or make a specific feature “temporarily unavailable” due to some severe errors in it. A method unfortunately won’t help.
I need to figure out which part of my code base is only called during startup.
The problem with this .subscription function is that the first line:
const userId = Meteor.userId();
is reactive. Unfortunately this part is called when the user is inactive too long and the network connection is closed. Which then leads to the:
Error: You can't use reactive data sources like Session inside the .subscriptions method!
You don’t need a (new) publication nor a method for this. You can logout all users by deleting all login tokens from the server. Since the meteor user account is already reactive, they will be automatically logged out once you removed the login tokens
This is what I do to kick out all users when the Backend server has to go down:
// set empty array in order to log out all userId sessions
Meteor.users.update(
{ _id: userId },
{ $set: { 'services.resume.loginTokens': [], 'status.online': false } },
);
I assume this is what you meant.
It doesn’t address the other use cases of the Status collection though
This was the one that I missed. In our app, we have a single hash value in a settings collection which is updated whenever a setting or a feature toggle has been updated. When an update happens, the single hash value is updated which is published to all clients. Then an update to that single hash value triggers a method call to fetch either the settings or the feature toggles.
Similar to @minhna above, we have a component dedicated for this at the very root of our App structure. The only re-rendering that will happen to that component is if a new hash is published from the server.
How do you organize your code? How can you extract the value from the subscription to be used on your components? How do you manage changes on those values?
For us, using React, it will most likely be inside a component.
Identities collection holds information on different DNA tests under one users account and is available in a drop-down in the navbar. That and other information is retrieved with a findOne.
All this is on Blaze, I can’t comment on React or Vue or any other.
Thanks for that suggestion. In my specific use case the backend is on another server and at that moment the remote connection might not be done yet (though both are in the same virtual private network in the cloud).