Error Handling
Handle connection issues and edge cases gracefully.
Connection Errors
room.on('error', (error) => {
console.error('Connection error:', error.message)
// Show user feedback
showNotification('Connection issue')
})Disconnect Handling
room.on('disconnected', () => {
console.log('Disconnected, reconnecting...')
showReconnectingUI()
})
room.on('connected', () => {
console.log('Reconnected!')
hideReconnectingUI()
})The SDK auto-reconnects with exponential backoff (1s, 2s, 4s, ... up to 30s). You don't need to manually reconnect.
Connect Errors
try {
const room = await connect('my-room')
} catch (error) {
console.error('Failed to connect:', error)
showNotification('Could not connect. Please try again.')
}Common Issues
Connection Timeout
If connection takes too long (10s), it times out. Usually indicates network issues.
WebSocket Not Supported
Very old browsers may not support WebSocket. All modern browsers do.
Debugging
// Log all messages
room.on('message', (from, data, meta) => {
console.log('Message:', { from, data, meta })
})
// Log connection state
room.on('connected', () => console.log('Connected'))
room.on('disconnected', () => console.log('Disconnected'))
room.on('error', (e) => console.error('Error:', e))User Feedback
Keep users informed about connection state:
// Simple connection indicator
function updateConnectionUI() {
const indicator = document.getElementById('connection-status')
if (room.connected) {
indicator.textContent = '🟢 Connected'
indicator.style.color = 'green'
} else {
indicator.textContent = '🔴 Reconnecting...'
indicator.style.color = 'red'
}
}
room.on('connected', updateConnectionUI)
room.on('disconnected', updateConnectionUI)