/
Launch Apollo Studio

WebSocket Link

Execute subscriptions (or other GraphQL operations) over WebSocket


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

The WebSocketLink is a terminating link that's used most commonly with GraphQL subscriptions (which usually communicate over WebSocket), although you can send queries and mutations 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";

const link = new WebSocketLink({
  uri: "ws://localhost:3000/subscriptions",
  options: {
    reconnect: true
  }
});

Options

The WebSocketLink constructor takes an options object with the following fields:

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.

Edit on GitHub