Skip to main content

Goal

By the end of this guide you will have a group chat interface where users can select a group member and open a private one-on-one conversation with them — without leaving the current screen. This is useful for side conversations, follow-ups, or sensitive messages that shouldn’t be shared with the entire group.

Prerequisites

  • Completed the Integration Guide guide
  • A running CometChatProvider setup with valid credentials
  • An existing group chat screen using CometChatMessageList and CometChatMessageComposer

Components Used

Component / APIPurpose
CometChatMessageListDisplays group messages (publishes ui:open-chat internally via “Message Privately” option)
CometChatMessageComposerText input for the private conversation
CometChatMessageHeaderDisplays user/group info
useCometChatEventsSubscribe to ui:open-chat event
ui:open-chatUI event published when user clicks “Message Privately”

Step 1: Set up the main layout with provider

Start with a layout that includes a conversations sidebar and a main chat area wrapped in CometChatProvider.
App.tsx

Step 2: Track group and private chat state

Maintain state for the active group conversation and a separate state for the private user when a user initiates a private message.
ChatWithPrivateMessaging.tsx

Step 3: Handle conversation selection

Connect CometChatConversations to switch between user and group chats. When switching conversations, close any open private message panel.

Step 4: Initiate a private message from a group member

Use the message header or a custom action to let users pick a group member for private messaging. You can fetch the user from a member click event or from the message sender’s info.

Step 5: Listen for the ui:open-chat event

Use useCometChatEvents to subscribe to the ui:open-chat event, which is published internally when a user clicks “Message Privately” from the context menu. When the event fires, extract the user and open the private panel.

Step 6: Render the private message panel

When privateUser is set, show a side panel with a private one-on-one chat. This panel includes a header, message list, and composer scoped to the private user.

Step 7: Handle edge cases

Consider these scenarios when implementing private messaging from a group:
ScenarioBehavior
User messages themselvesDisable the private message option for the logged-in user’s own messages
User is blockedCheck getBlockedByMe() before opening the private panel and show a prompt
Private panel already openReplace the current private user with the newly selected one
Group conversation changesClose the private panel when switching to a different conversation

Complete Example

App.tsx

Next Steps