How to use the automerge.Connection function in automerge

To help you get started, we’ve selected a few automerge 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 humandx / slate-automerge / src / example / server / index.js View on Github external
socket.on('join_document', function({clientId, docId}, callback) {
    // Permissions check
    if (!hasPermission(clientId, docId)) { return }

    docId  = Number(docId)
    if (!docSet.getDoc(docId)) {
      createNewDocument(docId)
    }

    if (!connections[clientId]) {
      connections[clientId] = new Automerge.Connection(
          docSet,
          (message) => {
              if (!hasPermission(clientId, message.docId)) { return }
              socket.emit("send_operation", message)
          }
      )
      connections[clientId].open()
    }

    socket.join(docId)
    callback(true)
  });
github cudr / slate-collaborative / packages / backend / src / Connection.ts View on Github external
private onConnect = socket => {
    const { id, conn } = socket
    const { name } = socket.nsp

    const doc = this.docSet.getDoc(name)

    const data = Automerge.save(doc)

    this.connections[id] = new Automerge.Connection(this.docSet, data => {
      socket.emit('operation', { id: conn.id, ...data })
    })

    socket.join(id, () => {
      this.connections[id].open()

      socket.emit('document', data)
    })

    socket.on('operation', this.onOperation(id, name))

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

    this.garbageCursors(name)
  }
github humandx / slate-automerge / src / example / simple / client.js View on Github external
constructor(props) {
        super(props)

        this.doc = Automerge.init();
        this.docSet = new Automerge.DocSet()
        this.onChange = this.onChange.bind(this)

        this.connection = new Automerge.Connection(
            this.docSet,
            (msg) => {
                this.props.sendMessage(this.props.clientId, msg)
            }
        )

        const initialValue = automergeJsonToSlate({
            "document": { ...this.doc.note }
        })
        const initialSlateValue = Value.fromJSON(initialValue)

        this.state = {
            value: initialSlateValue,
            online: true,
        }
    }
github automerge / mpl / src / mpl / network / peergroup.js View on Github external
getOrCreatePeer(id, name, handler) {
    if (!this.Peers[id]) {
      let peer = new Peer(id, name, handler, this.wrtc)
      this.Peers[id] = peer
      this.connections[id] = new Automerge.Connection(this.docSet, msg => {
        console.log('send to ' + id + ':', msg)
        peer.send(msg)
      })

      peer.on('message', msg => {
        console.log('receive from ' + id + ':', msg)
        this.connections[id].receiveMsg(msg)
      })

      peer.on('closed', () => {
        this.connections[id].close()
        delete this.connections[id]
        delete this.Peers[id]
      })

      this.connections[id].open()
github aslakhellesoy / automerge-codemirror / example / src / app.js View on Github external
const Automerge = require('automerge')
const CodeMirror = require('codemirror')
const automergeCodeMirror = require('../../index')

const docId = 'doc-id'

let leftConnection
let rightConnection

const leftDocSet = new Automerge.DocSet()
leftConnection = new Automerge.Connection(leftDocSet, msg =>
  rightConnection.receiveMsg(msg)
)
leftConnection.open()

const rightDocSet = new Automerge.DocSet()
rightConnection = new Automerge.Connection(rightDocSet, msg =>
  leftConnection.receiveMsg(msg)
)
rightConnection.open()

function createCodeMirror(docSet, domId) {
  const $e = document.getElementById(domId)
  const codeMirror = CodeMirror($e)
  const getDocText = doc => doc.text
  const updateDoc = doc => docSet.setDoc(docId, doc)
  const { automergeHandler, codeMirrorHandler } = automergeCodeMirror({
github cudr / slate-collaborative / packages / client / src / Connection.ts View on Github external
this.socket.on('connect', () => {
      this.connection = new Automerge.Connection(this.docSet, this.sendData)

      this.socket.on('document', this.recieveDocument)

      this.socket.on('operation', this.recieveData)

      this.socket.on('disconnect', this.disconnect)
    })
  }
github aslakhellesoy / automerge-codemirror / stories / index.stories.tsx View on Github external
function createConnectedDocs(count: number, id: string): WatchableDoc[] {
  const docSets: DocSet[] = []
  for (let i = 0; i < count; i++) {
    const docSet = new DocSet()
    docSets.push(docSet)

    if (i > 0) {
      let connectionA: Connection
      let connectionB: Connection

      connectionA = new Connection(docSet, msg => connectionB.receiveMsg(msg))
      connectionB = new Connection(docSets[i - 1], msg =>
        connectionA.receiveMsg(msg)
      )

      connectionA.open()
      connectionB.open()
    }
  }
  return docSets.map(docSet => new DocSetWatchableDoc(docSet, id))
}
github aslakhellesoy / automerge-codemirror / stories / index.stories.tsx View on Github external
function createConnectedDocs(count: number, id: string): WatchableDoc[] {
  const docSets: DocSet[] = []
  for (let i = 0; i < count; i++) {
    const docSet = new DocSet()
    docSets.push(docSet)

    if (i > 0) {
      let connectionA: Connection
      let connectionB: Connection

      connectionA = new Connection(docSet, msg => connectionB.receiveMsg(msg))
      connectionB = new Connection(docSets[i - 1], msg =>
        connectionA.receiveMsg(msg)
      )

      connectionA.open()
      connectionB.open()
    }
  }
  return docSets.map(docSet => new DocSetWatchableDoc(docSet, id))
}
github automerge / automerge-net / connection.js View on Github external
constructor (docSet, socket) {
    this.automerge = new Automerge.Connection(docSet, msg => this.sendMsg(msg))
    this.socket = socket
    this.buffer = Buffer.alloc(0)
    this.automerge.open()
  }