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
CometChatProvidersetup with valid credentials - An existing group chat screen using
CometChatMessageListandCometChatMessageComposer
Components Used
| Component / API | Purpose |
|---|---|
CometChatMessageList | Displays group messages (publishes ui:open-chat internally via “Message Privately” option) |
CometChatMessageComposer | Text input for the private conversation |
CometChatMessageHeader | Displays user/group info |
useCometChatEvents | Subscribe to ui:open-chat event |
ui:open-chat | UI 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 inCometChatProvider.
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
ConnectCometChatConversations 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
WhenprivateUser 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:| Scenario | Behavior |
|---|---|
| User messages themselves | Disable the private message option for the logged-in user’s own messages |
| User is blocked | Check getBlockedByMe() before opening the private panel and show a prompt |
| Private panel already open | Replace the current private user with the newly selected one |
| Group conversation changes | Close the private panel when switching to a different conversation |
Complete Example
App.tsx
Next Steps
- Message Composer — customize the composer for private chats
- Conversations — manage the conversations list
- Users — browse and select users directly
- CometChatProvider — learn about provider configuration