Coder Social home page Coder Social logo

p2p-edit's Introduction

p2p-edit

p2p-edit is a collaborative text editor powered by WebRTC and OT. Local operations to the document are reconciled by remote clients by use of a data structure called text-tp2.

This project is only a POC at this stage and will break often.

Setup

git clone https://github.com/ericmcdaniel/p2p-edit

This application has two components; a signaling server and a client. The signaling server must communicate with peers in order to establish WebRTC connections between them.

Client

The client is located at dist/p2p-edit.js. It is exported as a UMD module and is compatible with most module loaders.

client()

The method exported from the client module will create a new instance of the p2p-edit client. Client instances have a standard EventEmitter API and will emit the 'ready' event when they are ready to create text models.

client#model(id)

Text models (documents) can be created or retrieved with the client.model method. Simple string identifiers are used to namespace documents. Models currently only implement simple inserts and deletes via their insert and delete methods, respectively.

Currently you must wait for the 'ready' event on client instances before you create a model.

e.g.

client.on('ready', () => {
    this.model = client.model('/' + this.room);
    // Install an adapter. More on that below.
    this.model.adapter(cmAdapter, this.editor);
});

A client can have multiple active models. When you make a change to the model (via adapter or API), the change is integrated into the text-tp2 structure and broadcast to other clients.

client#adapter()

The client can be extended with adapters to translate proprietary changes to work with text-tp2. An example adapter for the CodeMirror library can be found at ex/client/cm-adapter.js.

e.g. An example adapter with a 1-to-1 mapping to text-tp2 operations;

function adapter(model, sync, dependency) {
    const handleChange = (change) => {
        if (change.type === 'delete') {
            model.delete(change.from, change.count);
        } else if (change.type === 'insert') {
            model.insert(change.from, change.text);
        }
        // Call update on other adapter instances within same model.
        sync();
    }

    const install = () => {
        dependency.on('change', handleChange);
        model.addListener('remoteOp', update);
    }

    const uninstall = () => {
        dependency.off('change', handleChange);
        model.removeListener('remoteOp', update);
    }

    const update = () => dependency.setValue(model.get());

    return { install, uninstall, update };
}

Adapters will be executed with the model, a sync function, and any additional arguments you pass to client#adapter(). They should return an object with install, uninstall and update functions.

A typical pattern would be to subscribe to changes on your dependency (like a textarea), map those changes to model inserts/deletes, and call sync.

The update function is triggered when handshake data from a peer is recieved, and when sync is called by other instances of the same adapter.

Server

Run npm run signal to start the signaling server.

Examples

Run npm run ex to run the example.

Notes

There is currently little implemented in the way of concurrency control, so you're gonna have a bad time if two people are typing at once.

p2p-edit's People

Contributors

3mcd avatar fijiwebdesign avatar

Stargazers

Ichii avatar Joshua Bailey avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.