Implement the Co-Doing API

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 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 Deploy 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 object, which serves as the entry point for Google Meet co-activities:

TypeScript

const session = await window.meet.addon.createAddonSession({
    cloudProjectNumber: "CLOUD_PROJECT_NUMBER",
});

Replace CLOUD_PROJECT_NUMBER with the project number of your Google Cloud project.

Create a co-doing client

To get started, create a CoDoingClient object from your AddonSession.

To create a CoDoingClient, call the createCoDoingClient() method and provide a CoDoingDelegate object.

The CoDoingDelegate is how the Co-Doing API updates your app whenever it has a new state available. It's expected that, when the onCoDoingStateChanged() method is called, your app 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 the new state to your ongoing CoDoing activity
    },
  });

Replace ACTIVITY_TITLE with the title of your activity.

Manage current state

When users take action in your app, it's expected that your app immediately calls the broadcastStateUpdate() method.

The following code sample shows an implementation of the broadcastStateUpdate() method:

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.