Client SDK

Client WebSocket

Real-time WebSocket client

Client WebSocket#

Connect via WebSocket for real-time command execution and event subscriptions:

typescript
// Connect to WebSocket
const ws = await client.connect()
ย 
// Execute commands over WebSocket
const result = await ws.execute('search', { query: 'laptop' })
ย 
// Listen for events
ws.on('order.updated', (data) => {
console.log('Order updated:', data)
})
ย 
// Disconnect when done
client.disconnect()

Retry & Cache

The client supports automatic retry with exponential backoff and an LRU response cache.

typescript
const client = await SurfClient.discover(url, {
// Retry configuration
retry: {
maxAttempts: 3,
backoffMs: 500,
backoffMultiplier: 2,
retryOn: [429, 502, 503, 504],
},
ย 
// Response caching (skips commands with sideEffects: true)
cache: {
ttlMs: 60_000, // 60s TTL
maxSize: 100, // Max 100 cached entries
},
})
ย 
// Execute โ€” automatically retries and caches
const result = await client.execute('search', { query: 'laptop' })
ย 
// Clear cache for a specific command
client.clearCache('search')
ย 
// Clear all caches
client.clearCache()
ย 
// Check for manifest updates (checksum comparison)
const update = await client.checkForUpdates()
if (update.changed) {
console.log('Manifest updated:', update.manifest)
}