Re-factor WebSocket generation logic