Build a TikTok Clone With a Twist
Tl;dr: Learn how to leverage Stargate to deliver schemaless JSON and REST/GraphQL to Apache Cassandra by building your own TikTok clone.
Join the DZone community and get the full member experience.
Join For FreeIt is a really great time to be a developer.
We have tons of APIs integrated within great tools for building dynamic, full stack apps. If you are a developer, you probably are using technologies like schemaless data stores, serverless architectures, JSON APIs, and/or the GraphQL language.
And now, thanks to Stargate, Apache Cassandra is a part of this world.
What exactly is Stargate?
Stargate is an open source data gateway that sits between your app server and your databases.
Multiple successful app companies - like Netflix and Yelp - built their own data gateways to help internal app developers create features using simple APIs, without needing to learn the underlying database or mess with schema. To give you the same power and ease of access to your data, DataStax integrated Stargate into DataStax Astra as the official data API.
If you are familiar with Apache Cassandra - the most proven open source NoSQL database trusted by brands like Netflix, Instagram, Spotify, Apple and so many more - Stargate brings together an API gateway with Cassandra’s distributed data request coordination code into one OSS project. Separating coordination (Stargate) nodes from storage (Cassandra) nodes enables optimization of each node type and independent scaling, or replacement, on either side of the gateway.
Stargate abstracts Cassandra-specific database concepts entirely so that you can work with your data the way you want -- JSON via schemaless document APIs or database schema aware GraphQL and RESTful APIs -- while Stargate serves as the proxy that coordinates these requests to different flavors of Cassandra.
To see it in action, Let’s see how this works by using JSON with Stargate’s schemaless Document API in a TikTok clone. Because, if Instagram and Snapchat have a TikTok clone, we should have one, too. Right?
Posts in TikTok
We are going to walk through using Stargate’s APIs for creating and updating posts within a TikTok clone. We are using DataStax Astra to host the database for free.
To use Stargate in Astra in your app, first install and set up our JavaScript SDK. You can learn about storing environment variables in your .env file here.
Let’s start with a basic TikTok post: a video with a caption, like:
xxxxxxxxxx
const postData = {
"postId": 0,
"video": "https://i.imgur.com/FTBP02Y.mp4",
"caption": "These ducks are cute",
"timestamp": "2020-12-09T09:08:31.020Z",
"likes": 0,
}
After connecting to Stargate in Astra with your nodejs client, let’s create a new collection in our app and add the post with:
xxxxxxxxxx
const postsCollection = astraClient.namespace("tikTokClone").collection("posts");
const post = await postsCollection.create(postData);
Stargate allows you to add data to Apache Cassandra in one line of code.
This level of ease of use hasn't previously been possible with Cassandra. Insert JSON and move on.
Next up, let’s say you want to find all posts about ducks. You can do that via:
xxxxxxxxxx
// find all posts about ducks
const posts = await postsCollection.find({ caption:
{ $in: ["ducks"] } });
And boom. Now you have your ducks channel all set up for your users. Because who doesn’t want a stream fully dedicated to ducks?
Now, your app isn’t going to be like Twitter. We can edit stuff here. Let’s show how to edit your post’s caption. Stories though? That’s on you.
xxxxxxxxxx
// update the post’s caption
const post = await postsCollection.update(post.documentId, {
caption: "These ducks are MEGA cute",
});
What’s next?
Want to see some more code? You can head to Stargate.io and check out the longer version of this example, or head to YouTube to view Ania Kubow’s full app.
Want to get your hands on it ASAP? You can try it now as Stargate is now live on Astra as the official data API. Astra offers a 5GB free-forever tier, with no credit card required so you can play.
Huzzah! Or, if you want to give Stargate a spin in your own environment, you can find Stargate on GitHub to download and use it wherever you want.
Lastly - have a fun idea? We would love to see how you customize your TikTok clone to show off more ways to feature data in your app. Come join our OSS community and tell us about your app in our contribute channel.
Let me know when you have stories up and I can subscribe to your ducks channel.
We hope you enjoy slinging JSON to and from Apache Cassandra as much as we do. You can get hands on with this right away in DataStax Astra, or check out our sample app gallery to see schemaless Cassandra in action.
Happy building!
Opinions expressed by DZone contributors are their own.
Comments