Chat object
The Chat object is your entry point to the Chat SDK after initialization. Use the Chat object to create and manage channels, users, messages, memberships, and other entities.
Key capabilities:
- Create and delete channels and users
- Track user presence across channels
- Send custom events
- Access the underlying JavaScript SDK
Properties
The Chat object has the following properties:
1class Chat {
2 sdk: PubNub,
3 config: ChatConfig,
4 mutedUsersManager: MutedUsersManager
5}
| Parameter | Description |
|---|---|
sdkType: PubNub | sdk lets you access any JavaScript SDK method. For example, if you want to call a method available in the App Context API, you'd use chat.sdk.objects.getAllUUIDMetadata(). |
configType: ChatConfig | ChatConfig contains chat app configuration settings, such as saveDebugLog or typingTimeout that you provide when initializing your chat app with the init() method. You can later directly access these properties, like: chat.storeUserActivityInterval. |
mutedUsersManagerType: MutedUsersManager | Manages the client-side muted users list. Available when syncMutedUsers is enabled. |
ChatConfig includes these properties:
1type ChatConfig = {
2 // For logging errors
3 saveDebugLog: boolean;
4 // For typing indicator
5 typingTimeout: number;
6 // Two properties for enabling user's global presence & for tracking the user's last online activity
7 storeUserActivityInterval: number;
8 storeUserActivityTimestamps: boolean;
9 // For push notifications
10 pushNotifications: {
11 sendPushes: boolean;
12 deviceToken?: string;
13 deviceGateway: "apns2" | "gcm";
14 apnsTopic?: string;
15 apnsEnvironment: "development" | "production";
show all 24 linesTo initialize the Chat SDK, you must provide three parameters: publishKey, subscribeKey, and userId, while all other settings (mentioned above) are optional. Read the Configuration page for details.
Methods
You can call the following methods on the Chat object.
Regular methods
createUser()createDirectConversation()createGroupConversation()createPublicConversation()- (getter)
currentUser deleteChannel()deleteUser()destroy()downloadDebugLog()fetchUnreadMessagesCounts()getChannel()getChannelGroup()getChannels()getChannelSuggestions()getCurrentUserMentions()getEventsHistory()getPushChannels()getUser()getUsers()getUserSuggestions()init()isPresent()markAllMessagesAsReadregisterPushChannels()removeChannelGroup()setRestrictions()unregisterPushChannels()unregisterAllPushChannels()updateChannel()updateUser()wherePresent()whoIsPresent()(deprecated)emitEvent()
Event listeners
addConnectionStatusListener()disconnectSubscriptions()reconnectSubscriptions()(deprecated)listenForEvents()
Use case
For example, you can use the Chat object methods to: