New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

datocms-listen

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

datocms-listen

![MIT](https://img.shields.io/npm/l/datocms-listen?style=for-the-badge) ![MIT](https://img.shields.io/npm/v/datocms-listen?style=for-the-badge) [![Build Status](https://img.shields.io/travis/datocms/datocms-listen?style=for-the-badge)](https://travis-ci.o

0.1.1
Source
npm
Version published
Weekly downloads
29K
-21.94%
Maintainers
1
Weekly downloads
 
Created
Source

datocms-listen

MIT MIT Build Status

A set of components and utilities to work faster with DatoCMS in React environments. Integrates seamlessy with DatoCMS's GraphQL Content Delivery API.

  • TypeScript ready;
  • Compatible with IE11;
  • CSS-in-JS ready;
  • Compatible with any GraphQL library (Apollo, graphql-hooks, graphql-request, etc.);
  • Usable both client and server side;
  • Compatible with vanilla React, Next.js and pretty much any other React-based solution;

Table of Contents

Installation

npm install datocms-listen

Live real-time updates

useQuerySubscription is a React hook that you can use to implement client-side updates of the page as soon as the content changes. It uses DatoCMS's GraphQL server-sent events (SSE) protocol to receive the updated query results in real-time, and is able to reconnect in case of network failures.

Live updates are great both to get instant previews of your content while editing it inside DatoCMS, or to offer real-time updates of content to your visitors (ie. news site).

Reference

Import useQuerySubscription from datocms-listen and use it inside your components like this:

const {
  data: QueryResult | undefined,
  error: ChannelErrorData | null,
  status: ConnectionStatus,
} = useQuerySubscription(options: Options);

Initialization options

proptyperequireddescriptiondefault
enabledboolean:x:Whether the subscription has to be performed or nottrue
querystring:white_check_mark:The GraphQL query to subscribe
tokenstring:white_check_mark:DatoCMS API token to use
variablesObject:x:GraphQL variables for the query
previewboolean:x:If true, the Content Delivery API with draft content will be usedfalse
environmentstring:x:The name of the DatoCMS environment where to perform the querydefaults to primary environment
initialDataObject:x:The initial data to use on the first render
reconnectionPeriodnumber:x:In case of network errors, the period to wait to reconnect
fetcha fetch-like function:x:The fetch function to use to perform the registration querywindow.fetch
baseUrlstring:x:The base URL to use to perform the queryhttps://graphql-listen.datocms.com

Connection status

The status property represents the state of the server-sent events connection. It can be one of the following:

  • connecting: the subscription channel is trying to connect
  • connected: the channel is open, we're receiving live updates
  • closed: the channel has been permanently closed due to a fatal error (ie. an invalid query)

Error object

proptypedescription
codestringThe code of the error (ie. INVALID_QUERY)
messagestringAn human friendly message explaining the error
responseObjectThe raw response returned by the endpoint, if available

Example

import React from "react";
import { useQuerySubscription } from "datocms-listen";

const App: React.FC = () => {
  const { status, error, data } = useQuerySubscription({
    enabled: true,
    query: `
      query AppQuery($first: IntType) {
        allBlogPosts {
          slug
          title
        }
      }`,
    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>
  );
};

FAQs

Package last updated on 04 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts