|
| 1 | +--- |
| 2 | +title: "One-to-One / Group Chat" |
| 3 | +sidebarTitle: "One-to-One / Group Chat" |
| 4 | +description: "Build a focused one-to-one or group chat experience in Angular with CometChat UI Kit." |
| 5 | +--- |
| 6 | + |
| 7 | +<Accordion title="AI Integration Quick Reference"> |
| 8 | + |
| 9 | +| Field | Value | |
| 10 | +| --- | --- | |
| 11 | +| Package | `@cometchat/chat-uikit-angular` | |
| 12 | +| Framework | Angular | |
| 13 | +| Components | `CometChatMessageHeaderComponent`, `CometChatMessageListComponent`, `CometChatMessageComposerComponent` | |
| 14 | +| Layout | Single chat window — no sidebar, no conversation list | |
| 15 | +| Prerequisite | Complete [Angular Integration](/ui-kit/angular/v5/integration) Steps 1–5 first | |
| 16 | +| Pattern | Support chat, embedded widgets, focused messaging | |
| 17 | + |
| 18 | +</Accordion> |
| 19 | + |
| 20 | +This guide builds a single chat window — no sidebar, no conversation list. Users go directly into a one-to-one or group chat. Good for support chat, embedded widgets, or any focused messaging experience. |
| 21 | + |
| 22 | +This assumes you've already completed [Angular Integration](/ui-kit/angular/v5/integration) (project created, UI Kit installed, init + login working, CSS imported). |
| 23 | + |
| 24 | +<Frame> |
| 25 | + <img src="/images/4d640baf-chat_experience_one_on_one-db9d6d7716241c59bb026625b05019fe.png" /> |
| 26 | +</Frame> |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +## What You're Building |
| 31 | + |
| 32 | +Three components stacked vertically: |
| 33 | + |
| 34 | +1. **Chat header** — displays recipient name, avatar, online status, and optional call buttons |
| 35 | +2. **Message list** — real-time chat history with scrolling |
| 36 | +3. **Message composer** — text input with media, emojis, and reactions |
| 37 | + |
| 38 | +Everything goes in `AppComponent` — no extra files needed. |
| 39 | + |
| 40 | +--- |
| 41 | + |
| 42 | +## Step 1 — Update AppComponent |
| 43 | + |
| 44 | +The app fetches a user (or group) on mount, sets it as the active entity via `ChatStateService`, and renders the three message components. The components auto-subscribe to the service — no prop passing needed. |
| 45 | + |
| 46 | +```ts title="src/app/app.component.ts" expandable |
| 47 | +import { Component, inject, OnInit } from "@angular/core"; |
| 48 | +import { |
| 49 | + CometChatUIKit, |
| 50 | + CometChatMessageHeaderComponent, |
| 51 | + CometChatMessageListComponent, |
| 52 | + CometChatMessageComposerComponent, |
| 53 | + ChatStateService, |
| 54 | +} from "@cometchat/chat-uikit-angular"; |
| 55 | +import { CometChat } from "@cometchat/chat-sdk-javascript"; |
| 56 | + |
| 57 | +@Component({ |
| 58 | + selector: "app-root", |
| 59 | + standalone: true, |
| 60 | + imports: [ |
| 61 | + CometChatMessageHeaderComponent, |
| 62 | + CometChatMessageListComponent, |
| 63 | + CometChatMessageComposerComponent, |
| 64 | + ], |
| 65 | + template: ` |
| 66 | + @if (chatStateService.activeUser() || chatStateService.activeGroup()) { |
| 67 | + <div class="chat-window"> |
| 68 | + <cometchat-message-header></cometchat-message-header> |
| 69 | + <cometchat-message-list></cometchat-message-list> |
| 70 | + <cometchat-message-composer></cometchat-message-composer> |
| 71 | + </div> |
| 72 | + } @else { |
| 73 | + <div class="empty-conversation"> |
| 74 | + Loading chat... |
| 75 | + </div> |
| 76 | + } |
| 77 | + `, |
| 78 | + styles: ` |
| 79 | + .chat-window { |
| 80 | + display: flex; |
| 81 | + flex-direction: column; |
| 82 | + height: 100vh; |
| 83 | + width: 100vw; |
| 84 | + } |
| 85 | + .empty-conversation { |
| 86 | + height: 100vh; |
| 87 | + width: 100%; |
| 88 | + display: flex; |
| 89 | + justify-content: center; |
| 90 | + align-items: center; |
| 91 | + background: var(--cometchat-background-color-03, #F5F5F5); |
| 92 | + color: var(--cometchat-text-color-secondary, #727272); |
| 93 | + font: var(--cometchat-font-body-regular, 400 14px Roboto); |
| 94 | + } |
| 95 | + .cometchat .cometchat-message-composer { |
| 96 | + border-radius: 0px; |
| 97 | + } |
| 98 | + `, |
| 99 | +}) |
| 100 | +export class AppComponent implements OnInit { |
| 101 | + chatStateService = inject(ChatStateService); |
| 102 | + |
| 103 | + ngOnInit(): void { |
| 104 | + CometChatUIKit.getLoggedinUser().then((user: CometChat.User | null) => { |
| 105 | + if (!user) { |
| 106 | + CometChatUIKit.login("cometchat-uid-1") |
| 107 | + .then(() => this.loadUser()) |
| 108 | + .catch(console.log); |
| 109 | + } else { |
| 110 | + this.loadUser(); |
| 111 | + } |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + private loadUser(): void { |
| 116 | + // Fetch the user whose chat you want to load |
| 117 | + const UID = "cometchat-uid-2"; |
| 118 | + |
| 119 | + CometChat.getUser(UID).then( |
| 120 | + (user: CometChat.User) => { |
| 121 | + this.chatStateService.setActiveUser(user); |
| 122 | + }, |
| 123 | + (error: CometChat.CometChatException) => { |
| 124 | + console.error("User fetch failed:", error); |
| 125 | + } |
| 126 | + ); |
| 127 | + |
| 128 | + // To load a group chat instead, uncomment below: |
| 129 | + // const GUID = "GUID"; |
| 130 | + // CometChat.getGroup(GUID).then( |
| 131 | + // (group: CometChat.Group) => { |
| 132 | + // this.chatStateService.setActiveGroup(group); |
| 133 | + // }, |
| 134 | + // (error: CometChat.CometChatException) => { |
| 135 | + // console.error("Group fetch failed:", error); |
| 136 | + // } |
| 137 | + // ); |
| 138 | + } |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +Key points: |
| 143 | +- `CometChat.getUser(UID)` fetches the user object from the SDK — you need a real user object, not a manually constructed one. |
| 144 | +- `chatStateService.setActiveUser(user)` sets the active entity — all message components auto-subscribe and render accordingly. |
| 145 | +- Setting a user automatically clears any active group (mutual exclusivity). |
| 146 | + |
| 147 | +--- |
| 148 | + |
| 149 | +## Switching Between User and Group Chat |
| 150 | + |
| 151 | +To load a group chat instead of one-to-one, replace the `setActiveUser` call with `setActiveGroup`: |
| 152 | + |
| 153 | +```ts highlight={1} |
| 154 | +const GUID = "GUID"; // Replace with your actual Group ID |
| 155 | + |
| 156 | +CometChat.getGroup(GUID).then( |
| 157 | + (group: CometChat.Group) => { |
| 158 | + this.chatStateService.setActiveGroup(group); |
| 159 | + }, |
| 160 | + (error: CometChat.CometChatException) => { |
| 161 | + console.error("Failed to fetch group:", error); |
| 162 | + } |
| 163 | +); |
| 164 | +``` |
| 165 | + |
| 166 | +You can also determine this dynamically — for example, based on a route parameter or a selection from another part of your app. |
| 167 | + |
| 168 | +--- |
| 169 | + |
| 170 | +## Step 2 — Run the Project |
| 171 | + |
| 172 | +```bash |
| 173 | +ng serve |
| 174 | +``` |
| 175 | + |
| 176 | +You should see the chat window load with the conversation for the UID you set. |
| 177 | + |
| 178 | +--- |
| 179 | + |
| 180 | +## Next Steps |
| 181 | + |
| 182 | +<CardGroup cols={2}> |
| 183 | + <Card title="Theming" icon="paintbrush" href="/ui-kit/angular/v5/customization/theming"> |
| 184 | + Customize colors, fonts, and styles to match your brand |
| 185 | + </Card> |
| 186 | + <Card title="Components Overview" icon="grid-2" href="/ui-kit/angular/v5/components/overview"> |
| 187 | + Browse all prebuilt UI components |
| 188 | + </Card> |
| 189 | + <Card title="Angular Integration" icon="angular" href="/ui-kit/angular/v5/integration"> |
| 190 | + Back to the main setup guide |
| 191 | + </Card> |
| 192 | + <Card title="Core Features" icon="comments" href="/ui-kit/angular/v5/core-features"> |
| 193 | + Chat features included out of the box |
| 194 | + </Card> |
| 195 | +</CardGroup> |
0 commit comments