Getting real-time updates with Subscriptions for Expo and React Native
In this section, we will learn how to use GraphQL Subscriptions to monitor for changes. Subscriptions help us query the database and listen to changes in the data. This helps us provide instant updates as we can maintain an active connection with the database. Subscriptions are useful for tasks such as creating real-time notifications when something has changed in the database. Open the App.tsx
file and enter the following code anywhere within the useEffect
hook:
- The
createSubscription
method will listen to the create action on the database with the autogenerated code calledonCreatePost
of thesubscriptions
object. It will do this to fetch posts when new ones are posted:useEffect(() => { Â Â Â Â const fetchPosts = async (): Promise<any> => { Â Â Â Â Â Â try { Â Â Â Â Â Â Â Â console.log("fetching posts"); Â ...