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
subscriptionsfolder inside the client'sapollofolder. You can save all subscriptions that you implement using Apollo'suseSubscriptionHook inside this folder. - Insert a
messageAdded.jsfile into the folder and paste in the following code:import { useSubscription, gql } from '@apollo/client'; export const MESSAGES_SUBSCRIPTION = gql' subscription onMessageAdded { messageAdded { ...