A promise-based WebSocket client, providing async/await interface for WebSocket communications.
- Supports JSON-RPC style requests
- Provides error handling for RPC errors
- No dependencies
- Vanilla JavaScript
deno add @amund/websocket-promise
import { WebSocketPromise } from 'jsr:@amund/websocket-promise'
// Basic usage
const client = new WebSocketPromise('ws://localhost:8080')
const response = await client.method('mirror', { data: 'test' })
console.log(response) // { method: 'mirror', params: { data: 'test' } }
// Error handling
try {
await client.method('unknown-method')
} catch (error) {
console.error('RPC Error:', error.message)
}
// Cleanup
client.close()
url
: WebSocket server URLoptions
: Connection options (timeout, headers, etc.)
- Send JSON-RPC style requests
- Returns a promise that resolves with the response
- Close the WebSocket connection cleanly
Tests use MSW (Mock Service Worker) for WebSocket mocking:
deno task test
# or watch mode
deno task test-watch
MIT (check repository for details)