Chat Provider for PubNub Chat Components for iOS

ChatProvider is the general interface for UI and data operations across the component library with many objects implemented as Generics. ChatProvider assures an overarching Type structure to coordinate the underlying data, services, themes, and view models.

Configuration

You can configure ChatProvider using the following parameters.

Required parameters

Required parameters don't have default values. If a parameter has a default value, it's optional.

ParameterTypeDefault valueDescription
datastoreConfigurationDatastoreConfiguration.pubnubDefaultConfiguration used when creating or connecting to the local CoreData instance.
pubnubConfigurationPubNubConfigurationn/aConfiguration that's used to create a managed PubNub instance.

Persistent database

Chat components rely on a local CoreData instance to manage the flow of data to the components. Network data, also known as Model objects, is fetched from remote APIs and then converted to Managed objects for storage inside of the CoreData instance. Meanwhile, the UI components create local connections to the CoreData instance to populate their views with data. CoreData automatically updates the displayed data as it changes or as new data is added.

To learn more about CoreData, refer to the official Apple docs.

Structure

Chat components are configured with a predefined Managed Object Model that define the NSManagedObject objects that implement the default ManagedChatEntities.

To allow for the customization of data, the default Managed object schema can be replaced if the custom schema implements the ManagedChatEntities protocol.

Managed vs Model objects

The following table describes the relationships between the Managed and Model objects:

Default Managed objectManagedChatEntities protocolModel objectGeneric custom dataDescription
PubNubManagedChannelManagedChatChannelChatChannelChannelCustomDataChannel data objects
PubNubManagedUserManagedChatUserChatUserUserCustomDataUser data objects
PubNubManagedMemberManagedChatMemberChatMemberChatCustomDataRelationship between channel and user data that also tracks presence.
PubNubManagedMessageManagedChatMessageChatMessageChatCustomDataMessage data objects
PubNubManagedMessageActionManagedChatMessageActionChatMessageActionChatCustomDataMessage actions data objects

Basic example

This example creates default ChatProvider using the default PubNub Type structure.

var pubnubConfig = PubNubConfiguration(
publishKey: "pub-c-key",
subscribeKey: "sub-c-key",
userId: "userId-of-current-user"
)

let chatProvider = ChatProvider<VoidCustomData, PubNubManagedChatEntities>(
pubnubConfiguration: pubnubConfig
)

Type aliases

You can use a typealias to make typing out the full Generic object name easier. For example, the above ChatProvider object has a public typealias of PubNubChatProvider, so PubNubChatProvider is interchangeable with any instance of ChatProvider<VoidCustomData, PubNubManagedChatEntities>. To do this in your code, follow the structure below:

public typealias PubNubChatProvider = ChatProvider<VoidCustomData, PubNubManagedChatEntities>

Local providers

A reference to ChatProvider is passed to the UI components and can be used to provide more rich functionality using the following providers.

DataProvider

Provides methods you can use to store data into the CoreData instance. This includes convenience methods for pagination and auto response storage of PubNub API methods and a subscription listener that stores subscribe events.

PubNubProvider

Provides access to the PubNub instance used by chat components with responses conforming to Model data used by ChatProvider. Use it to make direct calls to PubNub APIs without storing the response. You can still store the response afterward using the corresponding DataProvider methods.

ThemeProvider

Provides a template that can be used to modify the theme used by chat components. The template consists of the following themes:

Last updated on