Cursor prompt

Build Real-Time Feature with WebSockets

Implement a real-time feature using WebSockets or SSE with presence tracking, reconnection logic, and proper resource cleanup.

Build a real-time {{feature_type}} feature for {{project_name}}.

## Transport: {{transport}}
## Requirements: {{requirements}}

### Server Implementation
- WebSocket server using {{server_library}}
- Connection registry: Map<userId, Set<WebSocket>> to track per-user connections
- Heartbeat ping/pong every 30 seconds to detect dead connections
- On disconnect: remove from registry, cancel any active subscriptions
- Authentication: verify JWT token on connection handshake before accepting
- Per-connection rate limit: max 20 messages/second

### Client Implementation
- Auto-reconnect with exponential backoff: 1s → 2s → 4s → 8s → max 30s
- Connection state machine: idle | connecting | connected | reconnecting | disconnected
- Queue outbound messages while disconnected; drain queue after reconnection
- Visible connection status indicator (dot: green/yellow/red)
- Cleanup: close connection and remove all listeners on component unmount

### Message Protocol
```typescript
type Message = {
  type: string;
  payload: unknown;
  id: string;       // for deduplication
  timestamp: number;
}
```
- Client acknowledges critical messages
- Server deduplicates using id field

### Optimistic UI
- Apply state changes immediately without waiting for server confirmation
- Revert to previous state if server returns an error

### Output
Server handler, useRealtime() hook, connection status component, and message queue utility.

Fill in: {{feature_type}}, {{project_name}}, {{transport}}, {{requirements}}, {{server_library}}. In Loopy you fill these in a form and copy the finished prompt in one click.