How is realtime DB syncing accomplished without Firestore/Supabase?
Hi! I've been working with Firestore for a long time now and gotten very used to the realtime db syncing functionality it provides, although I'm trying to move away from Firestore now. Supabase is an obvious alternative because it also provides realtime functionality, but I'm wondering if there's any other way DB syncing might be accomplished (say, if I'm using PlanetScale).
If I'm using Planetscale as a DB and, say, viewing data from the database on the webapp, and a different user updates the data, is there a way for me to receive the update on my end (without refreshing)?
Thanks!
17 Replies
^^ planetscale is just a specific example, but I'm basically wondering about any DB option that doesn't explicitly provide a realtime client like Firestore or Supabase.
Realtime DB syncing is complicated, only way to make it work with postgres/mysql is by implementing it on your own with websockets.
@alandsleman thank you so much! Is there an easy way to accomplish, like, "near-realtime" functionality with the T3 stack, such as by intermittent polling of queries (or something of that nature)?
You can always put your queries in a setInterval block but it is extremely wasteful and a practice I would avoid at all costs
@IsoPhoenix If you are not looking to implement a bidirectional connection between db and client, try react query. T3 app is using react query under the hood and on top of that, you can use react query within T3 app as well. You should have access to the boolean value "isStale", so just create a condition to check if data is stale, and then call "refetch" function if the data is indeed stale.
Also, with next.js, useSWR will do the same but generally you can achieve what you want with react query.
react query has auto refetching built in
I'm running into the same issue with pscale. https://github.com/planetscale/discussion/discussions/119 The only work around is polling, which is blunt and expensive. I'm surprised realtime hooks aren't in demand. My use case is digital signage dashboards and kiosk apps so maybe i'm in a minority, but I would have thought listening to table updates would have a wide scope of uses. Did you find any solutions? @IsoPhoenix
GitHub
CDC from planetscale · planetscale discussion · Discussion #119
Is there already a solution for or plans to implement a feature that would allow change data capture with planetscale? i.e. deliver an event to my application with the changed data when a row is up...
Postgres has listeners that when combined with a trigger you can spin off an event then consume it on your application. https://www.postgresql.org/docs/8.4/catalog-pg-listener.html
I usually have a backend service that would listen for these events then handle the publish into a websocket that the front end consumes otherwise your only other options is gonna be polling.
PostgreSQL Documentation
pg_listener
Real time hooks are definitely in demand, you basically have ably and pusher that provide them but they are expensive
soketi
Soketi is your simple, fast, and resilient open-source WebSockets server. 📣
Theres also this if you want to play around w it
Thanks for the response keef. I was looking at the pub/sub services yesterday but didnt understand how they worked looking at the examples. Since there is no CDC/webhooks on planetscale how do they know when theres been an update. I suppose maybe you could run a seperate db which you call anytime you make updates to a pscale table and listen to that instead 🤔
Yeah its kinda off topic my bad* but its just general advice at achieving this outside of planetscale
All good!
Polling is kinda gross but it does the job when your service doesn't help you in achieving what you need
Yeahhh, im sure they'll figure a solution out in the coming months, I think for now I'll have to stick with supabase and miss out on the cool forking action :~)
Thanks for clearing that up for me though, I was obsessively looking for a solution that wasnt there lol
eh its not much of solution tbh lol just sharing it bc it may be useful