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:
import React from "react";import { useQuerySubscription } from "react-datocms";const App: React.FC = () => {const { status, error, data } = useQuerySubscription({query: `query AppQuery($first: IntType) {allBlogPosts {slugtitle}}`,variables: { first: 10 },token: "YOUR_API_TOKEN",});const statusMessage = {connecting: "Connecting to DatoCMS...",connected: "Connected to DatoCMS, receiving live updates!",closed: "Connection closed",};return (<div><p>Connection status: {statusMessage[status]}</p>{error && (<div><h1>Error: {error.code}</h1><div>{error.message}</div>{error.response && (<pre>{JSON.stringify(error.response, null, 2)}</pre>)}</div>)}{data && (<ul>{data.allBlogPosts.map((blogPost) => (<li key={blogPost.slug}>{blogPost.title}</li>))}</ul>)}</div>);};
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:
import { subscribeToQuery } from "datocms-listen";const unsubscribe = await subscribeToQuery({query: `query BlogPosts($first: IntType!) {allBlogPosts(first: $first) {titlenonExistingField}}`,variables: { first: 10 },token: "YOUR_TOKEN",includeDrafts: true,onUpdate: (response) => {// response is the GraphQL responseconsole.log(update.response.data);},onStatusChange: (status) => {// status can be "connected", "connecting" or "closed"console.log(status);},onChannelError: (error) => {// error will be something like:// {// code: "INVALID_QUERY",// message: "The query returned an erroneous response. Please consult the response details to understand the cause.",// response: {// errors: [// {// fields: ["query", "allBlogPosts", "nonExistingField"],// locations: [{ column: 67, line: 1 }],// message: "Field 'nonExistingField' doesn't exist on type 'BlogPostRecord'",// },// ],// },// }console.error(error);},});