Add chat to an iOS app (Swift)
The Ethora Swift SDK has two parts: XMPPChatCore (auth, REST, XMPP connection, stores) and XMPPChatUI (a ready-made SwiftUI chat built on the core). This guide installs both, points them at Ethora Cloud, signs a user in and puts the chat on screen.
Using Claude Code or Codex?
Let Claude Code or Codex do it
Our open-source skill for Claude Code and Codex runs the steps on this page for you: it adds the Swift package to your Xcode project from the terminal, signs in to Ethora to create or pick an app, writes EthoraEnv.swift and, with your OK, adds the sign-in token route to your backend. Then it checks the setup.
/plugin marketplace add dappros/chat-ethora-skill /plugin install ethora-skill@ethora
codex plugin marketplace add dappros/chat-ethora-skill codex plugin add ethora-skill@ethora
Then ask your agent to add Ethora chat to this app. Cursor and other agents: npx skills add dappros/chat-ethora-skill.
Requirements
- iOS 15 or later.
- Swift tools 5.9 or later, which means Xcode 15 or later.
- An Ethora app: its App ID, plus either a client JWT issued by your backend or an email/password user and the app token.
Install with Swift Package Manager
In Xcode, choose File → Add Package Dependencies…, enter the URL below and add both XMPPChatCore and XMPPChatUI to your app target.
https://github.com/dappros/ethora-sdk-swiftIn a Package.swift manifest:
// Package.swift (excerpt)
dependencies: [
.package(url: "https://github.com/dappros/ethora-sdk-swift", branch: "main")
],
targets: [
.target(
name: "YourApp",
dependencies: [
.product(name: "XMPPChatCore", package: "ethora-sdk-swift"),
.product(name: "XMPPChatUI", package: "ethora-sdk-swift")
]
)
]The SDK's own docs track branch: "main". The repository also has a v1.0.0 tag, so you can pin a release with from: "1.0.0" instead. The only third-party dependency is Starscream 4.x, which SPM resolves for you.
Configure for Ethora Cloud
All settings live in ChatConfig (module XMPPChatCore). Three values are required: baseUrl, appId and xmppSettings. The SDK has no built-in endpoints. If one of these values is missing, the chat stops and shows a ConfigError message.
import XMPPChatCore
func makeChatConfig(clientJWT: String) -> ChatConfig {
var config = ChatConfig()
config.baseUrl = "https://api.chat.ethora.com/v1"
config.appId = "YOUR_APP_ID"
config.xmppSettings = XMPPSettings(
xmppServerUrl: "wss://xmpp.chat.ethora.com/ws",
host: "xmpp.chat.ethora.com",
conference: "conference.xmpp.chat.ethora.com"
)
config.jwtLogin = JWTLoginConfig(token: clientJWT, enabled: true)
return config
}xmppServerUrl,hostandconferencemust all be non-empty.devServeris an older name forxmppServerUrland still works.- Room JIDs have the form
<room>@conference.xmpp.chat.ethora.com. - If you host Ethora yourself, replace all four URLs and hosts with your own.
Sign a user in
The chat UI needs a user with XMPP credentials in UserStore.shared. The SDK supports three ways to get one there.
Client JWT (recommended)
Your backend issues a JWT for the signed-in user (see Authentication). Put it in config.jwtLogin as shown above. If no user is stored yet, ChatWrapperView calls AuthAPI.loginViaJwt(clientToken:baseURL:) when it starts. That sends POST {baseUrl}/users/client with the token in the x-custom-token header.
If you want to sign in before the chat opens, or handle the error yourself, call it directly:
import XMPPChatCore
@MainActor
func signInToChat(config: ChatConfig, clientJWT: String) async throws {
ConfigStore.shared.mergeConfig(config) // AuthAPI reads baseUrl from here
let response = try await AuthAPI.loginViaJwt(clientToken: clientJWT)
UserStore.shared.setUser(from: response)
}Email and password
let response = try await AuthAPI.loginWithEmail(
email: email,
password: password,
appToken: "YOUR_ETHORA_APP_TOKEN"
)
UserStore.shared.setUser(from: response)The app token is the full app JWT (eyJ…), not the 24-character App ID. If you leave out appToken, the SDK falls back to the ETHORA_APP_TOKEN environment variable and then to a bundled development token. Do not ship that fallback to production.
A user you already signed in
If your app already holds an Ethora User with its token and XMPP credentials, pass it in: config.userLogin = UserLoginConfig(enabled: true, user: user).
Sign out
Call await LogoutManager.shared.logout() from your app's sign-out. It resets push, disconnects XMPP, and clears the stored user and caches. By default it keeps your ChatConfig. The SDK saves the user in UserDefaults and skips JWT login while a user is stored, so skipping this step leaves the previous account signed in.
Show the chat
SwiftUI
import SwiftUI
import XMPPChatCore
import XMPPChatUI
struct ChatScreen: View {
let config: ChatConfig
var body: some View {
ChatWrapperView(
config: config,
onUnreadCountChanged: { totalUnread in
// update your tab or app badge
}
)
}
}By default this shows the user's room list. To open one room with no list, turn on single-room mode:
var config = makeChatConfig(clientJWT: jwt)
config.disableRooms = true
ChatWrapperView(
config: config,
initialRoomJID: "my-room@conference.xmpp.chat.ethora.com"
)initialRoomJID only takes effect when disableRooms is true. It must be a full JID for a room the user belongs to. ChatWrapperView copies the config into ConfigStore.shared itself. If a different user signs in while the view is on screen, give it a new .id(...) so it starts over, as the sample app does.
UIKit
XMPPChatUI contains SwiftUI views only and no UIViewController. In a UIKit app, wrap ChatWrapperView in Apple's UIHostingController.
Push notifications
The SDK sends an FCM token to the Ethora backend and subscribes the user to their rooms. Getting the token (Firebase Messaging and APNs setup) is up to your app. After sign-in, once the chat has connected:
import XMPPChatCore
@MainActor
func registerChatPush(fcmToken: String) {
if let client = ClientRegistry.shared.getGlobalXMPPClient() {
PushNotificationManager.shared.attachClient(client)
}
PushNotificationManager.shared.updateFCMToken(fcmToken)
}Each call waits for the other, so the order does not matter. Push is on by default. To turn it off, set config.push = PushNotificationConfig(enabled: false).
Troubleshooting
- "User is not authenticated for chat." No stored user has an XMPP password. Either JWT login failed or no sign-in was configured. Look for
JWT autologin failed:in the Xcode console. - The loader shows "ChatConfig.baseUrl is not set…", or the same message for
xmppSettingsorappId. Set all three values. The SDK has no defaults. - "Failed to connect. Retrying..." XMPP did not connect within 15 seconds. Check
xmppServerUrl,hostand the user's credentials. - "Selected chat was not found." The single-room JID does not match any of the user's rooms. Use the full
room@conference…JID. initialRoomJIDis ignored. Setconfig.disableRooms = true.- The app crashes when the camera, photo picker or microphone opens. Add
NSCameraUsageDescription,NSMicrophoneUsageDescription,NSPhotoLibraryUsageDescriptionandNSPhotoLibraryAddUsageDescriptionto Info.plist. - Email login is rejected. Pass the app JWT, not the App ID.
Next steps
- Authentication: issue client JWTs from your backend.
- Rooms: create rooms and add members so they show up in the chat.
- Sample app: ethora-sample-ios (SDKPlayground: setup form, chat, headless unread badge, logs).
Stuck or planning something bigger?
Book 15 minutes with the team, or open an issue on GitHub. Self-hosting and enterprise questions are welcome.