Chat object
Once you initialize the Chat SDK and create its instance, you'll get immediate access to the Chat entity from which you can call the PubNub server.
To communicate with PubNub, you can use various methods. For example, you can use deleteChannel() to remove a given channel or wherePresent() to check which channels a given user is subscribed to.
By calling methods on the ChatImpl object, you create further objects like Channel, User, Message, Membership, ThreadChannel, and ThreadMessage. These objects also expose Chat API under various methods, letting you perform CRUD operations on messages, channels, users, the related user-channel membership, and many more.
Each of these entities comes with a set of "read-only" parameters you define when creating, modifying, and deleting specific channels, users, messages, or memberships in your chat app.
Properties
The ChatImpl class implements the Chat protocol and takes the following properties:
1public final class ChatImpl {
2 public let pubnub: PubNub
3 public let config: ChatConfiguration
4}
| Parameter | Description |
|---|---|
pubnubType: PubNub | Initialized instance of the PubNub Swift SDK. pubnub lets you access any Swift SDK method. For example, if you want to call a method available in the App Context API, you'd use chat.pubnub.getAllUUIDMetadata(). |
→ configurationType: PubNubConfiguration | Mandatory PubNub configuration parameters. |
→ publishKeyType: String | Specifies the key used to publish messages on a channel. |
→ subscribeKeyType: String | Specifies the key used to subscribe to a channel. |
→ userIdType: String | Unique User ID that becomes your app's current user. It's a string of up to 92 characters that identifies a single client (end user, device, or server) that connects to PubNub. Based on User ID, PubNub calculates pricing for your apps' usage. User ID should be persisted and remain unchanged. If you don't set userId, you won't be able to connect to PubNub. |
configType: ChatConfiguration | ChatConfiguration 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. |
ChatConfiguration includes these properties:
1public struct ChatConfiguration {
2 public var logLevel: LogLevel
3 public var typingTimeout: Int
4 public var storeUserActivityInterval: Int
5 public var storeUserActivityTimestamps: Bool
6 public var pushNotificationsConfig: PushNotificationsConfig
7 public var rateLimitFactor: Int
8 public var rateLimitPerChannel: [ChannelType: Int64]
9 public var customPayloads: CustomPayloads?
10}
PushNotificationsConfig includes these properties:
1public struct PushNotificationsConfig {
2 public var sendPushes: Bool
3 public var deviceToken: String?
4 public var deviceGateway: PubNub.PushService
5 public var apnsTopic: String?
6 public var apnsEnvironment: PubNub.PushEnvironment
7}
To initialize the Swift Chat SDK, you must provide two parameters: 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.
Click on each method for more details.
createUser()createDirectConversation()createGroupConversation()createPublicConversation()currentUserdeleteChannel()deleteUser()emitEvent()getEventsHistory()getChannel()getChannels()getCurrentUserMentions()fetchUnreadMessagesCounts()getUser()getUsers()getPushChannels()init()isPresent()listenForEvents()markAllMessagesAsReadregisterPushChannels()unregisterPushChannels()unregisterAllPushChannels()updateChannel()updateUser()wherePresent()whoIsPresent()
Use case
For example, you can use the Chat-related methods to: