GithubHelp home page GithubHelp logo

y-protocols's Introduction

Yjs Protocols

Binary encoding protocols for syncing, awareness, and history inforamtion

This API is unstable and subject to change.

API

Awareness Protocol

import * as awarenessProtocol from 'y-protocols/awareness.js'

The Awareness protocol implements a simple network agnostic algorithm that manages user status (who is online?) and propagate awareness information like cursor location, username, or email address. Each client can update its own local state and listen to state changes of remote clients.

Each client has an awareness state. Remote awareness are stored in a Map that maps from remote client id to remote awareness state. An awareness state is an increasing clock attached to a schemaless json object.

Whenever the client changes its local state, it increases the clock and propagates its own awareness state to all peers. When a client receives a remote awareness state, and overwrites the clients state if the received state is newer than the local awareness state for that client. If the state is null, the client is marked as offline. If a client doesn't receive updates from a remote peer for 30 seconds, it marks the remote client as offline. Hence each client must broadcast its own awareness state in a regular interval to make sure that remote clients don't mark it as offline.

awarenessProtocol.Awareness Class

const awareness = new awarenessProtocol.Awareness()
clientID:number
A unique identifier that identifies this client.
getLocalState():Object<string,any>|null
Get the local awareness state.
setLocalState(Object<string,any>|null)
Set/Update the local awareness state. Set `null` to mark the local client as offline.
setLocalStateField(string, any)
Only update a single field on the local awareness object. Does not do anything if the local state is not set.
getStates():Map<number,Object<string,any>>
Get all client awareness states (remote and local). Maps from clientID to awareness state.
on('change', ({ added: Array<number>, updated: Array<number> removed: Array<number> }, [transactionOrigin:any]) => ..)
Listen to remote and local state changes on the awareness instance.
on('update', ({ added: Array<number>, updated: Array<number> removed: Array<number> }, [transactionOrigin:any]) => ..)
Listen to remote and local awareness changes on the awareness instance. This event is called even when the awarenes state does not change.

License

The MIT License © Kevin Jahns

y-protocols's People

Contributors

dependabot[bot] avatar dmonad avatar flamenco avatar nemanja-tosic avatar toomim avatar ulion 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.