Registry / messaging / gin-websocket-client

gin-websocket-client

JSON →
library1.2.1jsnpmunverified

TypeScript/JavaScript client for gin-websocket that wraps WebSocket communication with a fetch-compatible API and event subscriptions using pica-route pattern matching. Current version 1.2.1. Supports auto-reconnect, TypeScript types, and works in both browser and Node.js environments. Differentiated by its fetch-like interface for request-response patterns and flexible pattern-based event subscription system, making it a convenient drop-in replacement for HTTP fetch over WebSocket.

npm install gin-websocket-client
INSTALL
IMPORT
SIG · GIN-WEBSOCKET-CLIE
G
gin-websocket-client
messagingjavascriptv1.2.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

WsClient
import { WsClient } from 'gin-websocket-client'
const WsClient = require('gin-websocket-client').WsClient
ESM-only; CommonJS require may not work without ESM interop. TypeScript types are included.
PushEvent
import type { PushEvent } from 'gin-websocket-client'
import { PushEvent } from 'gin-websocket-client'
PushEvent is an interface; use type import for TypeScript to avoid runtime import.
WsClientOptions
import type { WsClientOptions } from 'gin-websocket-client'
import { WsClientOptions } from 'gin-websocket-client'
WsClientOptions is an interface; use type import for TypeScript to avoid runtime import.

Creates a WebSocket client with auto-reconnect, demonstrates fetch-like HTTP requests, and pattern-based event subscriptions with parameters and wildcards.

import { WsClient } from 'gin-websocket-client'; const client = new WsClient('ws://localhost:8080/ws', { reconnect: true, reconnectInterval: 2000, maxReconnectAttempts: 10 }); async function example() { // Fetch over WebSocket const response = await client.fetch('/api/users', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'Alice' }) }); const data = await response.json(); console.log('Response:', data); // Subscribe to events client.subscribe('/events/user/created', (event) => { console.log('New user:', new TextDecoder().decode(event.data)); }); client.subscribe('/notifications/:type', (event, params) => { console.log(`Notification type: ${params.type}`); }); client.subscribe('/events/*', (event) => { console.log('Event:', event.route); }); } example();
Debug
Known issues
gotchaThe client uses fetch interface but is WebSocket-based; do not confuse with HTTP fetch. It requires a gin-websocket server on the backend.
fix
Ensure your server supports gin-websocket protocol.
affects: >=1.0.0
gotchaEvent data is provided as Uint8Array; decode manually with TextDecoder, not as string.
fix
Use new TextDecoder().decode(event.data) to get string.
affects: >=1.0.0
gotchaAuto-reconnect is enabled by default; set reconnect: false in options to disable.
fix
Pass { reconnect: false } in constructor options if auto-reconnect is not desired.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: client.fetch is not a function
The client may not be connected yet when fetch is called.
fix
Await client.connect() or ensure WebSocket is open before calling fetch.
Error: WebSocket is not open
Fetch or subscribe called on a disconnected client.
fix
Check client.readyState or use auto-reconnect; wait for 'open' event.
Unexpected token < in JSON at position 0
Server returned HTML error page instead of JSON, possibly due to wrong URL.
fix
Verify the WebSocket URL and that the server is running the gin-websocket protocol.
Upgrade
Version history
1.2.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
16 hits · last 30 days
node
14
Amazon
1
OpenAI (training)
1
Resources
gin-websocket-client — npm install gin-websocket-client · libregistry