For AI agents: Find a documentation index at https://talkjs.com/llms.txt (full content at https://talkjs.com/llms-full.txt). Get a markdown version of any page by appending .md to its URL path.

Create a group chat

Build a group chat with three or more participants.

View as Markdown

Build on your first TalkJS chat by creating a new conversation for Frank, Nina, and Juliana.

Prerequisites

Follow the getting-started guide for JavaScript, Angular, Vue, or Svelte until you can open the conversation between Frank and Nina. Use the same test app and keep browser synchronization enabled for users and conversations.

Create a group conversation

Change conversationId from 'my_conversation' to 'project_group_chat'. The new ID gives the group its own message history. If you're following the plain HTML example, also change the <t-chatbox> element's conversation-id attribute to "project_group_chat". The Angular, Vue, and Svelte examples already bind that attribute to conversationId.

Keep userId set to 'frank' and otherUserId set to 'nina'.

// Keep the existing `getTalkSession` call and replace the user
// and conversation setup that follows it with this code:
async function createGroupChat() {
  await session.currentUser.createIfNotExists({ name: 'Frank' });
  await session.user(otherUserId).createIfNotExists({ name: 'Nina' });
  await session.user('juliana').createIfNotExists({ name: 'Juliana' });

  const conversation = session.conversation(conversationId);
  await conversation.createIfNotExists({ subject: 'Project chat' });
  await conversation.participant(otherUserId).createIfNotExists();
  await conversation.participant('juliana').createIfNotExists();
}

createGroupChat();

Creating the conversation also adds Frank, the current user. Membership is specific to each conversation, so Nina needs to join the new group even though she already belongs to the original chat. The setup creates Juliana's user record and waits for the conversation to exist before adding Nina and Juliana.

Try the group chat

Send a message in Frank's chatbox. To try it as Juliana, render a second <t-chatbox> with the same app-id and conversation-id and set its user-id to "juliana". You should see Frank's message and be able to reply as Juliana. If you're using a <t-inbox>, pass the group conversation ID as selected-conversation-id.

Next steps

In your app, run membership changes when a group is created or someone is added. Running this tutorial's setup on every visit would re-add participants who have left or been removed.

Before going live, enable authentication. If it's already enabled, authenticate the user's session before making Data API requests or mounting Components. Enforce membership rules on your backend if users shouldn't be able to change membership directly, and configure browser synchronization accordingly.