How I Built HypeBeats - A Real-time Collaborative Beatbox with React & GraphQL

dabit3

Nader Dabit

Posted on March 19, 2019

How I Built HypeBeats - A Real-time Collaborative Beatbox with React & GraphQL

To view the live demo, click here.
To view the repo, click here.


I’ve been playing around with GraphQL subscriptions quite a lot lately, adding real-time functionality to applications to make them interactive. Last week, I released a real-time drawing app using react-canvas-draw:

A few weeks ago I saw this talk at React NYC about building a beatbox using React Hooks.

I was pretty hyped after watching it. I then found the repo for it, cloned it & started thinking what I could do to make it even more interesting.

GitHub logo kenwheeler / hooks-drum-machine

React Day Berlin Demo

hooks-drum-machine

Simple step sequencer built with React Hooks

How to skrrt

  • Run npm start
  • Be dope



The drum machine basically has an assortment of beats (bass, clap, snare, etc..) that run through a step sequencer:

With a step sequencer you move in sequence from left to right stepping over each item in the sequence. If the item is highlighted, the associated sound plays.

The drum machine gives us an initial state of beats with an array set for each beat in the sequencer:

If a beat is set to 0 it does not play (not highlighted), if it's set to 1 (highlighted) then it does play, if it's set to 2 (blinking) it plays triple.

To learn more about how the drum machine works under the hood, check out the video here.

Making it collaborative & real-time with GraphQL subscriptions

The idea I had was making this collaborative. To do so, I needed to add the capabilities to create & share unique drum machines. Users can make updates / changes to the machines in real time & the music will update across all machines as changes are made.

To do this, I needed to do three main things:

  1. Make the drum machine dynamic
  2. Add routing
  3. Add real-time functionality, ideally GraphQL subscriptions

To make the drum machine dynamic & add subscriptions, I created a basic GraphQL schema to hold the drum machines that I deployed using AWS AppSync:

type Beatbox @model {
  id: ID!
  clientId: ID!
  beats: String!
  name: String!
}
Enter fullscreen mode Exit fullscreen mode

Because the state of the drum machine is a basic JSON object, I figured we could stringify the data & store it in our database pretty easily.

The other thing I had to think about was routing. To do so, I used React Router & came up with a routing scheme that looks like this:

/machine/:id/:name
Enter fullscreen mode Exit fullscreen mode

In addition to the beat data, each drum machine has a unique id & a name property for us to use to identify it & query for it.

There is also a clientId in order to handle subscription data, giving us a way to filter out duplicates on the client.

When the user lands on the route, for example:

/machine/d562581d-2a2d-4597-a6bb-2580deaf0254/BassLord
Enter fullscreen mode Exit fullscreen mode

We parse the url & retrieve the machine name & id.

We use the machine name in our UI & we use the id to create or query for the machine.

When the user makes changes, we trigger an update in our API:

// DrumMachine.js
async function updateBeatbox(beats, machineId) {
  const beatbox = {
    id: machineId, clientId, beats: JSON.stringify(beats)
  }
  try {
    await API.graphql(graphqlOperation(UpdateBeatbox, { input: beatbox }))
    console.log('successfully updated beatbox...')
  } catch (err) {
    console.log('error updating beatbox...:', err)
  }
  return () => {}
}
Enter fullscreen mode Exit fullscreen mode

The updateBeatBox method is called in Step.js.

We also create a subscription to listen for updates in the GraphQL API:

// DrumMachine.js
useEffect(() => {
  const subscriber = API.graphql(graphqlOperation(onUpdateBeatbox)).subscribe({
    next: data => {
      const { value: { data: { onUpdateBeatbox: { clientId: ClientId, beats }}}} = data
      if (ClientId === clientId) return
      setSteps(JSON.parse(beats))
    }
  });
  return () => subscriber.unsubscribe()
}, []);
Enter fullscreen mode Exit fullscreen mode

Other than that, I pretty much used the functionality of the drum machine as is!

To view the live demo, click here.
To view the repo, click here.

Contributing / Next Steps

The next things I'd like to do to this is add more beats! I'd also like to add some more sounds. If you'd like to contribute to this, feel free to reach out to me & or submit a pull request!
If you'd like to deploy this application yourself, check out the documentation in the repo to get up & running.


My Name is Nader Dabit. I am a Developer Advocate at Amazon Web Services working with projects like AWS AppSync and AWS Amplify. I specialize in cross-platform & cloud-enabled application development.

💖 💪 🙅 🚩
dabit3
Nader Dabit

Posted on March 19, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related