Overview
The unread count endpoint returns the total number of unread messages across all conversations for the authenticated user. This is useful for displaying notification badges in your application’s UI.Authentication
This endpoint requires authentication via JWT token in theAuthorization header.
Get Unread Count
GET /api/chat/unread-count
Returns the total number of unread messages for the authenticated user across all their conversations.
Response Fields
Indicates if the request was successful
Total number of unread messages across all conversations. Returns
0 if there are no unread messages.Error message (only present when success is false)
How It Works
The endpoint:- Finds all conversations where the authenticated user is a participant
- Counts all messages in those conversations where:
- The sender is NOT the authenticated user
- The
is_readfield isfalse
- Returns the total count
Error Responses
- 401 Unauthorized: Missing or invalid authentication token
- 500 Internal Server Error: Server error occurred
Usage Examples
Display Notification Badge
React
Real-time Updates with Socket.io
For better user experience, combine the unread count endpoint with Socket.io message notifications:Mobile App Integration
Swift
Best Practices
Polling Strategy
How often should I poll?
How often should I poll?
The recommended polling interval depends on your use case:
- Active chat view: Poll every 10-30 seconds
- Background/other views: Poll every 1-2 minutes
- Mobile apps: Poll when app comes to foreground
Reducing API calls
Reducing API calls
To minimize server load and improve performance:
- Use Socket.io’s
message_notificationevent to increment the count client-side - Only fetch the actual count periodically or when the user opens the app
- Store the count in local state and update it optimistically
- Reset the count when users read messages (mark them as read)
Handling errors
Handling errors
If the unread count request fails:
- Keep showing the last known count
- Don’t show “0” as it may be misleading
- Retry with exponential backoff
- Show a visual indicator if the count is stale
Optimistic Updates
Update the count optimistically when users interact with messages:Related Endpoints
- Get Messages - Fetching messages automatically marks them as read
- List Conversations - See which conversations have unread messages via
last_message.is_read - Socket.io Events - Real-time message notifications
Implementation Checklist
Socket.io Integration
Listen to
message_notification events to increment the count in real-time when new messages arrive.Optimistic Updates
Decrease the count optimistically when users open conversations and read messages.
Periodic Sync
Poll the endpoint periodically (every 1-2 minutes) to sync with the server and correct any client-side drift.

