WebSocketLink

Execute subscriptions (or other operations) over WebSocket with the subscriptions-transport-ws library


caution
We no longer recommend using WebSocketLink or the subscriptions-transport-ws library, because the library is not actively maintained. To execute subscriptions, We instead recommend using the newer graphql-ws library with the accompanying GraphQLWsLink.Whichever library you use, make sure you use the same library in your server and any clients you support. For more information, see Choosing a subscription library.

WebSocketLink is a terminating link that executes GraphQL operations over WebSocket connections using the subscriptions-transport-ws library. It's primarily used for GraphQL subscriptions but can also handle queries and mutations.

TypeScript
1 import { WebSocketLink } from "@apollo/client/link/ws";
2 import { SubscriptionClient } from "subscriptions-transport-ws";
3
4 const wsLink = new WebSocketLink(
5   new SubscriptionClient("ws://localhost:4000/subscriptions", {
6     reconnect: true,
7   })
8 );

Constructor signature

TypeScript
1constructor(
2  paramsOrClient: WebSocketLink.Configuration | SubscriptionClient
3): WebSocketLink

Installation

WebSocketLink requires the subscriptions-transport-ws library. Install it in your project:

shell
1npm install subscriptions-transport-ws

Types

Configuration options for creating a WebSocketLink instance.

Properties
Name / Type
Description
ClientOptions

Configuration options passed to the underlying SubscriptionClient.

These options configure the WebSocket connection behavior, including reconnection settings, connection parameters, and event handlers.

For a complete list of available options, see the supported subscriptions-transport-ws options.

string

The WebSocket endpoint URI to connect to.

This should be a valid WebSocket URI (starting with ws:// or wss://) that points to your GraphQL subscription endpoint.

"ws://localhost:4000/subscriptions"

A custom WebSocket implementation to use for the connection.

This is useful in environments that don't have native WebSocket support. You can provide a WebSocket polyfill or implementation that conforms to the W3C WebSocket API.

TypeScript
1 import WebSocket from "ws";
2
3 const wsLink = new WebSocketLink({
4   uri: "ws://localhost:4000/subscriptions",
5   webSocketImpl: WebSocket,
6 });
Feedback

Edit on GitHub

Ask Community