#subscribe

32 messages · Page 1 of 1 (latest)

cursive bane
#

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?

These exports are not directly available in the convex package!

cursive bane
random olive
#

@cursive bane 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

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...

cursive bane
#

yes server side

random olive
#

To create a client in Node.js you need to pass a couple options, let me see if we have an example posted

cursive bane
#
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?

random olive
#

You don't need that stuff, that's to build a generator of results

cursive bane
#

after that is a nonReactiveQuery, which isn't what i want... i think?

random olive
#

That example is client side but it also works server side

cursive bane
#

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?

random olive
#

Yep, that's it, no React hooks, just methods on the client

cursive bane
#

maybe

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?

random olive
#
import ws from "ws";

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

const watch = client.watchQuery("listMessages", {});
watch.onUpdate(() => console.log(watch.localQueryResult()));
cursive bane
#

ok looks good, tx.

random olive
#

We should absolutely have a dedicated doc for this, above is doing this with the React client but we need an example without React

cursive bane
#

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.

random olive
#

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.

cursive bane
#

would rather not import all of react into a SS app

random olive
#

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.