Skip to content

Groups

Member management

You can add or remove members from a group.

Remove members from a group

// get the group
const { group } = context;
await group.sync();
//By address
await group.removeMembers(userAddresses);
//By inboxId
await group.removeMembersByInboxId(removedInboxes);

Removed member

When a member is removed from a group it will emit a group_updated event with a removedInboxes array containing the addresses of the users removed.

if (typeId === "group_updated") {
  const { removedInboxes } = context.message.content;
  if (removedInboxes?.length > 0) {
    for (const inbox of removedInboxes) {
      console.log(`User removed: ${inbox.inboxId}`);
    }
  }
}

Add members to a group

// get the group
const { group } = context;
await group.sync();
//By address
await group.addMembers(userAddresses);
//By inboxId
await group.addMembersByInboxId(addedInboxes);

Added member

When a member is added to a group it will emit a group_updated event with a addedInboxes array containing the addresses of the users added.

if (typeId === "group_updated") {
  const { addedInboxes } = context.message.content;
 
  if (addedInboxes?.length > 0) {
    for (const inbox of addedInboxes) {
      console.log(`User added: ${inbox.inboxId}`);
    }
  }
}

For more details, see XMTP's Groups documentation.