Server side collection transformations in publish method

My scenario: I want to display whether a user is currently online to other users. So I am storing the last “ping” time on the user object. For data privacy reasons, I do not want to disclose this information directly to other users.

So I set up a collection transformation function and bound it to the find() method in a publish() method. This transformation compares the ping date with the current date, removes the ping field from the user document and adds an “online” field instead.

This works if I use the find() on server side only. But if I am using the find() in the publish() method, the transformation is not called. I found out that transformation actually happens if fetch() is called, which is understandable. But in my case it means that it is not working. A client-side transform is not an option, as this would require the ping information to be passed to the client.

Is there any other way to hook into the fetch() mechanism on server side, so the transformation can be done when the client actually retrieves the data using fetch()?

Transforms are not supported in publish. See:

However, you can perform any manipulation of published data (adding/deleting/changing fields) using this.added and the likes. There is an example at the end of this article. It is quite complex and you can probably find a simpler one.


Thanks for your answer. I expected it won’t be that easy…

There are good packages for that like:

If privacy is an issue maybe review that package to see if it fits your specific requirements.

Transformation further does not seem to be a good thing for this. It makes more sense to observe users on the server or to run a cron every few minutes.

On storage: Just add a field:
user: {
lastOnline: date here
online: boolean here

and just publish only the boolean.

Thanks a lot for pointing me to that package. I remember that I’ve once seen it, but there are so many packages that you sometimes get lost in hyperspace. I will definitely use this package!

Good luck with it, saves you quite some time! @mizzao makes some nice packages.

You can use package for publish transformations.

1 Like

Here’s another package for server-side transformations:

1 Like

Great, thanks for pointing me to the additional packages.