Lucas Couto
Lucas Couto3w ago

Query arguments issue?

Came across an interesting problem that I was hoping to get some help with. Basically I have a query that receives a timestamp as an argument to control what information is returned. This argument will change in certain key situations. I know the default Convex behavior is to create a new subscription for each set of arguments, but I don't want all these subscriptions active because those will consume too much data. Is there a way to close unwanted subscriptions?
8 Replies
Convex Bot
Convex Bot3w ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
lee
lee3w ago
When the useQuery is unmounted (in the React sense), the subscription is dropped and Convex stops running it. Are you okay with a new query running every time the timestamp arg changes? That's what's happening; it's replacing the queries at previous timestamps
Lucas Couto
Lucas CoutoOP3w ago
It's fine running a new query. The problem is that all previous subscriptions for different timestamps are still active. Adding a console.log does print each previous timestamp. I don't really have the option to unmount the component. The main thing I'm trying to address with this timestamp is that I need this function to return only what actually changed between calls, not just a full result object every time.
lee
lee3w ago
Oh okay. Well you can get more control of the subscription lifetime by using the underlying watchQuery method https://docs.convex.dev/api/classes/react.ConvexReactClient#watchquery
lee
lee3w ago
Like
const convex = useConvex();
const useEffect(() => {
const watch = convex.watchQuery(...);
const unsubscribe = watch.onUpdate(() => {
const newValue = watch.localQueryResult();
// do something with new value
});
// put the unsubscribe function somewhere so you can call it later
});
const convex = useConvex();
const useEffect(() => {
const watch = convex.watchQuery(...);
const unsubscribe = watch.onUpdate(() => {
const newValue = watch.localQueryResult();
// do something with new value
});
// put the unsubscribe function somewhere so you can call it later
});
```
Lucas Couto
Lucas CoutoOP3w ago
Hey @Lee! We did end up going with watchQuery. It worked like a charm, thank you very much for your help 😁
lee
lee3w ago
That's awesome! If you want to write it up and share (maybe #show-and-tell ) , this sounds like an interesting pattern others could learn about
Tom Redman
Tom Redman3w ago
I didn’t know about this. Would love to see it @Lucas Couto!

Did you find this page helpful?