#Can reactive convex client be used from within Convex Node action?

4 messages · Page 1 of 1 (latest)

upbeat geyser
#

Can reactive convex client be used from within Convex Node action to wait on some data (read set) to change in a certain query?

exotic scaffoldBOT
#

Hi @upbeat geyser. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The knowledge sources do not explicitly address using the reactive ConvexClient from within a Convex action (Node.js runtime). However, based on what the knowledge sources do say, here is what can be stated:

What the knowledge sources confirm

The reactive ConvexClient (which supports WebSocket-based subscriptions via onUpdate) is described as suitable for Node.js environments and long-running servers: [Convex JavaScript Clients]

"The reactive Convex Client allows web applications and long-running Node.js servers to subscribe to updates on Convex queries."

Example usage in Node.js:

import { ConvexClient } from "convex/browser";
import { api } from "../convex/_generated/api";

const client = new ConvexClient(process.env.CONVEX_URL!);

client.onUpdate(api.messages.listAll, {}, (messages) =>
  console.log(messages.map((msg) => msg.body)),
);

[Convex JavaScript Clients]

Important caveats

  1. The knowledge sources do not explicitly confirm or deny whether instantiating a ConvexClient inside a Convex action (running on Convex's infrastructure) is a supported or recommended pattern.

  2. Convex actions are described as able to run for up to 10 minutes and can call external services, but the knowledge sources do not discuss using a ConvexClient subscription loop from within an action to wait for data changes. [Why calling an action from a client is an anti-pattern?]

#
  1. The typical recommended pattern for reacting to data changes in Convex is to use useQuery on the client side, or to schedule follow-up mutations/actions via ctx.scheduler on the server side — not to poll or subscribe from within an action. [Action Triggers from Convex Queries]

Summary

While the ConvexClient technically works in Node.js environments, the knowledge sources do not provide enough information to confirm it is safe or supported to use it inside a Convex action to subscribe and wait on query changes. I'd recommend consulting the Convex documentation directly or asking in the Convex Discord for an authoritative answer on this specific use case.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: