Saltar al contenido principal
Versión: 10.x

Enlace WebSocket

Traducción Beta No Oficial

Esta página fue traducida por PageTurner AI (beta). No está respaldada oficialmente por el proyecto. ¿Encontraste un error? Reportar problema →

wsLink es un enlace terminal que se utiliza con el Cliente de WebSockets y Suscripciones de tRPC. Puedes obtener más información sobre esto aquí.

Uso

Para usar wsLink, debes pasarle un TRPCWebSocketClient, que puedes crear con createWSClient:

client/index.ts
ts
import { createTRPCProxyClient, createWSClient, wsLink } from '@trpc/client';
import type { AppRouter } from '../server';
const wsClient = createWSClient({
url: 'ws://localhost:3000',
});
const trpcClient = createTRPCProxyClient<AppRouter>({
links: [wsLink<AppRouter>({ client: wsClient })],
});
client/index.ts
ts
import { createTRPCProxyClient, createWSClient, wsLink } from '@trpc/client';
import type { AppRouter } from '../server';
const wsClient = createWSClient({
url: 'ws://localhost:3000',
});
const trpcClient = createTRPCProxyClient<AppRouter>({
links: [wsLink<AppRouter>({ client: wsClient })],
});

La función wsLink requiere que se le pase un TRPCWebSocketClient, el cual puede configurarse con los campos definidos en WebSocketClientOptions:

ts
export interface WebSocketLinkOptions {
client: TRPCWebSocketClient;
}
function createWSClient(opts: WebSocketClientOptions) => TRPCWebSocketClient
export interface WebSocketClientOptions {
url: string;
WebSocket?: typeof WebSocket;
retryDelayMs?: typeof retryDelay;
onOpen?: () => void;
onClose?: (cause?: { code?: number }) => void;
}
ts
export interface WebSocketLinkOptions {
client: TRPCWebSocketClient;
}
function createWSClient(opts: WebSocketClientOptions) => TRPCWebSocketClient
export interface WebSocketClientOptions {
url: string;
WebSocket?: typeof WebSocket;
retryDelayMs?: typeof retryDelay;
onOpen?: () => void;
onClose?: (cause?: { code?: number }) => void;
}

Referencia

Puedes consultar el código fuente de este enlace en GitHub.