If you want to use real-time updates on the browser, the easiest way is to use one of our libraries. They will handle all the hard-wiring for you, including reconnecting to a new subscription channel in case of network errors.
Please take a look at our Next.js integration guide to learn how you can use the Real-time Updates API to produce instant refresh of content as soon as it gets saved into DatoCMS.
We have also prepared a step-by-step tutorial that shows how to get to live-previews of draft content, so be sure to check that out!
You can also deploy and play with the code of one of our Next.js project starters, as they both support real-time updates:
If you're in a React project the react-datocms
package exposes a useQuerySubscription
hook that makes it trivial to make any webpage updated in real-time.
For more info on all the available options, please refer to its documentation on Github:
On any other JS environment you can use the datocms-listen
package which exposes a generic subscribeToQuery
function that encapsulates all the connection logic.
For more info on all the available options, please refer to its documentation on Github:
1import { subscribeToQuery } from "datocms-listen";2
3const unsubscribe = await subscribeToQuery({4 query: `5 query BlogPosts($first: IntType!) {6 allBlogPosts(first: $first) {7 title8 nonExistingField9 }10 }11 `,12 variables: { first: 10 },13 token: "YOUR_TOKEN",14 includeDrafts: true,15 onUpdate: (response) => {16 // response is the GraphQL response17 console.log(update.response.data);18 },19 onStatusChange: (status) => {20 // status can be "connected", "connecting" or "closed"21 console.log(status);22 },23 onChannelError: (error) => {24 // error will be something like:25 // {26 // code: "INVALID_QUERY",27 // message: "The query returned an erroneous response. Please consult the response details to understand the cause.",28 // response: {29 // errors: [30 // {31 // fields: ["query", "allBlogPosts", "nonExistingField"],32 // locations: [{ column: 67, line: 1 }],33 // message: "Field 'nonExistingField' doesn't exist on type 'BlogPostRecord'",34 // },35 // ],36 // },37 // }38 console.error(error);39 },40});