Skip to content

Instantly share code, notes, and snippets.

@vvatikiotis
Last active August 23, 2017 12:24
Show Gist options
  • Save vvatikiotis/e112a9d239d8f9f73e84dcaac34ae98c to your computer and use it in GitHub Desktop.
Save vvatikiotis/e112a9d239d8f9f73e84dcaac34ae98c to your computer and use it in GitHub Desktop.
WIP: Client-server PoC using gulf. Incomplete so far. Changes flow from server to client but not the opposite direction.
const textOT = require('ot-text').type
const gulf = require('gulf')
const net = require('net')
var doc = new gulf.EditableDocument({
storageAdapter: new gulf.MemoryAdapter,
ottype: textOT,
})
let content
doc._onBeforeChange = function() {
console.log('onBeforeChange')
return Promise.resolve()
}
doc._onChange = function(cs) {
console.log(`onChange: ${cs}`)
content = textOT.apply(content, cs)
return Promise.resolve()
}
doc._setContent = function(newcontent) {
console.log(`setContent: ${newcontent}`)
content = newcontent
return Promise.resolve()
}
var master = doc.masterLink()
const socket = net.connect(8080, function() {
socket.pipe(master).pipe(socket)
setTimeout(() => doc.submitChange([1, 'r']), 3000)
})
socket.on('pipe', (src) => console.log(`Pipping to client`))
const gulf = require('gulf')
var textOT = require('ot-text').type
const net = require('net')
var doc = new gulf.Document({
storageAdapter: new gulf.MemoryAdapter,
ottype: textOT,
})
doc.initializeFromStorage('abc')
const sock = net.createServer((socket) => {
console.log('starting net server')
const slave = doc.slaveLink()
doc.on('commit', (a, b) => console.log(`server: ${JSON.stringify(a)}, ${b}`))
socket.pipe(slave).pipe(socket)
}).listen(8080)
sock.on('pipe', (src) => console.log(`1. pipping to server`))
sock.on('error', (err) => console.log('error'))
@marcelklehr
Copy link

You can check that the server receives the changes by listening to the master doc's commit event. I tried it and it seems to work. I think the only problem is that you didn't update you content variable. Gulf doesn't do that, as it assumes the changes come from an editor and sending them to the editor again would result in a mess, so if your changes don't come from an editor, you'll need to update your "editor" or whatever you're editing manually.

Also, https://gist.github.com/vvatikiotis/e112a9d239d8f9f73e84dcaac34ae98c#file-server-js-L10 : This should be inside the socket listener ;)

@vvatikiotis
Copy link
Author

vvatikiotis commented Aug 23, 2017

Fixed https://gist.github.com/vvatikiotis/e112a9d239d8f9f73e84dcaac34ae98c#file-server-js-L10, cheers :)

As for updating the content variable. The client _onChange listener doesn't fire, ever, and Im wondering why. The client document handlers (_onChange, _onBeforeChange and _setContent) are implemented as per Gulf's tests. And content var is updated after the OT transform (https://gist.github.com/vvatikiotis/e112a9d239d8f9f73e84dcaac34ae98c#file-client-js-L19), but the _onChange doesn't fire at all, so no change.

One thing that might shed some light in my head: server and client are run within their own process.

@vvatikiotis
Copy link
Author

False alarm. I was expecting the _onChange handler of client doc to fire when submitChange is called (within that same doc), whereas the documentation specifically states that _onChange fires "for every change that is received from master".

I have synced docs :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment