DCsan
DCsan2y ago

subscribe

I'm trying to use this as a realtime DB (firebase replacement) on the SERVER to listen to events. How do i listen for changes on a collection, or subscribe to a query? A bit like useQuery hook but for server side? Or do I have to resort to something with actions?
react.js | Convex Developer Hub
These exports are not directly available in the convex package!
19 Replies
DCsan
DCsanOP2y ago
https://docs.convex.dev/functions/query-functions#caching--reactivity
"clients can subscribe to queries "
how do i do this without looking into the hooks implementation for client side web apps (i'd like to do it server side). i think i read somewhere this was the last part of the convex puzzle that hasn't been built yet?
Queries | Convex Developer Hub
Queries are the bread and butter of your backend API. They fetch data from the
ballingt
ballingt2y ago
@DCsan do you want this for a server you're running? If it can connect with a websocket you can use the base client or the React client on the server, things like this https://observablehq.com/@ballingt/hello-convex-beta
Observable
Hello Convex
How do you get shared, reactive server-side state for an Observable notebook? There are many ways: AWS, firebase, a slick solution from Tom Larkworthy — and now there's one more, Convex. You can sign up for the Convex at convex.dev. To use Convex in a webapp or from Node.js the standard npm install convex etc. is recommended, but in Observable...
DCsan
DCsanOP2y ago
yes server side
ballingt
ballingt2y ago
To create a client in Node.js you need to pass a couple options, let me see if we have an example posted
DCsan
DCsanOP2y ago
queryGenerator = (client, query, args = {}) => {
const watch = client.watchQuery(query, args);
console.log(watch);
return Generators.observe((notify) => {
console.log(watch.onUpdate);
return watch.onUpdate(() => notify(watch.localQueryResult()));
});
}
queryGenerator = (client, query, args = {}) => {
const watch = client.watchQuery(query, args);
console.log(watch);
return Generators.observe((notify) => {
console.log(watch.onUpdate);
return watch.onUpdate(() => notify(watch.localQueryResult()));
});
}
is this the incantation? or is that also client side code?
ballingt
ballingt2y ago
You don't need that stuff, that's to build a generator of results
DCsan
DCsanOP2y ago
after that is a nonReactiveQuery, which isn't what i want... i think?
ballingt
ballingt2y ago
That example is client side but it also works server side
DCsan
DCsanOP2y ago
so ... client = new c.ConvexReactClient(url) ? then i use the reactHooks ? (not on this article?) so there isn't a documented way to listen to events on the server side?
ballingt
ballingt2y ago
Yep, that's it, no React hooks, just methods on the client
DCsan
DCsanOP2y ago
maybe
const watch = client.watchQuery(queryName, ...args)
watch.onUpdate(cb)
const watch = client.watchQuery(queryName, ...args)
watch.onUpdate(cb)
? like, what methods? should i do reflection on the client to try and figure it out?
ballingt
ballingt2y ago
import ws from "ws";

const client = new ConvexReactClient(address, {
webSocketConstructor: ws,
unsavedChangesWarning: false,
});

const watch = client.watchQuery("listMessages", {});
watch.onUpdate(() => console.log(watch.localQueryResult()));
import ws from "ws";

const client = new ConvexReactClient(address, {
webSocketConstructor: ws,
unsavedChangesWarning: false,
});

const watch = client.watchQuery("listMessages", {});
watch.onUpdate(() => console.log(watch.localQueryResult()));
DCsan
DCsanOP2y ago
ok looks good, tx.
ballingt
ballingt2y ago
We should absolutely have a dedicated doc for this, above is doing this with the React client but we need an example without React
DCsan
DCsanOP2y ago
is this not a supported use case or something? i'm wondering why its not in the docs, it seems a primary benefit of convex is realtime stuff? your example doesn't use react right? unless i'm missing some import/deps ConvexReactClient oops ok so importing that into node without a DOM should work? also, were there some limitations that I can only listen to "pure" functions? where as anything that touches the DB must by definition be non deterministic.
ballingt
ballingt2y ago
This is supported, we've just found it a lot rarer so have focused on the client-side applications. This works without a DOM, but requires you have React installed which isn't ideal. When folks want to talk to the database from Node.js usually they've wanted non-reactive reads, so https://docs.convex.dev/quickstart/nodejs is the common path I'll get an example up that shows subscriptions from Node.js without React, it's a couple more lines of code.
DCsan
DCsanOP2y ago
:ty: would rather not import all of react into a SS app
ballingt
ballingt2y ago
also, were there some limitations that I can only listen to "pure" functions? where as anything that touches the DB must by definition be non deterministic.
That's one of the big pieces we do special, accessing the DB counts as deterministic When you run a query we track which ranges of which tables and indexes were accessed by that query. Because it's deterministic, we know we only need to rerun that query if data in those ranges is modified. If it is, we rerun the query and send the new result down the websocket.
DCsan
DCsanOP2y ago
ok doke, ty

Did you find this page helpful?