i use the standard websocket (not y-websocket) for sync update:
server:
…
let models={}
ws.onopen=()=>{
let model=models[ws.data.fileId]
if(!model){
let ydoc=new Y.Doc()
let update:Uint8Array=loadBytes(‘path’)
Y.applyUpdate(ydoc,update)
model={ydoc,update,change:0}
models[ws.data.fileId]=model
}
if(model.change){
model.change=0
model.update=Y.encodeStateAsUpdate(model.ydoc)
}
ws.send(model.update)
}
ws.onmessage=(e)=>{
let model=models[ws.data.fileId]
model.change++
let update=new Uint8Array(e.data)
Y.applyUpdate(model.ydoc,update)
…
}
client:
…
let ydoc=new Y.Doc()
let first=true
ws.onmessage=async (ev)=>{
let update=new Uint8Array(await ev.data.arrayBuffer())
if(first){
first=false
Y.applyUpdate(ydoc,update)
this.initModel()
ydoc.on(‘update’,(update,origin,doc,tr)=>{
if(tr.local){
ws.send(update)
}
})
}else{
Y.applyUpdate(ydoc,update)
}
}
…
when client type a char in a transction, which will cause update event, and is sent to server.
the problem is :
when refresh, client receive the old y.doc, not the updated one.
what am i doing wrong?