メインコンテンツへスキップ
バージョン: 11.x

WebSocket リンク

非公式ベータ版翻訳

このページは PageTurner AI で翻訳されました(ベータ版)。プロジェクト公式の承認はありません。 エラーを見つけましたか? 問題を報告 →

wsLink終端リンクで、tRPC の WebSocket クライアントとサブスクリプションを使用する際に利用されます。詳細は こちら でご確認ください。

使用方法

wsLink を使用するには、createWSClient で作成できる TRPCWebSocketClient を渡す必要があります:

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

認証 / 接続パラメータ

詳細はこちら

wsLink 関数には TRPCWebSocketClient の渡し込みが必要で、これは WebSocketClientOptions で定義されたフィールドで設定できます:

ts
export interface WebSocketLinkOptions {
client: TRPCWebSocketClient;
/**
* Data transformer
* @see https://trpc.io/docs/v11/data-transformers
**/
transformer?: DataTransformerOptions;
}
function createWSClient(opts: WebSocketClientOptions) => TRPCWebSocketClient
export interface WebSocketClientOptions {
/**
* The URL to connect to (can be a function that returns a URL)
*/
url: string | (() => MaybePromise<string>);
/**
* Connection params that are available in `createContext()`
* These are sent as the first message
*/
connectionParams: string | (() => MaybePromise<string>);
/**
* Ponyfill which WebSocket implementation to use
*/
WebSocket?: typeof WebSocket;
/**
* The number of milliseconds before a reconnect is attempted.
* @default {@link exponentialBackoff}
*/
retryDelayMs?: typeof exponentialBackoff;
/**
* Triggered when a WebSocket connection is established
*/
onOpen?: () => void;
/**
* Triggered when a WebSocket connection encounters an error
*/
onError?: (evt?: Event) => void;
/**
* Triggered when a WebSocket connection is closed
*/
onClose?: (cause?: { code?: number }) => void;
/**
* Lazy mode will close the WebSocket automatically after a period of inactivity (no messages sent or received and no pending requests)
*/
lazy?: {
/**
* Enable lazy mode
* @default false
*/
enabled: boolean;
/**
* Close the WebSocket after this many milliseconds
* @default 0
*/
closeMs: number;
};
/**
* Send ping messages to the server and kill the connection if no pong message is returned
*/
keepAlive?: {
/**
* @default false
*/
enabled: boolean;
/**
* Send a ping message every this many milliseconds
* @default 5_000
*/
intervalMs?: number;
/**
* Close the WebSocket after this many milliseconds if the server does not respond
* @default 1_000
*/
pongTimeoutMs?: number;
};
}
ts
export interface WebSocketLinkOptions {
client: TRPCWebSocketClient;
/**
* Data transformer
* @see https://trpc.io/docs/v11/data-transformers
**/
transformer?: DataTransformerOptions;
}
function createWSClient(opts: WebSocketClientOptions) => TRPCWebSocketClient
export interface WebSocketClientOptions {
/**
* The URL to connect to (can be a function that returns a URL)
*/
url: string | (() => MaybePromise<string>);
/**
* Connection params that are available in `createContext()`
* These are sent as the first message
*/
connectionParams: string | (() => MaybePromise<string>);
/**
* Ponyfill which WebSocket implementation to use
*/
WebSocket?: typeof WebSocket;
/**
* The number of milliseconds before a reconnect is attempted.
* @default {@link exponentialBackoff}
*/
retryDelayMs?: typeof exponentialBackoff;
/**
* Triggered when a WebSocket connection is established
*/
onOpen?: () => void;
/**
* Triggered when a WebSocket connection encounters an error
*/
onError?: (evt?: Event) => void;
/**
* Triggered when a WebSocket connection is closed
*/
onClose?: (cause?: { code?: number }) => void;
/**
* Lazy mode will close the WebSocket automatically after a period of inactivity (no messages sent or received and no pending requests)
*/
lazy?: {
/**
* Enable lazy mode
* @default false
*/
enabled: boolean;
/**
* Close the WebSocket after this many milliseconds
* @default 0
*/
closeMs: number;
};
/**
* Send ping messages to the server and kill the connection if no pong message is returned
*/
keepAlive?: {
/**
* @default false
*/
enabled: boolean;
/**
* Send a ping message every this many milliseconds
* @default 5_000
*/
intervalMs?: number;
/**
* Close the WebSocket after this many milliseconds if the server does not respond
* @default 1_000
*/
pongTimeoutMs?: number;
};
}

参考

このリンクのソースコードは GitHub でご確認いただけます。