For highly scalable reactive data graphs, it is recommended to move from tailing the MongoDB oplog to using redis-oplog. Grapher is fully compatible with redis-oplog.
To optimize performance, you can use $options.namespace inside a $filter() to create namespaced watchers. This prevents unnecessary reactivity across the entire collection by limiting updates to specific subsets of data.
Using $filter with namespaces
You can define the namespace directly in the query. This is useful for grouping updates (e.g., by a specific thread ID).
export default Messages.createQuery('messagesForThread', {
$filter({filters, options, params}) {
filters.threadId = params.threadId;
options.namespace = `thread::${params.threadId}`;
},
text: 1,
})
Embodying $filter server-side
To prevent clients from seeing or manipulating the filter logic, use embody in your .expose.js file. This encapsulates the filtering and namespacing logic on the server.
// query.expose.js
query.expose({
embody: {
$filter({options, filters, params}) {
filters.threadId = params.threadId;
options.namespace = `thread::${params.threadId}`;
}
}
})
Alternatively, embody can be a function that allows you to modify the query body directly:
// query.expose.js
query.expose({
embody(body, params) {
// Modify body here
}
})
export default Messages.createQuery('messagesForThread', {
$filter({filters, options, params}) {
filters.threadId = params.threadId;
options.namespace = `thread::${params.threadId}`;
},
text: 1,
})