WebSocket Implementation
Feature: $ARGUMENTS
- Detect WebSocket library:
cat package.json | grep -E '"socket.io"|"ws"|"@fastify/websocket"|"hono.*ws"|"partykit"'
- Generate server-side handler:
// Socket.io example
io.on('connection', (socket) => {
// Auth check
const userId = socket.handshake.auth.token
if (!isValidToken(userId)) {
socket.disconnect()
return
}
// Room management
socket.join(`user:${userId}`)
socket.on('message', async (data) => {
// Validate input
// Handle event
// Broadcast to relevant room
io.to(`room:${data.roomId}`).emit('message', response)
})
socket.on('disconnect', () => {
// Cleanup
})
})
- Generate client-side hook with:
- Automatic reconnection with exponential backoff
- Connection state management
- Cleanup on unmount
- TypeScript event typing
- Add error handling for:
- Auth failures
- Network drops
- Server restarts
- Include a message type registry to keep events typed end-to-end.