Team Chat offers an example for how to implement PubNub Chat on Apple platforms. It works on iOS, iPadOS, and Mac OS, and includes features like user management, multiple group conversations, online/offline presence status, and more. Its UIs are built using SwiftUI, and it uses PubNub’s Client State Management framework to manage local state on the application.
The sample app, combined with our documentation, should serve as a resource for any developer looking to build in-app chat using our Swift SDK. It showcases how to go beyond the simple sending and receiving of messages and covers UI state management practices, which let you take full advantage of the features available in our chat API. In turn, this sample app can form the foundation for using PubNub chat in rich, production-ready applications.
The full GitHub repo for this project is available here.
The application uses SwiftUI: an innovative, simple way to build user interfaces across all Apple platforms with the power of Swift. SwiftUI uses a declarative syntax so you can simply state what your user interface should do. So, your code is simpler and easier to read than ever before. SwiftUI builds user interfaces for any Apple device using just one set of tools and APIs. With it, you can display your UI on any device and at any orientation. This lets you easily develop a flexible application that works well (and looks good) across iPhones, iPads, and Apple computers of all sizes. It also has prebuilt support for large fonts, localizations, left-handed mode, and dark mode, saving you time and maintenance.
About PubNub Client State Manager
PubNub Client State Manager is a framework that offers data management for apps built on the Apple platform. This framework listens to realtime events generated by the PubNub network to update the view inside your applications. And of course, your data can coexist with PubNub-defined data. It’s built upon ReSwift which is a Redux-like implementation of the unidirectional data flow architecture in Swift, and is quickly gaining popularity in the Swift community.
Why Client State Management
Have you ever worked on an unpredictable application? Perhaps the application works on the well-traveled "happy path." But, when users access less common features or combinations of features, bugs appear. And then when those bugs are fixed, more bugs are found or created. Or worse, you may have an unpredictable application in which bugs occur, but that can’t be replicated reliably, and don’t give evidence of their cause.
Modern user interfaces are complex. They feature complicated interactions, they're highly asynchronous, and they may be worked on by many different people. ReSwift attempts to solve these problems by placing strong constraints on the way applications can be written. This reduces the room for programmer error and leads to applications that can be more easily understood.
PubNub Client State Manager uses ReSwift to store data received via PubNub Messaging, connecting it to SwiftUI and helping to solve these problems of predictability and stability.
Run the app
iOS 13.0+ / Mac Catalyst 13.0+ / tvOS 13.0+ / watchOS 6.0+
To run this application you must obtain publish and subscribe keys from your PubNub Account. If you don't already have an account, you can create one for free.
Sign in to your PubNub Dashboard.
Click Create New App.
Give your app a name, and select Chat App as the app type.
Select a region to store your user data (e.g. Portland). Click Create.
Click your new app to open its settings, then click its keyset.
Locate the Publish and Subscribe keys. You'll need these keys to include in this project.
Run the app
To run the application in your local environment, do the following:
1. Clone the GitHub repository.
git clone firstname.lastname@example.org:pubnub/swift-ref-app-team-chat.git
2. Set the following environment variables inside your terminal's configuration file (such as ~/.bash_profile).
export TEAM_CHAT_PUB_KEY="<Enter Your PubNub Publish Key Here>" export TEAM_CHAT_SUB_KEY="<Enter Your PubNub Subscribe Key Here>"
3. Setup the project.
cd swift-ref-app-team-chat bundle install && brew bundle && npm install bundle exec fastlane setup
4. When fastlane setup has completed, run the following command in your Terminal window:
5. Then, use the Run command on the TeamChat scheme to build and execute the application on your physical device or simulator.
Once you’ve gotten a chance to poke around our sample app, you may be interested in building something similar for your own use. Visit our docs for PubNub Chat to learn more about how to use SwiftUI and the PubNub Client State Management framework together, to add in-app chat to your applications.
Additionally, PubNub Chat makes it super easy for developers to add rich chat features like unread message counts, typing indicators, read receipts, reactions and more. Check out our Chat Feature Guide, below, for more details.