How to use the sharedb.types function in sharedb

To help you get started, we’ve selected a few sharedb examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github chili-epfl / collab-react-components / src / server / CollabServer.js View on Github external
const server = http.createServer(app);
  let db = null;
  if (CollabServer.options.db.type === 'mongo') {
    db = CollabServer.options.db.url
      ? ShareDBMongo(CollabServer.options.db.url)
      : {};
    console.log('CollabServer: Using MongoDB adapter');
  } else {
    console.log(
      'CollabServer: No Database specified, falling back to In Memory'
    );
  }

  // Create the ShareDB backend (that will need to be exported)
  ShareDB.types.register(RichText.type);
  CollabServer.backend = new ShareDB({ db });

  // Create the Websocket server
  new WebSocket.Server({ server }).on('connection', function(ws) {
    CollabServer.backend.listen(new WebsocketJSONStream(ws));
    console.log('New socket client on CollabServer instance');
  });

  server.listen(CollabServer.options.port, function(err) {
    if (err) throw err;
    console.log(
      'CollabServer: Server listening on port ' + CollabServer.options.port
    );
  });
};
github bluemir / node-wikinote / routes / share.js View on Github external
var ShareDB = require('sharedb');
var ottext = require('ot-text');

var wikiFS = require("../app/wikiFS");
var WikiPath = require("../app/wikipath");

ShareDB.types.register(ottext.type);

var share = new ShareDB({
	//db: ShareDB.MemoryDB,
	//pubsub: ShareDB.MemoryPubSub
});

var WebSocketJSONStream = require('websocket-json-stream');

var browserify = require('browserify');
var b = browserify({basedir: __dirname});

b.require("sharedb/lib/client", {expose: "share"});
b.require("ot-text");

// make browerify buffer
exports.static = function(req, res) {
github sillsdev / web-languageforge / src / SIL.XForge / Realtime / server.js View on Github external
var express = require('express');
var http = require('http');
var richText = require('rich-text');
var otJson0 = require('ot-json0');
var ShareDB = require('sharedb');
var ShareDBMongo = require('sharedb-mongo');
var WebSocketJSONStream = require('websocket-json-stream');
var WebSocket = require('ws');

ShareDB.types.register(richText.type);
ShareDB.types.register(otJson0.type);

var backend = null;

// Create web servers to serve files and listen to WebSocket connections
var app = express();
app.use(express.static('static'));
var server = http.createServer(app);

module.exports = {
  start: function(callback, mongo, port) {
    var database = ShareDBMongo(mongo);
    backend = new ShareDB({
      db: database,
      disableDocAction: true,
      disableSpaceDelimitedActions: true
    });
github sillsdev / web-languageforge / src / node / server.js View on Github external
var cookie = require('cookie');
var express = require('express');
var fs = require('fs');
var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var os = require('os');
var path = require('path');
var richText = require('rich-text');
var ShareDB = require('sharedb');
var database = require('sharedb-mongo')('mongodb://localhost:27017/realtime');
var WebSocketJSONStream = require('websocket-json-stream');
var WebSocket = require('ws');

ShareDB.types.register(richText.type);

var share = new ShareDB({ db: database });

// Expose the session from initial connection as agent.phpSessionId.
share.use('connect', function (request, done) {
  if (request.req) {
    request.agent.phpSessionId = request.req.phpSessionId;
  }

  done();
});

share.use('apply', useUserData);
share.connect();

var hostname = '0.0.0.0';
github sillsdev / web-languageforge / src / SIL.XForge / Realtime / server.js View on Github external
var express = require('express');
var http = require('http');
var richText = require('rich-text');
var otJson0 = require('ot-json0');
var ShareDB = require('sharedb');
var ShareDBMongo = require('sharedb-mongo');
var WebSocketJSONStream = require('websocket-json-stream');
var WebSocket = require('ws');

ShareDB.types.register(richText.type);
ShareDB.types.register(otJson0.type);

var backend = null;

// Create web servers to serve files and listen to WebSocket connections
var app = express();
app.use(express.static('static'));
var server = http.createServer(app);

module.exports = {
  start: function(callback, mongo, port) {
    var database = ShareDBMongo(mongo);
    backend = new ShareDB({
      db: database,
      disableDocAction: true,
      disableSpaceDelimitedActions: true
github share / sharedb / examples / rich-text / server.js View on Github external
var http = require('http');
var express = require('express');
var ShareDB = require('sharedb');
var richText = require('rich-text');
var WebSocket = require('ws');
var WebSocketJSONStream = require('@teamwork/websocket-json-stream');

ShareDB.types.register(richText.type);
var backend = new ShareDB();
createDoc(startServer);

// Create initial document then fire callback
function createDoc(callback) {
  var connection = backend.connect();
  var doc = connection.get('examples', 'richtext');
  doc.fetch(function(err) {
    if (err) throw err;
    if (doc.type === null) {
      doc.create([{insert: 'Hi!'}], 'rich-text', callback);
      return;
    }
    callback();
  });
}
github nicktogo / coeditor / server / server.js View on Github external
var ShareDB = require('sharedb');
var WebSocket = require('ws');
var WebSocketStream = require('websocket-push-stream');
var otText = require('ot-text');

ShareDB.types.register(otText.type);

allSessions = [];

var backend = new ShareDB();
backend.use('op', (request, callback) => {
  callback();
  setTimeout( () => {
    let ws = request.agent.stream.ws;
    let cursors = allSessions[ws.sessionId].cursors;
    if (typeof cursors !== 'undefined') {
      console.log('Broadcasting ' + ws.clientId + '\'s cursors');
      for (let path in cursors) {
        if (cursors.hasOwnProperty(path) && JSON.parse(cursors[path]).clientId === ws.clientId) {
          console.log(path);
          broadcastMsg(cursors[path], ws);
        }
github pedrosanta / quill-sharedb-cursors / helpers / sharedb-server.js View on Github external
var ShareDB = require('sharedb');

ShareDB.types.register(require('rich-text').type);

module.exports = new ShareDB({
  db: require('sharedb-mongo')(process.env.MONGODB_URI || 'mongodb://localhost/quill-sharedb-cursors?auto_reconnect=true')
});
github lostintangent / kyte / packages / kyte / server / index.js View on Github external
module.exports = port => {
  const app = express();
  app.use(express.static(path.join(__dirname, "dist")));

  const server = http.createServer(app);
  server.listen(port);

  ShareDB.types.map["json0"].registerSubtype(require("ot-text").type);
  const shareDB = ShareDB();

  const webSocketServer = new WebSocketServer({ server });
  webSocketServer.on("connection", socket => {
    shareDB.listen(WebSocketJsonStream(socket));
  });

  return {
    url: `http://localhost:${port}`,
    wsUrl: `ws://localhost:${port}`
  };
};