The Co-Doing API is used to synchronize arbitrary data between meeting participants. This can be any data that your app depends on.
You must serialize the data down to a Uint8Array
for it to be transmitted. For
more information, see the JavaScript standard library
reference.
If you aren't sure how to serialize your data, review the code samples following.
This guide explains how to implement the Co-Doing API.
Get started
To use the Co-Doing API, you first must Build a Meet Add-on. Once you've completed those steps, you can start using the Co-Doing API from within your new add-on.
To use the Co-Doing API, start by getting an
AddonSession
, which
serves as the entry point for co-activities:
TypeScript
const session = await window.meet.addon.createAddonSession({
cloudProjectNumber: "CLOUD_PROJECT_NUMBER",
});
Create a CoDoing client
To get started, create a
CoDoingClient
from your AddonSession
.
To create a CoDoingClient
, call the
AddonSession.createCoDoingClient
method and provide a
CoDoingDelegate
.
The CoDoingDelegate
is how the Co-Doing API
updates your application whenever it has a new state available. It's expected
that, when the
CoDoingDelegate.onCoDoingStateChanged
method is called, your application immediately applies the new state.
The following code sample shows how to use the Co-Doing API:
TypeScript
interface MyState {
someString: string;
someNumber: number;
}
/**
* Serialize/deserialize using JSON.stringify
* You can use any method you want; this is included for as an example
*/
function toBytes(state: MyState): Uint8Array {
return new TextEncoder().encode(JSON.stringify(state));
}
function fromBytes(bytes: Uint8Array): MyState {
return JSON.parse(new TextDecoder().decode(bytes)) as MyState;
}
const coDoingClient = await addonSession.createCoDoingClient({
activityTitle: "ACTIVITY_TITLE",
onCoDoingStateChanged(coDoingState: CoDoingState) {
const newState = fromBytes(coDoingState.bytes);
// This function should apply newState to your ongoing CoDoing activity
},
});
Replace ACTIVITY_TITLE with the title of your activity.
Manage current state
When users take action in your application, it's expected that your application
immediately calls the
CoDoingClient.broadcastStateUpdate
method.
The following code sample shows an implementation of the
CoDoingClient.broadcastStateUpdate
:
TypeScript
const myState: MyState = {
someString: "SOME_STRING",
someNumber: 0
};
document.getElementById('some-button').onClick(() => {
myState.someNumber = myState.someNumber + 1;
coDoingClient.broadcastStateUpdate({ bytes: toBytes(myState) });
});
Replace SOME_STRING with the app's current state.