Skip to main content 홈 크리에이터 electric-sql electric electric-yjs
electric-yjs Set up ElectricProvider for real-time collaborative editing with Yjs via Electric shapes. Covers ElectricProvider configuration, document updates shape with BYTEA parser (parseToDecoder), awareness shape at offset='now', LocalStorageResumeStateProvider for reconnection with stableStateVector diff, debounceMs for batching writes, sendUrl PUT endpoint, required Postgres schema (ydoc_update and ydoc_awareness tables), CORS header exposure, and sendErrorRetryHandler. Load when implementing collaborative editing with Yjs and Electric.
설치로 이동 Skills Marketplace 커뮤니티가 만든 AI 스킬을 발견하고 탐색하세요.
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
직접 명령은 검토 Prompt를 거치지 않습니다. 실행하기 전에 소스를 확인하세요.
npx skills add https://github.com/electric-sql/electric --skill electric-yjs명령은 한 줄로 유지됩니다. 복사하기 전에 가로로 스크롤해 전체 내용을 확인하세요.
로컬 사본을 원하시나요? SkillsMP에서 현재 제공할 수 있는 파일을 다운로드하세요.
Zip 다운로드 다운로드 중... Configure ShapeStream and Shape to sync a Postgres table to the client. Covers ShapeStreamOptions (url, table, where, columns, replica, offset, handle), custom type parsers (timestamptz, jsonb, int8), column mappers (snakeCamelMapper, createColumnMapper), onError retry semantics, backoff options, log modes (full, changes_only), requestSnapshot, fetchSnapshot, subscribe/unsubscribe, and Shape materialized view. Load when setting up sync, configuring shapes, parsing types, or handling sync errors.
Use when an app developer wants to build an entity (a.k.a. an agent) for their Electric Agents app — designing a single entity type, picking a coordination pattern when needed (single-agent, manager-worker, pipeline, map-reduce, dispatcher, blackboard, reactive-observer), defining state, handler, schemas, and implementing it in one entity file. Applies to any use of `registry.define(...)` / `defineEntity(...)` in a `@electric-ax/agents-runtime` app.
name electric-yjs description Set up ElectricProvider for real-time collaborative editing with Yjs via Electric shapes. Covers ElectricProvider configuration, document updates shape with BYTEA parser (parseToDecoder), awareness shape at offset='now', LocalStorageResumeStateProvider for reconnection with stableStateVector diff, debounceMs for batching writes, sendUrl PUT endpoint, required Postgres schema (ydoc_update and ydoc_awareness tables), CORS header exposure, and sendErrorRetryHandler. Load when implementing collaborative editing with Yjs and Electric.
type composition library electric library_version 0.1.36 requires ["electric-shapes"] sources ["electric-sql/electric:packages/y-electric/src/y-electric.ts","electric-sql/electric:packages/y-electric/src/types.ts","electric-sql/electric:packages/y-electric/src/local-storage-resume-state.ts","electric-sql/electric:packages/y-electric/src/utils.ts","electric-sql/electric:examples/yjs/"]
This skill builds on electric-shapes. Read it first for ShapeStream configuration.
Electric — Yjs Collaboration
Setup
1. Create Postgres tables
CREATE TABLE ydoc_update (
id SERIAL PRIMARY KEY ,
room TEXT NOT NULL ,
update BYTEA NOT NULL
);
CREATE TABLE ydoc_awareness (
client_id TEXT,
room TEXT,
update BYTEA NOT NULL ,
updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY (client_id, room)
);
CREATE OR REPLACE FUNCTION gc_awareness_timeouts()
RETURNS TRIGGER AS $$
BEGIN
DELETE FROM ydoc_awareness
WHERE updated_at < (CURRENT_TIMESTAMP - INTERVAL '30 seconds' )
AND room = NEW.room;
RETURN NEW ;
END ;
$$ LANGUAGE plpgsql;
CREATE TRIGGER gc_awareness
AFTER INSERT OR UPDATE ON ydoc_awareness
FOR EACH ROW EXECUTE FUNCTION gc_awareness_timeouts();
2. Create server endpoint for receiving updates
app. ( , (req, res) => {
body = . ( req. ())
db. ( , [
req. [ ],
body,
])
res. ( ). ()
})
put
'/api/yjs/update'
async
const
Buffer
from
await
arrayBuffer
await
query
'INSERT INTO ydoc_update (room, update) VALUES ($1, $2)'
headers
'x-room-id'
status
200
end
3. Configure ElectricProvider import * as Y from 'yjs'
import {
ElectricProvider ,
LocalStorageResumeStateProvider ,
parseToDecoder,
} from '@electric-sql/y-electric'
const ydoc = new Y.Doc ()
const roomId = 'my-document'
const resumeProvider = new LocalStorageResumeStateProvider (roomId)
const provider = new ElectricProvider ({
doc : ydoc,
documentUpdates : {
shape : {
url : `/api/yjs/doc-shape?room=${roomId} ` ,
parser : parseToDecoder,
},
sendUrl : '/api/yjs/update' ,
getUpdateFromRow : (row ) => row.update ,
},
awarenessUpdates : {
shape : {
url : `/api/yjs/awareness-shape?room=${roomId} ` ,
parser : parseToDecoder,
offset : 'now' ,
},
sendUrl : '/api/yjs/awareness' ,
protocol : provider.awareness ,
getUpdateFromRow : (row ) => row.update ,
},
resumeState : resumeProvider.load (),
debounceMs : 100 ,
})
resumeProvider.subscribeToResumeState (provider)
Core Patterns
CORS headers for Yjs proxy
const corsHeaders = {
'Access-Control-Expose-Headers' :
'electric-offset, electric-handle, electric-schema, electric-cursor' ,
}
Resume state for reconnection
const provider = new ElectricProvider ({
doc : ydoc,
documentUpdates : { shape : shapeOpts, sendUrl : '/api/yjs/update' },
resumeState : resumeProvider.load (),
})
const unsub = resumeProvider.subscribeToResumeState (provider)
provider.destroy ()
unsub ()
When stableStateVector is provided in resume state, the provider sends only the diff between the stored vector and current doc state on reconnect.
Connection lifecycle provider.on ('status' , ({ status } ) => {
console .log ('Yjs sync status:' , status)
})
provider.on ('sync' , (synced : boolean ) => {
console .log ('Document synced:' , synced)
})
provider.disconnect ()
provider.connect ()
Common Mistakes
HIGH Not persisting resume state for reconnection const provider = new ElectricProvider ({
doc : ydoc,
documentUpdates : {
shape : { url : '/api/yjs/doc-shape' , parser : parseToDecoder },
sendUrl : '/api/yjs/update' ,
getUpdateFromRow : (row ) => row.update ,
},
})
const resumeProvider = new LocalStorageResumeStateProvider ('my-doc' )
const provider = new ElectricProvider ({
doc : ydoc,
documentUpdates : {
shape : { url : '/api/yjs/doc-shape' , parser : parseToDecoder },
sendUrl : '/api/yjs/update' ,
getUpdateFromRow : (row ) => row.update ,
},
resumeState : resumeProvider.load (),
})
resumeProvider.subscribeToResumeState (provider)
Without resumeState, the provider fetches the ENTIRE document shape on every reconnect. With stableStateVector, only a diff is sent.
Source: packages/y-electric/src/types.ts:102-112
HIGH Missing BYTEA parser for shape streams documentUpdates : {
shape : { url : '/api/yjs/doc-shape' },
sendUrl : '/api/yjs/update' ,
getUpdateFromRow : (row ) => row.update ,
}
import { parseToDecoder } from '@electric-sql/y-electric'
documentUpdates : {
shape : {
url : '/api/yjs/doc-shape' ,
parser : parseToDecoder,
},
sendUrl : '/api/yjs/update' ,
getUpdateFromRow : (row ) => row.update ,
}
Yjs updates are stored as BYTEA in Postgres. Without parseToDecoder, the shape returns raw hex strings instead of lib0 Decoders, and Y.applyUpdate fails silently or corrupts the document.
Source: packages/y-electric/src/utils.ts
MEDIUM Not setting debounceMs for collaborative editing const provider = new ElectricProvider ({
doc : ydoc,
documentUpdates : { shape : shapeOpts, sendUrl : '/api/yjs/update' },
})
const provider = new ElectricProvider ({
doc : ydoc,
documentUpdates : { shape : shapeOpts, sendUrl : '/api/yjs/update' },
debounceMs : 100 ,
})
Default debounceMs is 0, sending a PUT request for every keystroke. Set to 100+ to batch rapid edits and reduce server load.
Source: packages/y-electric/src/y-electric.ts
See also: electric-shapes/SKILL.md — Shape configuration and parser setup.
Version Targets @electric-sql/y-electric v0.1.x.