Building a Real-time Collaborative Beatbox with React & GraphQL

Nader Dabit
Mar 19 · 4 min read

How I used GraphQL Subscriptions to build HypeBeats — a real-time collaborative beatbox with React Hooks Drum Machine & GraphQL.

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 from Ken Wheeler 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.

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

React Hooks Drum Machine

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.

Making it collaborative & real-time with GraphQL subscriptions

The thought 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 DrumMachine @model {
  id: ID!
  clientId: ID!
  beats: String!
  name: String!

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 (beats).

In addition to the beats 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.

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:


When the user lands on the route, for example:


We parse the url & retrieve the machine name & id for use in our component.

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.jsasync 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 () => {}

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

// DrumMachine.jsuseEffect(() => {
  const subscriber = API.graphql(graphqlOperation(onUpdateBeatbox)).subscribe({
    next: data => {
      const { value: { data: { onUpdateBeatbox: { clientId: ClientId, beats }}}} = data
      if (ClientId === clientId) return
  return () => subscriber.unsubscribe()
}, []);

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

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.

Open GraphQL

Anything & Everything GraphQL

Nader Dabit

Written by

Developer Advocate at AWS Mobile — Specializing in Teaching and Building React & React Native —

Open GraphQL

Anything & Everything GraphQL