The application covered here is a polling app with no server component to run: static assets are served from GitHub Pages' CDN, while all state and data access live in Macrometa's Global Data Network (GDN), a geo-distributed stateful serverless platform that keeps reads and writes fast regardless of client location and scales elastically. The GDN serves as the data store, and its streams subscribe to updates so the UI can react in real time.

Gatsby handles the front end and the deployment target. The gatsby-source-c8db plugin retrieves markdown content from Macrometa, which Gatsby turns into HTML at build time; the Macrometa JSC8 SDK keeps a socket open for real-time updates and wraps the platform's API calls.

Prerequisites

  1. Install Node.js and npm.
  2. Install the Gatsby CLI: npm install -g gatsby-cli
  3. Sign up for a free Macrometa developer account.

Creating the collections

Two document collections carry the application's data. The first, markdownContent, holds static content as documents with title and content fields written in markdown; both keys are converted by gatsby-source-c8db, with title becoming <h2></h2> and content becoming <p></p>.

{
  "title": "## Real-Time Polling Application",
  "content": "Full-Stack Geo-Distributed Serverless App Built with GatsbyJS and Macrometa!"
}

The second, polls, stores each poll as its own document:

{
  "pollName": "What is your spirit animal?",
  "polls": [
    {
      "editing": false,
      "id": "975e41",
      "text": "dog",
      "votes": 2
    },
    {
      "editing": false,
      "id": "b8aa60",
      "text": "cat",
      "votes": 1
    },
    {
      "editing": false,
      "id": "b8aa42",
      "text": "unicorn",
      "votes": 10
    }
  ]
}

Configuration and credentials

Macrometa login details go into gatsby-config.js, together with the collections to use and the markdown transformation rules:

 {
  resolve: "gatsby-source-c8db",
  options: {
    config: "https://gdn.paas.macrometa.io",
    auth: {
    email: "<my-email>",
     password: "process.env.MM_PW"
  },
    geoFabric: "_system",
    collection: 'markdownContent',
    map: {
      markdownContent: { 
        title: "text/markdown",
        content: "text/markdown" 
      }
    }
  }
}

The password value is read from process.env.MM_PW rather than written directly. To supply it, create .env.development and .env.production in the project root, each containing one line:

MM_PW='<your-password-here>'

Add both files to .gitignore so the password is never pushed to GitHub.

Running locally and deploying

After forking the repository, creating the Macrometa account and collections, and adding the password files:

  1. Run npm install.
  2. Run npm run develop. This starts the development server on http://localhost:<some_port> and the GraphQL server at http://localhost:<some_port>/___graphql.

To publish, run npm run deploy. Gatsby generates the static site, creates a gh-pages branch, and deploys to GitHub; the app is then reachable at <your-github-username>.github.io/tutorial-jamstack-pollingapp. If it doesn't appear, check the repository settings and confirm GitHub Pages is enabled and pointed at the gh-pages branch.

Inside the code

The client code does three things first: loads the Macrometa JSC8 driver, opens a socket to Macrometa, and defines the API calls the app relies on. The resulting configuration is then made available application-wide:

Front-end event handlers sit on top of that layer. The vote submission handler, for example, looks like this:

onVote = async (onSubmitVote, getPollData, establishLiveConnection) => {
  const { title } = this.state;
  const { selection } = this.state;
  this.setState({ loading: true }, () => {
  onSubmitVote(selection)
    .then(async () => {
      const pollData = await getPollData();
      this.setState({ loading: false, hasVoted: true, options: Object.values(pollData) }, () => {
        // open socket connections for live updates
        const onmessage = msg => {
          const { payload } = JSON.parse(msg);
          const decoded = JSON.parse(atob(payload));
          this.setState({ options: decoded[title] });
        }
        establishLiveConnection(onmessage);
      });
    })
    .catch(err => console.log(err))
  });
}

A live version of the app is available, where a poll can be created and its vote URL shared with anyone who should cast a vote on the same topic.