Skip to content

Commit

Permalink
fix: socket io namespace message broadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
sdfs committed Oct 27, 2020
1 parent 5e0ee3e commit ab3c36a
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions packages/backend/src/SocketIOConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default class SocketIOCollaboration {
private io: SocketIO.Server
private options: SocketIOCollaborationOptions
private backend: AutomergeBackend
private autoSaveDoc: (docId: string) => void

/**
* Constructor
Expand All @@ -45,6 +46,15 @@ export default class SocketIOCollaboration {

this.configure()

/**
* Save document with throttle
*/
this.autoSaveDoc = throttle(
async (docId: string) =>
this.backend.getDocument(docId) && this.saveDocument(docId),
this.options?.saveFrequency || 2000
)

return this
}

Expand Down Expand Up @@ -108,24 +118,24 @@ export default class SocketIOCollaboration {
const { name } = socket.nsp

this.backend.createConnection(id, ({ type, payload }: CollabAction) => {
socket.emit('msg', { type, payload: { id: conn.id, ...payload } })
if (payload.docId === name) {

This comment has been minimized.

Copy link
@ulion

ulion Jan 15, 2021

Contributor

although do filter here, but the automerge connection still keep those unrelated doc's clock. even after those unrelated doc removed due to disconnect of all that doc's users, once any one of such doc users reconnect, the doc re-inited, but with a new empty clock, which conflict with here cached clock in connection._ourClock. then cause the "old state" error in every setDoc call's callback handler, which will cause those re-inited doc stop sync, user can only refresh to get new content, no cursors, no realtime update anymore if that happen (re-init a doc + another doc keep connected). no idea how to fix this, created issue on automerge: automerge/automerge#303

socket.emit('msg', { type, payload: { id: conn.id, ...payload } })
}
})

socket.on('msg', this.onMessage(id, name))

socket.on('disconnect', this.onDisconnect(id, socket))

socket.join(id, () => {
const doc = this.backend.getDocument(name)
const doc = this.backend.getDocument(name)

socket.emit('msg', {
type: 'document',
payload: Automerge.save<SyncDoc>(doc)
})

this.backend.openConnection(id)
socket.emit('msg', {
type: 'document',
payload: Automerge.save<SyncDoc>(doc)
})

this.backend.openConnection(id)

this.garbageCursors(name)
}

Expand All @@ -148,16 +158,6 @@ export default class SocketIOCollaboration {
}
}

/**
* Save document with throttle
*/

private autoSaveDoc = throttle(
async (docId: string) =>
this.backend.getDocument(docId) && this.saveDocument(docId),
this.options?.saveFrequency || 2000
)

/**
* Save document
*/
Expand All @@ -183,6 +183,8 @@ export default class SocketIOCollaboration {
*/

private onDisconnect = (id: string, socket: SocketIO.Socket) => async () => {
socket.leave(socket.nsp.name)

this.backend.closeConnection(id)

await this.saveDocument(socket.nsp.name)
Expand Down Expand Up @@ -219,8 +221,7 @@ export default class SocketIOCollaboration {
garbageCursors = (nsp: string) => {
const doc = this.backend.getDocument(nsp)

if (doc == null || doc == undefined) return;
if (!doc.cursors) return
if (!doc || !doc.cursors) return

const namespace = this.io.of(nsp)

Expand Down

0 comments on commit ab3c36a

Please sign in to comment.