Building a Real-time Collaborative Beatbox with React & GraphQL

How I used GraphQL Subscriptions to build HypeBeats — a real-time collaborative beatbox with React Hooks Drum Machine & 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 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.

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 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:

/machine/:id/:name

When the user lands on the route, for example:

/machine/d562581d-2a2d-4597-a6bb-2580deaf0254/BassLord

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.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 () => {}
}
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()
}, []);

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

To view the final repo, click here.
To try it out the drum machine, 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.

Special thanks to Ken Wheeler for making the drum machine!

My Name is Nader Dabit .
I am a Developer Advocate at AWS working with projects like AWS AppSync and AWS Amplify.
I’m also the author of React Native in Action, & the editor of React Native Training & OpenGraphQL.