The PubNub CSM framework offers a data management framework that listens to realtime events generated by the PubNub network to update the view inside your application. And of course, your data can coexist with PubNub-defined data.
Note: This is a beta version of the PubNub CSM framework
The PubNub CSM framework includes the following components that you help manage your application’s state and PubNub server communications.
- Actions: Preconfigured actions that correspond to PubNub internals
- Reducers: Configured to respond to the actions dispatched by the PubNub CSM framework
- Listeners: Monitor subscription notifications and dispatch actions
- Commands: Functions that execute PubNub API calls and dispatch actions
- Sending and Receiving Messages
- Indicating User Presence
- Monitoring Network Status
- Managing Users
- Managing Spaces
- Managing Space Memberships
- PubNub Access Manager
- Storage and Playback
- Signals
- Message Actions
- Xcode
- PubNub Account (Free)
- iOS 8.0+ / MacOS 10.10 / Mac Catalyst 13.0+ / tvOS 9.0+ / watchOS 2.0+
- Swift 5+
This section provides the basic integration steps to connect the PubNub CSM framework into your existing application. For an example of an application built on top of this framework, go to our reference application site at: https://www.pubnub.com/docs/chat/quickstart
Before you can create an application with the PubNub CSM framework, obtain a set of chat-optimized keys from your PubNub Dashboard.
In the following instructions, replace the following references with key values from your PubNub Dashboard.
myPublishKey
mySubscribeKey
A Redux application manages all application state in a centralized location called the store. To gain the benefits offered by the PubNub CSM framework, you must configure your store to include references to the PubNub libraries.
struct AppState: StateType, Equatable {
var networkStatus = NetworkStatusState()
var user = UserState<UserObject>()
var membership = MembershipState<UserObjectMembership>()
var space = SpaceState<SpaceObject>()
var member = MemberState<SpaceObjectMember>()
var presence = PresenceState<[String: AnyJSON]>()
var messages = MessageState<AnyJSON>()
}
func appReducer(action: Action, state: AppState?) -> AppState {
var state = state ?? AppState()
switch action {
case let action as PubNubActionType:
NetworkStatusReducer.reducer(action, state: &state.networkStatus)
UserReducer.reducer(action, state: &state.user)
SpaceReducer.reducer(action, state: &state.conversation)
MembershipReducer.reducer(action, state: &state.membership)
MemberReducer.reducer(action, state: &state.member)
MessageReducer.reducer(action, state: &state.messages)
PresenceReducer.reducer(action, state: &state.presence)
default: break
}
return state
}
The PubNub CSM framework uses Redux Thunk to manage the interaction with commands that execute PubNub API calls, process the responses, and dispatch ReSwift actions.
// Create the PubNub Instance
let pubnub = PubNub(configuration: .init(publishKey: "myPublishKey", subscribeKey: "mySubscribeKey"))
// Configure the service provider used by the Thunk
PubNubServiceProvider.shared.set(service: pubnub)
let middleware = ThunkAction.getMiddleware
let myStore = Store(
reducer: appReducer,
state: AppState(),
middleware: [ThunkAction.getMiddleware]
)
You can register all the listeners that are included in the PubNub CSM framework.
PubNubServiceProvider.shared.add(listener: PubNubListener.createListener(dispatch: dispatch))
This section contains a few examples of commands to get your started. This is not meant to be an exhaustive list. Also, you can create commands in your application that use our actions to meet your requirements.
store.dispatch(
MessageCommand.sendMessage(
SendMessageRequest(
content: [
"title": "hello world",
"body": "This is our hello world message."
],
channel: "my-channel")
))
store.dispatch(
UserCommand.createPubNub(
UserRequest(user: UserObject(name: "User Name", id: "my-user-id"))
))
store.dispatch(
UserCommand.fetchPubNubUser(
UserIdRequest(userId: "my-user-id")
))
store.dispatch(
SpaceCommand.createPubNub(
SpaceRequest(space: SpaceObject(name: "Space Name", id: "my-space-id"))
))
store.dispatch(
MembershipCommand.join(
MembershipModifyRequest(
userId: "my-user-id",
modifiedBy: [
UserObjectMembership(id: "my-space-id")
])
))