Setting up Apollo Client for subscriptions
In this section, we'll set up Apollo Client for subscriptions to notify users if the other users on the network liked or commented on their posts in real time.
Subscriptions need a persistent connection, so they can't be handled using the default HTTP protocol that Apollo Client uses for queries and mutations. Instead, they are sent over WebSocket, via the community-maintained subscriptions-transport-ws
library, which we need to install in our project:
- Head over to your Terminal, make sure you have navigated inside your frontend project's folder (the
client/
folder), and run the following command:npm install subscriptions-transport-ws
- Open the
src/app/graphql.module.ts
file and start by adding the following imports:import { ApolloClientOptions, from, split } from '@apollo/client/core'; import { WebSocketLink } from '@apollo/client/link/ws'; import...