Replace the current mixed approach (REST + Supabase real-time) with a unified WebSocket-based chat API that handles all chat operations through WebSocket connections.
- Chat Store: Uses REST API calls + Supabase real-time subscriptions
- API Endpoints: REST endpoints for conversations, groups, messages
- Real-time: Supabase real-time for live updates
- Dependencies:
wspackage already installed
- Create WebSocket server that integrates with SvelteKit
- Handle authentication via JWT tokens
- Manage client connections and rooms
- Implement message broadcasting
Define standardized message format:
{
type: 'action_type',
payload: { /* action-specific data */ },
requestId: 'unique_id', // for request/response correlation
timestamp: 'ISO_string'
}auth- Authenticate user connectionjoin_conversation- Join a conversation roomleave_conversation- Leave a conversation roomsend_message- Send a message to conversationload_conversations- Get user's conversationsload_messages- Get messages for conversationtyping_start- Start typing indicatortyping_stop- Stop typing indicatorcreate_conversation- Create new conversationjoin_group- Join group by invite codecreate_group- Create new group
- Message broadcasting to conversation participants
- Typing indicators
- User presence (online/offline)
- Message read receipts
- Connection status updates
- Create WebSocket server handler
- Implement authentication middleware
- Set up connection management
- Define message protocol
- Implement conversation management
- Add message sending/receiving
- Handle typing indicators
- Add user presence tracking
- Update chat store to use WebSocket
- Remove REST API dependencies
- Update UI components
- Add error handling and reconnection
- Create WebSocket API tests
- Test real-time functionality
- Performance testing
- Error scenario testing
src/
├── lib/
│ ├── websocket/
│ │ ├── server.js # WebSocket server
│ │ ├── handlers/ # Message handlers
│ │ │ ├── auth.js
│ │ │ ├── conversations.js
│ │ │ ├── messages.js
│ │ │ └── typing.js
│ │ ├── middleware/ # WebSocket middleware
│ │ │ └── auth.js
│ │ └── utils/
│ │ ├── protocol.js # Message protocol
│ │ └── rooms.js # Room management
│ └── stores/
│ └── websocket-chat.js # Updated chat store
└── routes/
└── api/
└── websocket/
└── +server.js # WebSocket endpoint
- Unified Communication: Single WebSocket connection for all chat operations
- Better Performance: Reduced HTTP overhead, persistent connections
- Real-time: Native real-time capabilities without external dependencies
- Scalability: Better connection management and resource usage
- Simplicity: Single protocol for all chat operations