Overview
ChatStateService is the centralized Angular service for managing active chat state across the CometChat Angular UIKit. It provides a single source of truth for the currently active chat entity (User, Group, or Conversation) in your application.
The service is provided at the root level (providedIn: 'root') and is available throughout your application via Angular dependency injection.
Hybrid Approach
ChatStateService implements the Hybrid Approach pattern:
- Service-based (default) — Components automatically subscribe to
ChatStateServiceand react to state changes. - Props override — When
@Input()bindings are provided, they take priority over service state for that component instance.
Import
Signals (Readonly)
ChatStateService exposes Angular Signals for synchronous, fine-grained reactivity with automatic dependency tracking and efficient change detection.
Observables
For RxJS-based reactivity and integration with existing observable-based code,ChatStateService provides Observable streams derived from the underlying signals via toObservable().
Setter Methods
Setter methods update the active chat state. They enforce mutual exclusivity — only one chat entity (User or Group) can be active at a time.Getter Methods (Snapshots)
Getter methods return the current value of a signal as a one-time read without subscribing to changes. Use these in event handlers or one-time operations.Mutual Exclusivity
ChatStateService enforces that only one chat entity — a CometChat.User or a CometChat.Group — can be active at any given time:
- Calling
setActiveUser(user)with a non-null value automatically setsactiveGrouptonull. - Calling
setActiveGroup(group)with a non-null value automatically setsactiveUsertonull. - Calling
setActiveConversation(conversation)extracts the entity and delegates tosetActiveUser()orsetActiveGroup(), which applies the same rule. - Calling
clearActiveChat()resets all three signals tonull.
Usage Examples
Signal-Based (Recommended)
Signals provide synchronous, fine-grained reactivity with automatic dependency tracking. This is the recommended approach for new code.- Component
Observable-Based
Observables integrate with existing RxJS-based code and theasync pipe.
- Component
Snapshot (One-Time Read)
Use getter methods when you need the current value once without subscribing to changes — for example, inside event handlers.- Component
Hybrid Approach (Props Override Service)
Components can accept optional@Input() bindings that take priority over ChatStateService state. This lets you override the service for specific component instances while keeping the default wiring elsewhere.
When
@Input() bindings are provided, they take priority over ChatStateService
state for that component instance. Other components continue to read from the service.- Component
- Usage
Related Components
The following Chat-Aware Components integrate withChatStateService. Each supports the Hybrid Approach — they auto-subscribe to the service by default and accept @Input() overrides.
See Also
- State Management Guide — Hybrid Approach architecture and decision matrix
- Service Reference — Overview of all documented services
- FormatterConfigService — Text formatter configuration service
- RichTextEditorService — Rich text editor configuration service