Implement the Co-Doing API

The Google Meet Live Sharing 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 below.

This guide explains how to implement the Co-Doing API.

Create a CoDoingClient

To get started, create a CoDoingClient from the AddonSession you created in Get started.

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 CoDoingClient.broadcastStateUpdate.

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.