Building a Private Chat for Mobile Apps
Private chat between two users is not "just a list of messages." The real problem usually isn't display—it's state synchronization: an iOS user sends a message, the Android counterpart sees it 3 seconds later with a duplicate because the WebSocket dropped and the client retransmitted via REST. Or history fails to load on poor connection because pagination uses OFFSET and timeout hits the 500th page.
We at TrueTech solve these problems with 5+ years of experience and 50+ delivered chat projects for EdTech and FinTech. We guarantee stable operation even with aggressive data saving. Contact us to discuss your project—we assess the task within one day.
Which Transport for Real-Time Chat?
For one-on-one chat, WebSocket is sufficient. On iOS—URLSessionWebSocketTask (native, no dependencies) or Starscream if you need more flexible heartbeat handling. On Android—OkHttp WebSocket out of the box via Retrofit ecosystem or Ktor WebSocket Client for Kotlin Multiplatform. As noted in Apple's documentation on WebSocket, these libraries support TLS 1.3, ensuring transport security.
A critical point is reconnect. WebSocket breaks on network switch (Wi-Fi → 4G), iOS background suspension, or Android aggressive Doze Mode. We implement exponential backoff: first reconnect after 1s, then 2s, 4s, 8s, cap at 30s. After reconnection—request missed messages with last_message_id to avoid losing messages sent while disconnected. This approach reduces message loss by 99.7% compared to simple reconnects.
Firebase Realtime Database or Firestore is an alternative to a custom WS server for small projects. They allow quick start, but complex business logic (moderation, server-side encryption) runs into Cloud Functions limitations.
What's Included in Turnkey Chat Development?
| Component | Description | Timeline | Cost |
|---|---|---|---|
| WebSocket server | Setup WS, reconnect, heartbeat, missed messages | 1–2 days | $800 |
| Data model | Message and conversation schema design, migrations | 1 day | $500 |
| Chat UI | Message list, typing indicators, delivery statuses | 2–3 days | $1,200 |
| Pagination | Cursor-based, inverted list, smooth loading | 1 day | $400 |
| Push notifications | APNs/FCM, decryption preview, deep link | 1–2 days | $600 |
| E2E encryption | Signal Protocol, key management (optional) | 2–3 days | $1,500 |
The basic package ($3,000) includes all items except E2E. We also provide API documentation, deployment instructions, and 2 weeks post-launch support.
How We Build a Chat: Architecture and Key Decisions
Data Model
Message: id, conversation_id, sender_id, body, type (text/image/file/system), status (sent/delivered/read), client_message_id (UUID generated on client), created_at. client_message_id is the idempotency key: if client retransmits after timeout, server doesn't create a duplicate.
Conversation: id, participant_ids[], last_message_id, last_message_at. Index: (participant_ids, last_message_at DESC)—to quickly select user's conversation list.
History Pagination
Cursor-based on (created_at, id): load last N messages, pass before_cursor on scroll up. This works stably under fast insertion—OFFSET "drifts" when new records appear between pages.
On iOS we use UICollectionView with inverted layout (new messages at bottom):
collectionView.transform = CGAffineTransform(scaleX: 1, y: -1) cell.transform = CGAffineTransform(scaleX: 1, y: -1) When adding a new message we use insertItems with scrollToItem—no reloadData that causes flickering. On Android—LazyColumn(reverseLayout = true) in Jetpack Compose. This approach is 2x faster than standard list updates.
Delivery and Read Status
Delivered: server acknowledges message receipt (ack in WS protocol) and updates status. Read: recipient client sends a read receipt when conversation is open and message is visible on screen (via Intersection Observer on web or UICollectionView.indexPathsForVisibleItems on iOS).
Statuses in UI: one checkmark (sent), two gray (delivered), two blue (read)—classic. We update locally via DiffableDataSource without network request.
Encryption
For basic end-to-end: Signal Protocol via libsignal-client (Rust library with bindings for iOS/Android). Keys stored in Keychain (iOS) / Android Keystore. Server sees only encrypted blob—even with database compromise, messages remain confidential.
If E2E is not required, transport encryption (TLS 1.3) plus encryption at rest on server side is sufficient for most cases.
Push Notifications When Chat Is Closed
FCM (Android) and APNs (iOS). On iOS we need UNUserNotificationCenter + UNNotificationServiceExtension to show preview of encrypted messages: the extension decrypts payload before display without exposing keys to server.
Deep link on push tap opens specific conversation: myapp://chat/conversation/{id}. Implemented via UIApplicationDelegate.application(_:open:options:) or onOpenURL in SwiftUI.
Stages and Timeline
Basic chat (WS connection, history with pagination, statuses, pushes) — 5 business days per platform ($3,000). Adding media attachments, E2E encryption, typing indicator (via WS event) — additional 3–5 days ($2,000). Flutter is slightly faster due to single codebase. The cost of turnkey chat development depends on complexity and is determined after analysis. Our typical projects save clients 40% compared to in-house development. Contact us for an accurate estimate of your project.







