Overview
Threadly uses Socket.IO for bidirectional real-time communication between the Android client and Node.js server. This enables instant messaging, live notifications, and real-time status updates without polling.Architecture
Socket.IO Integration
SocketManager Implementation
TheSocketManager class implements the Singleton pattern to maintain a single WebSocket connection throughout the app lifecycle:
SocketIo/SocketManager.java:1
Key Features
| Feature | Implementation |
|---|---|
| Singleton Pattern | Single connection instance across the app |
| Lazy Initialization | Socket created only when first accessed |
| Thread-Safe | Synchronized getInstance() method |
| Clean Disconnect | Properly closes connection and nullifies instance |
Connection Management
Establishing Connection
Connection Lifecycle
Connection URL
constants/ApiEndPoints.java:93
Event Emission
TheSocketEmitterEvents class provides helper methods for emitting events to the server:
Update Message Seen Status
SocketIo/SocketEmitterEvents.java:9
Event Payload Structure
Event Listening
Setting Up Listeners
Common Socket Events
| Event Name | Direction | Purpose |
|---|---|---|
new_message | Server → Client | Receive new incoming message |
message_delivered | Server → Client | Message delivery confirmation |
message_seen | Server → Client | Message read receipt |
update_seen_msg_status | Client → Server | Mark messages as seen |
user_typing | Server → Client | Show typing indicator |
user_online | Server → Client | User online status |
user_offline | Server → Client | User offline status |
Message Delivery Flow
Sending Messages
network_managers/MessageManager.java:36
Receiving Messages
Message Status Codes
| Status Code | Meaning | Display |
|---|---|---|
0 | Pending | Clock icon |
1 | Sent | Single checkmark |
-1 | Delivered | Double checkmark |
-2 | Seen | Blue double checkmark |
FCM Fallback Mechanism
When the Socket.IO connection is unavailable, Threadly falls back to Firebase Cloud Messaging:Architecture
FCM Token Management
constants/ApiEndPoints.java:96
Push Notification Flow
- Sender sends message via HTTP API
- Server checks recipient status:
- If online (Socket.IO connected) → Instant delivery via WebSocket
- If offline → Store message + Send FCM push notification
- Recipient receives notification and opens app
- App connects to Socket.IO and fetches pending messages
- Messages synced from server to local database
Pending Messages Sync
Checking for Pending Messages
network_managers/MessageManager.java:123
Retrieving Pending Messages
network_managers/MessageManager.java:59
Best Practices
1. Connection Management
2. Event Listener Cleanup
3. Thread Safety
4. Error Handling
Dependency
Related Topics
- Networking - HTTP API communication
- Offline Support - Message caching and sync
- Tech Stack - Complete technology overview