At MaestroQA we have been doing an architectural refactoring to improve the performance of our app. One of the goals is to identify the parts of the application that don’t really need reactivity, and replace existing pub/sub code in those parts with Method calls for fetching data.
However, using Method calls for fetching data has certain disadvantages:
Switching an existing codebase from using pub/sub to using Methods usually requires a lot of refactoring, especially on the client-side.
Data retrieved from Methods is not merged into Minimongo, which means other parts of the front-end cannot make use of this data.
Each Method call will trigger a separate request from the front-end, resulting in a separate invocation on the back-end. This means if a particular Method is called repeatedly with the same arguments, we will end up wasting resources processing and sending the same data multiple times.
Without pub/sub, the changes made to documents during a mutation Method are not sent to client. It will be impossible for the client to always reflect these changes accurately, because there can be server-only logic that cannot be simulated with optimistic updates on the client-side. We can refactor the Method to manually return the changed documents and merge them to Minimongo, but that requires a lot of custom code.
To solve these problems, we created pub-sub-lite, a Meteor package that provides:
Easy-to-use helpers (
Meteor.subscribeLite) that can be used in place of
Meteor.subscribe. No other change is necessary. The original pub/sub will be converted to a Method call and the result data will be merged to Minimongo automatically.
Meteor.subscribeLitealso provides a handle that reactively returns true once data has arrived, which means existing front-end code can be kept intact!
Enhanced Methods (
- Automatically merge result data into Minimongo.
- Automatically send server-side document changes made during a Method invocation to the client-side caller and merge these changes to Minimongo, without using any pub/sub.
- Automatically deduplicate unnecessary repeated Method calls, with customizable cache duration that can be set globally or individually per Method.
- Result data caching: Method result data is cached and can be synced with Minimongo (when data contains documents) before being returned when there is a cache hit.
pub-sub-lite will be useful when:
- You have used pub/sub predominantly in your codebase and now want to switch certain parts to using Methods, without doing a lot of refactoring.
- You want to leverage the benefits of enhanced Methods (caching, Minimongo merging, mutation updates emitting).
More information about the package can be found at https://github.com/adtribute/pub-sub-lite.
We hope to receive your feedback to further improve the package!