Create a group chat
Build a group chat with three or more participants.
Build on your first TalkJS chat by creating a new conversation for Frank, Nina, and Juliana.
Follow the getting-started guide for React or Next.js until you can open the conversation between Frank and Nina. Use the same test app and keep browser synchronization enabled for users and conversations.
In your existing Chat component, change conversationId from 'new_conversation' to 'project_group_chat'. The new ID gives the group its own message history, and the existing chatbox already uses that variable to select which conversation to display.
Replace the existing useEffect with this one. Keep userId set to 'frank' and otherUserId set to 'nina'.
useEffect(() => {
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();
}, [session, conversationId, otherUserId]);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 effect creates Juliana's user record and waits for the conversation to exist before adding Nina and Juliana.
Send a message in Frank's chatbox. To try it as Juliana, render a second chatbox with the same appId and conversationId and set its userId prop to "juliana". You should see Frank's message and be able to reply as Juliana. If you're using an Inbox, pass the group conversation ID as selectedConversationId.
- Add users to a group chat to manage participants and their access settings.
- Remove users from a group chat to remove participants or let users leave.
- Customize your chat with styles and theme components.
In your actual app, you should only run membership changes once a group is created or when someone new is added. Running the participant calls 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.