Skip to main content

Overview

The Horse Trust platform uses Socket.io for real-time bidirectional communication, primarily for the chat system and live notifications.

Socket.io Setup

Configured in index.ts with CORS and authentication:
Reference: index.ts:11-20

Authentication Middleware

Socket connections require JWT authentication:
Reference: index.ts:22-35 Client Connection Example:

Connection Handler

When a user connects, they automatically join their personal room:
Reference: index.ts:38-123

Event Handlers

join_conversation

Join a conversation room to receive messages:
Reference: index.ts:46-49 Client Example:

send_message

Send a message in a conversation:
Reference: index.ts:52-109 Client Example with Acknowledgment:

typing

Indicates the user is typing:
Reference: index.ts:112-114 Client Example:

stop_typing

Indicates the user stopped typing:
Reference: index.ts:116-118 Client Example:

Room Structure

The socket implementation uses two types of rooms:

User Rooms

Format: user:{userId}
  • Users automatically join their personal room on connection
  • Used for personal notifications and badges
  • Example: user:507f1f77bcf86cd799439011

Conversation Rooms

Format: conv:{conversationId}
  • Users join when opening a conversation
  • Used for broadcasting messages to all participants
  • Example: conv:507f191e810c19729de860ea

Client Events to Listen For

new_message

Received when a new message is sent in a joined conversation:
Reference: index.ts:89

message_notification

Received in the user’s personal room when a message is sent to them:
Reference: index.ts:96-100

user_typing

Received when another user in the conversation starts typing:
Reference: index.ts:113

user_stop_typing

Received when another user stops typing:
Reference: index.ts:117

Complete Client Integration Example

Error Handling

Socket authentication errors: