ShareDBError: Both src and seq must be set together

31 views Asked by At

I'm trying to use ShareDB for a live text editing project I've been working on (it's like Google's collaboration feature in docs)

The problem is that I apparently need to set the seq and src together, it's even worst there is nothing I could find on the web

Here is my current code:

const express = require('express');
const WebSocket = require('ws');
const ShareDB = require('sharedb');
const richText = require('rich-text');
const WebSocketJSONStream = require('@teamwork/websocket-json-stream');

ShareDB.types.register(richText.type);

const ShareDBMongo = require('sharedb-mongo');
const db = ShareDBMongo('mongodb://localhost:27017/');

const backend = new ShareDB({ db });
const app = express();
app.use(express.static('public'));
const wss = new WebSocket.Server({ noServer: true });

wss.on('connection', (ws) => {
    console.log("new client Connected!")
    const stream = new WebSocketJSONStream(ws);
    backend.listen(stream);
});

wss.on('listening', () => {
    console.log('WebSocket server is listening on port 3000');
});

createInitialDocument(() => {
    const PORT = 3000;
    const server = app.listen(PORT, () => {
        console.log(`Listening on ws://localhost:${PORT}`);
    });
    server.on('upgrade', (request, socket, head) => {
        wss.handleUpgrade(request, socket, head, (ws) => {
            wss.emit('connection', ws, request);
        });
    });
});

function createInitialDocument(callback) {
    const connection = backend.connect();
    const doc = connection.get('documents', 'main');

    doc.fetch((err) => {
        if (err) throw err;
        if (doc.type === null) {
            doc.create({ content: 'Text goes here' }, 'rich-text', callback);
            return;
        }
        callback();
    });
}

I have tied setting the seq and src together in this function:

    function submitOp(data) {
      const op = data.op;
    op.forEach((o) => {
          o.seq = doc.version;
          o.src = 'pi';
        });
      doc.submitOp(op, (err) => {
          if (err) throw err;
      });

this didn't do anything to fix my problem.

Due to the lack of info on the web I don't quite know how to fix this

0

There are 0 answers