Colab

cyxth collab provides you with an API to easily add real time collaboration to your application in minutes.

import Cyxth from '@cyxth/core';
import Colab from '@cyxth/colab';

const APP_ID = "YOUR_APP_ID";
const cyxth = new Cyxth(APP_ID);
cyxth.register([Colab]);

// authorize user check https://docs.cyxth.com/authorize for more info
await cyxth.connect(USER_TOKEN_SMH);

const colab = await cyxth.colab("https://cdn.cyxth.com/[email protected]");

const stateId = "tasks-01";

await cyxth.start(stateId);

colab.change('tasks').getList().push({
    date: new Date().toIsoString(),
    done: false,
    value: "write the docs",
});

colab.on((change) => {...update tasks})

index

properties

Properties

stateId ?

state id

string

wasmUrl ?

colab wasm url

string

Methods

addUsers

addUsers(users: string[]) : Promise<Status>;

add users to colab state, users will get newcolab notification to join if they are online

emits user:add event

Parameters

users

an array of user ids

string[]
Returns
Promise<Status>

change

change(path: KeyPath) : ChangeContext;

change the local state

Parameters

path

key path

KeyPath
Returns

change context

ChangeContext

configure

configure(config: ColabConfig) : Promise<Status>;

update colab configuration

Parameters

config

new configuration emits cnl:config event

ColabConfig
Returns
Promise<Status>

create

create(stateId: string, initialState?: any, config?: ColabConfig) : Promise<Status>;

create a new colab state

fails if state exists at stateId or user has no permissions to create state.

Parameters

stateId

state id

string
initialState ?

initial state

any
config ?

state configuration

ColabConfig
Returns
Promise<Status>

delUsers

delUsers(users: string[]) : Promise<Status>;

remove user from state

users removed won't be able to join again unless added

emits user:del event

Parameters

users

an array of user ids

string[]
Returns
Promise<Status>

delete

delete(reason?: string) : Promise<any>;

delete a colab state disconnecting all active users, only the owner (one who started it) can do this

Parameters

reason ?

optionally tell others the reason

string
Returns
Promise<any>

getUsers

getUsers(): Promise<User[]>;

get active colab state users

Returns

active users connected to state

Promise<User[]>

join

join(stateId: string) : Promise<any>;

join an existing colab state

emits user:join event

Parameters

stateId

state id

string
Returns

the current state

Promise<any>

leave

leave(): Promise<Status>;

leave a colab state,

emits user:left event

Returns
Promise<Status>

load

load(stateId: string) : Promise<any>;

load a collaboration instance

user must have started or joined the collaboration instance earlier for this to work will fail if user is blocked or state with id does not exist

Parameters

stateId

state id

string
Returns

an upto date state value

Promise<any>

modUsers

modUsers(users: User[]) : Promise<Status>;

moderate users in state deciding who has edit, view, no-access permissions

emits user:mod event

check concepts for more about permissions

Parameters

users

an array of users

User[]
Returns
Promise<Status>

on

on<K extends keyof ColabEventMap>(event: K, handler: (ev: ColabEventMap[K]) : any;) : void;

listen for colab events

colab.on("user:join",(user) => {
    //... update list
})

Parameters

event

event

K
handler

event handler function

(ev: ColabEventMap[K]) : any;
Returns
void

presence

presence(data: any) : void;

send presence data to all connected users i.e text selections and cursor positions to enhance the collaboration experience by conveying user intent

other users can listen using on presence event

colab.on('presence', (data) => {
		// ... convey user intent
})

Parameters

data

and presense data

any
Returns
void