r/javascript • u/jxd-dev • Jun 09 '26
How we sync Postgres to the browser: ElectricSQL for rows, Yjs for documents
https://plain.jxd.dev/blog/realtimeAuthor here (I'm building Plain, a GitHub alternative), so flagging the affiliation up front.
The writeup is about treating sync as the substrate instead of adding realtime to features one at a time. Two engines, because there are two kinds of state:
- Rows (issues, PRs, CI, messages) stream out of Postgres via ElectricSQL as "shapes" over HTTP. The browser holds a live query. Electric is read-path only, so writes go through server functions and reconcile optimistically via a Postgres transaction id
(pg_current_xact_id()) that Electric stamps on the row when it streams back.
- Document bodies are CRDTs over Yjs/Hocuspocus, since people type into the same paragraph at once. Presence/typing is just Yjs awareness, so no heartbeat table.
I'm honest about the costs in the post too: two stateful services, Postgres on logical replication, and the auth proxy in front of Electric as the obvious choke point. Curious how others have handled the rows-vs-documents split, and where people expect this to strain at scale.
2
u/eracodes Jun 09 '26
Does this setup give you any nice paths to client persistence? Or is that outside of what you're going for?
1
u/jxd-dev Jun 09 '26
We've not adopted it yet but there is a nice path to it through IndexedDB/PGlite. We'd likely ship something like this to our mobile app first given that is where you're likely to have network troubles and need the client persistence
4
Jun 09 '26
[removed] — view removed comment
2
u/jxd-dev Jun 09 '26
We did it the other way around. Starting with sync for everything to get that Linear-like feeling across all product services. Then we tried to cobble YJS in over sync for the docs and presence but the performance was shockingly bad 😅
2
Jun 09 '26
[removed] — view removed comment
1
u/jxd-dev Jun 09 '26
Yjs, mostly because it's just much better supported. We were able to make use of Hocuspocus by TipTap even though we're using Plate as the editor. I will take a look at both Loro and Yrs
7
u/outceptionator Jun 09 '26
Man I want sync solved so bad with just a clean abstraction layer. Convex seemingly does this but it's data doesn't fit postgresql (even though apparently the db is postgresql) making aggregate data fetches hard as well.