Join us from October 8-10 in New York City to learn the latest tips, trends, and news about GraphQL Federation and API platform engineering.Join us for GraphQL Summit 2024 in NYC
Docs
Start for Free

WebSocket Link

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


⚠️ We no longer recommend using WebSocketLink or the subscriptions-transport-ws library, because the library is not actively maintained. To execute , 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.

We recommend reading Apollo Link overview before learning about individual links.

The WebSocketLink is a terminating link that's used most commonly with subscriptions (which usually communicate over WebSocket), although you can send queries and over WebSocket as well.

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

npm install subscriptions-transport-ws

Constructor

import { WebSocketLink } from "@apollo/client/link/ws";
import { SubscriptionClient } from "subscriptions-transport-ws";
const link = new WebSocketLink(
new SubscriptionClient("ws://localhost:4000/graphql", {
reconnect: true
})
);

Options

The WebSocketLink constructor takes either a SubscriptionClient object or an options object with the following . (These options are passed directly to the SubscriptionClient constructor.)

Name /
Type
Description
uri

String

Required. The URL of the WebSocket endpoint to connect to (e.g., ws://localhost:4000/subscriptions).

options

Object

Options for configuring the WebSocket connection.

See supported options

webSocketImpl

Object

A W3C-compliant WebSocket implementation to use. Provide this if your environment does not provide native WebSocket support (for example, in Node.js).

Usage

See Subscriptions.

Previous
Subscriptions (newer protocol)
Next
Community links
Rate articleRateEdit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc., d/b/a Apollo GraphQL.

Privacy Policy

Company