Building a Real-time Collaborative Beatbox with React & GraphQL

Nader Dabit
Mar 19, 2019 · 4 min read

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 the data & store it in our database pretty easily ().

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

There is also a 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 & for use in our component.

We use the machine in our UI & we use the 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 () => {}
}

The method is called in Step.js.

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

Open GraphQL

Anything & Everything GraphQL

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store