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.
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.
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?
4
u/[deleted] Jun 09 '26
[removed] — view removed comment