📡 Connection
WebSocket URL:
Namespace:
Transports: WebSocket, Polling
ws://your-domain.com/chatNamespace:
/chatTransports: WebSocket, Polling
Authentication
You must provide a JWT token for authentication. You can send it in one of two ways:
// Option 1: Authorization header
const socket = io('ws://your-domain.com/chat', {
extraHeaders: {
Authorization: 'Bearer YOUR_JWT_TOKEN'
}
});
// Option 2: Auth token in handshake
const socket = io('ws://your-domain.com/chat', {
auth: {
token: 'YOUR_JWT_TOKEN'
}
});
⚠️ Important: Connections without valid authentication will be immediately rejected.
🔌 Connection Events
connect
Emitted when successfully connected to the server
socket.on('connect', () => {
console.log('Connected to Dozaar WebSocket');
});
user:online
Broadcast when a user comes online
Payload:
| Field | Type | Description |
|---|---|---|
| userId | string | UUID of the user who came online |
| timestamp | number | Unix timestamp in milliseconds |
user:offline
Broadcast when a user goes offline
Payload: Same as user:online
📤 Client Events (Emit)
join:conversation
Join a conversation room to receive messages
Payload:
| Field | Type | Required | Description |
|---|---|---|---|
| conversationId | string (UUID) | Required | ID of the conversation to join |
socket.emit('join:conversation', {
conversationId: '123e4567-e89b-12d3-a456-426614174000'
});
// Listen for acknowledgment
socket.on('joined:conversation', (data) => {
console.log('Joined conversation:', data.conversationId);
});
leave:conversation
Leave a conversation room
Payload:
| Field | Type | Required |
|---|---|---|
| conversationId | string (UUID) | Required |
socket.emit('leave:conversation', {
conversationId: '123e4567-e89b-12d3-a456-426614174000'
});
send:message
Send a message to a conversation
Payload:
| Field | Type | Required | Description |
|---|---|---|---|
| conversationId | string (UUID) | Required | ID of the conversation |
| message | string | Optional | Text content (max 10,000 characters) |
| attachmentUrls | string[] | Optional | Array of attachment URLs |
Note: You must provide either
message or attachmentUrls (or both).
socket.emit('send:message', {
conversationId: '123e4567-e89b-12d3-a456-426614174000',
message: 'Hello, world!',
attachmentUrls: ['https://example.com/image.jpg']
});
typing:start
Indicate that you're typing in a conversation
Payload:
| Field | Type | Required |
|---|---|---|
| conversationId | string (UUID) | Required |
socket.emit('typing:start', {
conversationId: '123e4567-e89b-12d3-a456-426614174000'
});
Typing indicators automatically expire after 10 seconds.
typing:stop
Indicate that you've stopped typing
Payload: Same as typing:start
socket.emit('typing:stop', {
conversationId: '123e4567-e89b-12d3-a456-426614174000'
});
mark:read
Mark a message as read
Payload:
| Field | Type | Required |
|---|---|---|
| messageId | string (UUID) | Required |
socket.emit('mark:read', {
messageId: '123e4567-e89b-12d3-a456-426614174000'
});
get:online_status
Check online status of multiple users
Payload:
| Field | Type | Required |
|---|---|---|
| userIds | string[] | Required |
socket.emit('get:online_status', {
userIds: ['user-id-1', 'user-id-2']
});
socket.on('online:status', (data) => {
console.log('Online statuses:', data.statuses);
});
📥 Server Events (Listen)
new:message
Received when a new message is sent in a conversation you've joined
socket.on('new:message', (message) => {
console.log('New message:', message);
// message contains: id, conversationId, senderId, message,
// attachmentUrls, createdAt, sender object, etc.
});
message:notification
Personal notification when someone sends you a message
Payload:
| Field | Type | Description |
|---|---|---|
| conversationId | string | ID of the conversation |
| messageId | string | ID of the new message |
| preview | string | First 100 characters of the message |
| sender | object | { id, username } |
| timestamp | number | Unix timestamp |
user:typing
Someone started typing in the conversation
socket.on('user:typing', (data) => {
console.log(`User ${data.userId} is typing`);
});
user:stopped_typing
Someone stopped typing in the conversation
message:read
Your message was read by someone
Payload:
| Field | Type |
|---|---|
| messageId | string |
| readBy | string |
| timestamp | number |
exception
Error occurred while processing your request
socket.on('exception', (error) => {
console.error('Error:', error.event, error.error);
});
💡 Complete Client Example
import { io } from 'socket.io-client';
// Connect with authentication
const socket = io('ws://your-domain.com/chat', {
auth: {
token: 'YOUR_JWT_TOKEN'
}
});
// Connection events
socket.on('connect', () => {
console.log('✅ Connected to Dozaar');
// Join a conversation
socket.emit('join:conversation', {
conversationId: 'your-conversation-id'
});
});
socket.on('disconnect', () => {
console.log('❌ Disconnected');
});
// Listen for messages
socket.on('new:message', (message) => {
console.log('📨 New message:', message);
});
socket.on('message:notification', (notification) => {
console.log('🔔 Notification:', notification);
});
// Typing indicators
socket.on('user:typing', (data) => {
console.log('⌨️ User typing:', data.userId);
});
// Presence
socket.on('user:online', (data) => {
console.log('🟢 User online:', data.userId);
});
socket.on('user:offline', (data) => {
console.log('⚫ User offline:', data.userId);
});
// Error handling
socket.on('exception', (error) => {
console.error('❌ Error:', error);
});
// Send a message
function sendMessage(text) {
socket.emit('send:message', {
conversationId: 'your-conversation-id',
message: text
});
}
// Start typing
function startTyping() {
socket.emit('typing:start', {
conversationId: 'your-conversation-id'
});
}
// Stop typing
function stopTyping() {
socket.emit('typing:stop', {
conversationId: 'your-conversation-id'
});
}
✨ Best Practices
✅ Do's
- Always join a conversation before sending messages to it
- Handle reconnection logic in your client
- Clear typing indicators when sending a message
- Implement exponential backoff for reconnection attempts
- Listen for
exceptionevents for error handling - Leave conversations when navigating away to free up resources
⚠️ Don'ts
- Don't send messages without joining the conversation first
- Don't spam typing indicators - throttle them on the client side
- Don't forget to handle connection errors and reconnection
- Don't store sensitive data in message payloads
- Don't exceed the 10,000 character limit for messages
⚡ Technical Details
| Setting | Value | Description |
|---|---|---|
| Ping Timeout | 60 seconds | Time before considering connection dead |
| Ping Interval | 25 seconds | Interval between ping packets |
| Presence TTL | 5 minutes | Time before user is marked offline |
| Typing Indicator TTL | 10 seconds | Auto-expire time for typing status |
| Max Message Length | 10,000 chars | Maximum message text length |