Install the package first: see Installation.
- 1. Configure PoolChat
- 2. Key Exchange
- 3. Encrypt and Send a Message
- 4. Use the Built-in SwiftUI View
Set up logging and storage before using any PoolChat services:
import PoolChat
// Inject your logger (optional, falls back to os.Logger)
PoolChatConfiguration.logger = MyAppLogger()
// Inject your encrypted storage provider (required for history persistence)
PoolChatConfiguration.storageProvider = MySecureStorage()
// Security settings (defaults are recommended)
PoolChatConfiguration.rejectUnencryptedMessages = true
PoolChatConfiguration.enableHistorySync = trueEvery knob is documented in Configuration.
When a peer connects, exchange public keys to establish encryption:
let encryptionService = ChatEncryptionService.shared
// Get your public key to send to the peer
let myPublicKey = encryptionService.publicKey
// When you receive a peer's public key, perform key exchange
let success = encryptionService.performKeyExchange(
peerPublicKeyData: peerPublicKeyData,
peerID: remotePeerID
)
if success {
print("E2E encryption established with \(remotePeerID)")
}First contact records the peer identity under TOFU. See Security: Trust-On-First-Use.
// Create a message
let message = RichChatMessage.textMessage(
from: localPeerID,
senderName: "Alice",
text: "Hello from PoolChat!",
isFromLocalUser: true
)
// Serialize the payload
let payload = RichChatPayload(from: message)
let payloadData = try JSONEncoder().encode(payload)
// Encrypt for a specific peer
if let encrypted = encryptionService.encrypt(payloadData, for: targetPeerID) {
let envelope = EncryptedChatPayload(
encryptedData: encrypted,
senderPeerID: localPeerID,
isPrivateChat: false,
targetPeerID: nil,
messageType: .chatMessage
)
// Send via ConnectionPool
}For a complete chat UI out of the box:
import PoolChat
import ConnectionPool
struct ChatScreen: View {
@StateObject private var viewModel = PoolChatViewModel()
var body: some View {
PoolChatView(viewModel: viewModel)
}
}The view includes message bubbles, emoji picker, voice recording controls, image sending, poll creation, reactions, reply threading, @mention autocomplete, and voice/video calling flows, all with cross-platform support.