Colab
Easily turn your app multiplayer with cyxth colab.
import Cyxth from '@cyxth/core';
import Colab from '@cyxth/colab';
const APP_URL = "my-app.cyxth.com"; // appid
const TOKEN = "ey....."; // authorized user token
const cyxth = await new Cyxth(APP_ID, [Colab]).connect(TOKEN);
const colab = await cyxth.colab("https://cdn.cyxth.com/colab@0.0.1.wasm");
const stateId ="tasks-uxi"; // state id
await colab.createOrJoin(stateId); // create or join state
const tree = colab.changeContext().tree();
tree.setDefaultHandler((change,userId) => {
// ...handle change
})
const state: {tasks: Task[]} = tree.state;
const createTask = (task: Task) => {
state.tasks.push(task);
}
const markDone = (index: number, done: boolean) => {
state.tasks[index].done = done;
}
// ....
index
methods
Properties
offline
offline status
booleanstateId ?
state id
stringwasmUrl ?
colab wasm url
stringMethods
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>configure
configure (config: ColabConfig) : Promise<Status> ;update colab configuration
Parameters
Returns
Promise<Status>create
create (stateId: string, 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
stringReturns
Promise<Status>createOrJoin
createOrJoin (stateId: string, config?: ColabConfig) : Promise<Status> ;create a state or join if it exists
Parameters
stateId
stringconfig ?
ColabConfigReturns
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
stringReturns
Promise<any>getUsers
getUsers () : Promise<User[]> ;get active colab state users
Returns
active users connected to state
Promise<User[]>join
join (stateId: string) : Promise<Status> ;join an existing colab state
emits user:join event
Parameters
stateId
state id
stringReturns
Promise<Status>load
load (stateId: string) : Promise<Status> ;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
stringReturns
Promise<Status>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
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
Returns
voidpresence
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
anyReturns
voidtoggleOffline
toggleOffline () : Promise<void> ;toggle offline status.
cyxth will automatically set offline status when a user goes offline or when the connection is too slow
listen for user:offline event in cyxth core or colab
Returns
Promise<void>