Recommended webhook listener pattern
I have some data in a table
conversations
that I'm querying using an action as I'm running several queries and returning a new object type. I'm also using functions from Node runtimes, which can't be run on normal queries/mutations.
However, I want to run this action every time there's an update to conversations
.
In my webapp, I'm wondering whether I should:
a) convert the action into a pure query
or
b) query conversations
, include it in a useEffect dependency, and call the action within the callback
Which approach would be recommended from a best practices/efficiency standpoint?3 Replies
@punn the short answer is, the support we intend to provide for this soon is data-dependent action/mutation triggering. this is the final missing primitive convex needs for true workflow (in addition to schedules, which we already have)
for now, pure queries and mutations are always going to be better when they're possible given performance / cost / caching / robustness / composability and so on
if not possible due to long runtimes, side effects, or complex npm dependencies, you can use approach (b), which is sort of having the app "advocate" for advancing the workflow. it's not ideal, but it sort of the necessary evil for now, unless
(c) you're okay to just have a scheduled function drive things forward every minute or whatever. if a bit of a delay is okay, this is a little cleaner than having to litter your app with
useEffect
just for this purpose
(c) is useful in case you want to ensure the next step proceeds even if the app disconnects and is no longer around to advance the backend state
(d) which could be cleanest still if this pattern is okay with you, is in the mutation that actually updates conversation
, call scheduler.runAfter(0, ...)
to transnationally schedule the action to run on the new datareference here: https://docs.convex.dev/api/interfaces/server.Scheduler#runafter
Interface: Scheduler | Convex Developer Hub
server.Scheduler
Since I have an action being run frequently to sync conversations (on every chat conversation change), the best approach seems to be minimizing node dependencies so pure queries can be used and updated the UI on the WS channel
haven't had the chance to try the scheduler, mainly because I'm not sure where it would fit in my messaging flow. curious if you have any resources/references for understanding how schedulers can be used to support various apps