Notifications with Apollo subscriptions
In this section, I'll quickly guide you through the second use case for subscriptions. Showing notifications to a user is perfectly traditional and commonplace, as you know from Facebook. Instead of relying on the subscribeToMore
function, we use the Subscription
component that's provided by Apollo. This component works like the Query
and Mutation
components, but for subscriptions.
Follow these steps to get your first Subscription
component running:
- Create a
subscriptions
folder inside the client'sapollo
folder. You can save all subscriptions that you implement using Apollo'suseSubscription
Hook inside this folder. - Insert a
messageAdded.js
file into the folder and paste in the following code:import { useSubscription, gql } from '@apollo/client'; export const MESSAGES_SUBSCRIPTION = gql' subscription onMessageAdded { messageAdded { ...