This is part one of this blog series, where we'll learn how to build a YouTube clone. In this part 1, we'll set up the Strapi CMS backend with collections, create data relationships, create custom endpoints for liking, commenting, and viewing videos, set up Socket.io, and create lifecycle methods to listen to real-time updates on Strapi collections.
For reference, here's the outline of this blog series:
Before we dive in, ensure you have the following:
Below is the folder structure for the app we'll be building throughout this tutorial.
📦youtube_clone
┣ 📂config: Configuration for the app.
┃ ┣ 📜admin.ts: Admin settings.
┃ ┣ 📜api.ts: API configuration.
┃ ┣ 📜database.ts: Database connection settings.
┃ ┣ 📜middlewares.ts: Middleware configuration.
┃ ┣ 📜plugins.ts: Plugin settings.
┃ ┣ 📜server.ts: Server settings.
┃ ┗ 📜socket.ts: Socket configuration.
┣ 📂database: Database setup files.
┃ ┃ ┣ 📂users-permissions: User permissions config.
┃ ┃ ┃ ┗ 📂content-types: User data structure.
┃ ┣ 📂utils: Utility functions.
┃ ┃ ┗ 📜emitEvent.ts: Event emitter utility.
┃ ┗ 📜index.ts: App's main entry point.
┣ 📂types: TypeScript type definitions.
┃ ┗ 📂generated: Auto-generated type files.
┃ ┃ ┣ 📜components.d.ts: Component types.
┃ ┃ ┗ 📜contentTypes.d.ts: Content type definitions.
┣ 📜.env: Environment variables.
When building a video Streaming app, the developer is required to carefully select the right technologies that will provide an uninterrupted user experience. For the backend, the developer is required to use a scalable infrastructure because chat applications usually have high traffic (different users making concurrent real-time requests), the user base grows faster, and they generate a large amount of data.
Strapi 5 comes with an effective, scalable, headless CMS that is flexible and easy to use. It allows developers to structure their content, manage their media, and build complex data relationships, eliminating all the overheads from traditional backend development.
On the user end, Flutter provides a great solution for easily developing visually pleasant and highly responsive mobile applications. It improves the development of highly performant applications with a different feel and looks for both iOS and Android platforms, supported by its extensive pre-designed widget collection and robust ecosystem.
In this series, we’ll be building a video streaming app that allows users to upload videos, view a feed of videos, and interact with content through likes and comments.
The app will feature:
Below is a demo of what we will build by the end of this blog series.Link
Let's start by setting up a Strapi 5 project for our backend. Create a new project by running the command below:
npx create-strapi-app@rc my-project --quickstart
The above command will scaffold a new Strapi 5 project and install the required Node.js dependencies. Strapi uses SQL database as the default database management system. We'll stick with that for the demonstrations in this tutorial.
Once the installation is completed, the project will run and automatically open on your browser at http://localhost:1337
.
Now, fill out the form to create your first Strapi administration account and authenticate to the Strapi Admin Panel.
Strapi allows you to create and manage your database model from the Admin panel. We'll create a Video and Comment collections to save the video data and users' comments on videos. To do that, click on the Content-Type Builder -> Create new collection type tab from your Admin panel to create a Video collection for your application and click Continue.
Then add the following fields to Video collection:
Field | Type |
---|---|
title | Short Text field |
description | Long Text field |
thumbnail | Single Media field |
video_file | Single Media field |
Then click the Save button.
Next, click Create new collection type to create a Comment collection and click Continue.
Add a text
field (short text) to the Comment collection and click the Save button. Lastly, click on User -> Add another field -> Media and add a new field named profile_picture
to allow users to upload their profile pictures when creating an account on the app.
I left out some fields in our Video and Comments collections because they are relations fields. I needed us to cover them separately. For the Video Collection, the fields are:
uploader
views
comments
likes
For the Comment collection, the fields are:
video
(the video commented on)user
(the user who posted the comment).To add the relation fields to the Video collection, click on the Video -> Add new fields from Content-Type Builder page. Select a Relations from the fields modal, and add a new relation field named comments
, which will be a many-to-many relationships with the User collection. This is so that a user can comment on other users' videos, and another can also comment on their videos. Now click on the Finish button to save the changes.
Select Relation for Video and Comment Collection
A video can have many comments
Repeat this process to create the relation field for the uploader
, likes
, and views
fields. Your Video collection should look like the screenshot below:
For the Comment collection, click on the Comment -> Add new fields from the Content-Type Builder page. Select a Relation from the fields modal and add a new relation field named user
, which will be a many-to-many relationship with the User collection. Then click on the Finish button to save the changes.
Create Comment and User relationship
A User can have multiple comments
To create the video
relation field, you must also repeat this process. After the fields, your Comment collection will look like the screenshot below:
Now update your User Collection to add a new relation field named subscribers
to save users' video subscribers. Click User -> Add new fields from the Content-Type Builder page, select the Relation field, and enter subscribers
, which is also a many-to-many relation with the User collection. On the left side, name the field subscribers
, and on the right, which is the User
collection, name it user_subscribers
since they are related to the same collection.
With collections and relationships created, let's create custom controllers in our Strapi 5 backend to allow users to like, subscribe, and track users who viewed videos. In your Strapi project, open the video/controllers/video.ts
file, and extend your Strapi controller to add the like functionality with the code:
1import { factories } from "@strapi/strapi";
2
3export default factories.createCoreController(
4 "api::video.video",
5 ({ strapi }) => ({
6 async like(ctx) {
7 try {
8 const { id } = ctx.params;
9 const user = ctx.state.user;
10
11 if (!user) {
12 return ctx.forbidden("User must be logged in");
13 }
14
15 // Fetch the video with its likes
16 const video: any = await strapi.documents("api::video.video").findOne({
17 documentId: id,
18 populate: ["likes"],
19 });
20
21
22 if (!video) {
23 return ctx.notFound("Video not found");
24 }
25
26 // Check if the user has already liked this video
27 const hasAlreadyLiked = video.likes.some((like) => like.id === user.id);
28 let updatedVideo;
29 if (hasAlreadyLiked) {
30 // Remove the user's like
31
32 video.updatedVideo = await strapi
33 .documents("api::video.video")
34 .update({
35 documentId: id,
36 data: {
37 likes: video.likes.filter(
38 (like: { documentId: string }) =>
39 like.documentId !== user.documentId,
40 ),
41 },
42 populate: ["likes"],
43 });
44 } else {
45 // Add the user's like
46 updatedVideo = await strapi.documents("api::video.video").update({
47 documentId: id,
48 populate:"likes",
49 data: {
50 likes: [...video.likes, user.documentId] as any,
51 },
52 });
53 }
54 return ctx.send({
55 data: updatedVideo,
56 });
57 } catch (error) {
58 return ctx.internalServerError(
59 "An error occurred while processing your request",
60 );
61 }
62 },
63);
The above code fetches the video the user wants to like and checks if the user has already liked it. If true, it unlikes the video by removing it from the array of likes for that video. Otherwise, it adds a new user object to the array of likes for the video and writes to the database for both cases to update the video records.
Then add the code below to the video controller for the views functionality:
1 //....
2
3 export default factories.createCoreController(
4 //....
5 async incrementView(ctx) {
6 try {
7 const { id } = ctx.params;
8 const user = ctx.state.user;
9
10 if (!user) {
11 return ctx.forbidden("User must be logged in");
12 }
13
14 // Fetch the video with its views
15 const video: any = await strapi.documents("api::video.video").findOne({
16 documentId: id,
17 populate: ["views", "uploader"],
18 });
19
20 if (!video) {
21 return ctx.notFound("Video not found");
22 }
23 // Check if the user is the uploader
24 if (user.id === video.uploader.id) {
25 return ctx.send({
26 message: "User is the uploader, no view recorded.",
27 });
28 }
29
30 // Get the current views
31 const currentViews =
32 video.views.map((view: { documentId: string }) => view.documentId) ||
33 [];
34 // Check if the user has already viewed this video
35 const hasAlreadyViewed = currentViews.includes(user.documentId);
36
37 if (hasAlreadyViewed) {
38 return ctx.send({ message: "User has already viewed this video." });
39 }
40
41 // Add user ID to the views array without removing existing views
42 const updatedViews = [...currentViews, user.documentId];
43 // Update the video with the new views array
44 const updatedVideo: any = await strapi
45 .documents("api::video.video")
46 .update({
47 documentId: id,
48 data: {
49 views: updatedViews as any,
50 },
51 });
52 return ctx.send({ data: updatedVideo });
53 } catch (error) {
54 console.error("Error in incrementView function:", error);
55 return ctx.internalServerError(
56 "An error occurred while processing your request",
57 );
58 }
59 },
60 );
The above code fetches the video clicked by the user by calling the strapi.service("api::video.video").findOne
method, which checks if the video has been liked by the video before, to avoid a case where a user likes a video twice. If the check is true it will simply send a success message, else it will update the video record to add the user object to the array of likes and write to the database by calling the strapi.service("api::video.video").update
method.
Lastly, add the code to implement the subscribe functionality to allow users to subscribe to channels they find interesting:
1 //....
2
3 export default factories.createCoreController(
4 //....
5 async subscribe(ctx) {
6 try {
7 const { id } = ctx.params;
8 const user = ctx.state.user;
9
10 if (!user) {
11 return ctx.forbidden("User must be logged in");
12 }
13
14 // Fetch the uploader and populate the subscribers relation
15 const uploader = await strapi.db
16 .query("plugin::users-permissions.user")
17 .findOne({
18 where: { id },
19 populate: ["subscribers"],
20 });
21
22 if (!uploader) {
23 return ctx.notFound("Uploader not found");
24 }
25
26 // Check if the user is already subscribed
27 const isSubscribed =
28 uploader.subscribers &&
29 uploader.subscribers.some(
30 (subscriber: { id: string }) => subscriber.id === user.id,
31 );
32
33 let updatedSubscribers;
34
35 if (isSubscribed) {
36 // If subscribed, remove the user from the subscribers array
37 updatedSubscribers = uploader.subscribers.filter(
38 (subscriber) => subscriber.id !== user.id,
39 );
40 } else {
41 // If not subscribed, add the user to the subscribers array
42 updatedSubscribers = [...uploader.subscribers, user.id];
43 }
44
45 // Update the uploader with the new subscribers array
46 const updatedUploader = await strapi
47 .query("plugin::users-permissions.user")
48 .update({
49 where: { id },
50 data: {
51 subscribers: updatedSubscribers,
52 },
53 });
54
55 return ctx.send({
56 message: isSubscribed
57 ? "User has been unsubscribed from this uploader."
58 : "User has been subscribed to this uploader.",
59 data: updatedUploader,
60 });
61 } catch (error) {
62 console.error("Error in subscribe function:", error);
63 return ctx.internalServerError(
64 "An error occurred while processing your request",
65 );
66 }
67 },
68 }),
69);
The above code fetches the details of the channel owner using the Strapi users permission plugin. plugin::users-permissions.user
. After that, it uses the strapi.db.query("plugin::users-permissions.user").findOne
method to check if the subscriber user id is present in the subscriber's array, if true, it removes the user from the array of subscribers. Else, it adds the user to the array of subscribers user objects.
Next, create a new file named custom-video.ts
in the video/routes
folder and add the following custom endpoints for the controllers we defined earlier:
1 export default {
2 routes: [
3 {
4 method: 'PUT',
5 path: '/videos/:id/like',
6 handler: 'api::video.video.like',
7 config: {
8 policies: [],
9 middlewares: [],
10 },
11 },
12 {
13 method: 'PUT',
14 path: '/videos/:id/increment-view',
15 handler: 'api::video.video.incrementView',
16 config: {
17 policies: [],
18 middlewares: [],
19 },
20 },
21 {
22 method: 'PUT',
23 path: '/videos/:id/subscribe',
24 handler: 'api::video.video.subscribe',
25 config: {
26 policies: [],
27 middlewares: [],
28 },
29 },
30 ],
31 };
The above code defines custom routes for like
, incrementView
, and subscribe
. The endpoint can be accessed at http://localhost:1337/api/videos/:id/like
, http://localhost:1337/api/videos/:id/like
, and http://localhost:1337/api/videos/:id/like
, respectively.
Strapi provides authorization for your collections out of the box, you only need to specify what kind of access you give users. To do this, navigate to Settings -> Users & Permissions plugin -> Role.
Here you will find two user roles:
For the Authenticated role, give the following access to the collections:
Collection | Access |
---|---|
Comments | find , create , findOne and update |
Videos | create , incrementView , subscribe , update , find , findOne , and like |
Upload | upload |
Users-permissions(User) | find , findOne , update , me |
Then give the Public role the following access to the collections:
Collection | Access |
---|---|
Comments | find and findOne |
Videos | find and findOne |
Upload | upload |
Users-permissions (User) | find and findOne |
The above configurations allow the Public role (unauthenticated user) to view videos and the details of the user who uploaded the video, such as username, profile picture, and number of subscribers. We also gave it access to see comments on videos and upload files because users must upload a profile during sign-up. For the Authenticated role, we gave it more access to comment, like, subscribe, and update their user details.
We need to allow users to get real-time updates when a new video, comment, or like is created or when a video is updated. To do this, we'll use Socket.IO. We'll write a custom Socket implementation in our Strapi project to handle real-time functionalities.
First, install Socket.IO in your Strapi project by running the command below:
npm install socket.io
Then, create a new folder named socket
in the api
directory for the socket API. In the api/socket
directory, create a new folder named services
and a socket.ts
file in the services
folder. Add the code snippets below to setup and initialize a socket connection:
1import { Core } from "@strapi/strapi";
2
3export default ({ strapi }: { strapi: Core.Strapi }) => ({
4 initialize() {
5 strapi.eventHub.on('socket.ready', async () => {
6 const io = (strapi as any).io;
7 if (!io) {
8 strapi.log.error("Socket.IO is not initialized");
9 return;
10 }
11
12 io.on("connection", (socket: any) => {
13 strapi.log.info(`New client connected with id ${socket.id}`);
14
15 socket.on("disconnect", () => {
16 strapi.log.info(`Client disconnected with id ${socket.id}`);
17 });
18 });
19
20 strapi.log.info("Socket service initialized successfully");
21 });
22 },
23
24 emit(event: string, data: any) {
25 const io = (strapi as any).io;
26 if (io) {
27 io.emit(event, data);
28 } else {
29 strapi.log.warn("Attempted to emit event before Socket.IO was ready");
30 }
31 },
32});
Then update your src/index.ts
file to initialize the Socket.IO server, set up event listeners for user updates and creations, and integrate the socket service with Strapi's lifecycle hooks:
1import { Core } from "@strapi/strapi";
2import { Server as SocketServer } from "socket.io";
3import { emitEvent, AfterCreateEvent } from "./utils/emitEvent";
4
5interface SocketConfig {
6 cors: {
7 origin: string | string[];
8 methods: string[];
9 };
10}
11
12export default {
13 register({ strapi }: { strapi: Core.Strapi }) {
14 const socketConfig = strapi.config.get("socket.config") as SocketConfig;
15
16 if (!socketConfig) {
17 strapi.log.error("Invalid Socket.IO configuration");
18 return;
19 }
20
21 strapi.server.httpServer.on("listening", () => {
22 const io = new SocketServer(strapi.server.httpServer, {
23 cors: socketConfig.cors,
24 });
25
26 (strapi as any).io = io;
27 strapi.eventHub.emit("socket.ready");
28 });
29 },
30
31 bootstrap({ strapi }: { strapi: Core.Strapi }) {
32 const socketService = strapi.service("api::socket.socket") as {
33 initialize: () => void;
34 };
35 if (socketService && typeof socketService.initialize === "function") {
36 socketService.initialize();
37 } else {
38 strapi.log.error("Socket service or initialize method not found");
39 }
40 },
41};
The above code sets up the Socket.IO configuration, creates a new SocketServer
instance when the HTTP server starts listening, and subscribes to database lifecycle events for User collection to emit real-time updates.
Next, create a new folder named utils
in the src
folder. In the utils
folder, create an emitEvents.ts
file and add the code snippets below to define an emitEvent
function:
1import type { Core } from "@strapi/strapi";
2
3interface AfterCreateEvent {
4 result: any;
5}
6
7function emitEvent(eventName: string, event: AfterCreateEvent) {
8 const { result } = event;
9 const strapi = global.strapi as Core.Strapi;
10
11 const socketService = strapi.service("api::socket.socket");
12 if (socketService && typeof (socketService as any).emit === "function") {
13 (socketService as any).emit(eventName, result);
14 } else {
15 strapi.log.error("Socket service or emit method not found");
16 }
17}
18
19export { emitEvent, AfterCreateEvent };
This function emits socket events when certain database actions occur. It takes an event name and an AfterCreateEvent
object as parameters, extracts the result from the event, and uses the socket service to emit the event with the result data.
Now let's use the lifecycle method for the Video and Comment collection methods to listen to create, update, and delete events. Create a lifecycles.ts
file in the api/video/content-type/video
folder and add the code below:
1import { emitEvent, AfterCreateEvent } from "../../../../utils/emitEvent";
2
3export default {
4 async afterUpdate(event: AfterCreateEvent) {
5 emitEvent("video.updated", event);
6 },
7 async afterCreate(event: AfterCreateEvent) {
8 emitEvent("video.created", event);
9 },
10 async afterDelete(event: AfterCreateEvent) {
11 emitEvent("video.deleted", event);
12 },
13};
Next, create a lifecycles.ts
file in the api/comment/content-type/comment
folder and add the code below:
1import { emitEvent, AfterCreateEvent } from "../../../../utils/emitEvent";
2
3export default {
4 async afterCreate(event: AfterCreateEvent) {
5 emitEvent("comment.created", event);
6 },
7
8 async afterUpdate(event: AfterCreateEvent) {
9 emitEvent("comment.updated", event);
10 },
11
12 async afterDelete(event: AfterCreateEvent) {
13 emitEvent("comment.deleted", event);
14 },
15};
Lastly, update the bootstrap
function in your src/index.ts
file to listen to create and update events in the users-permissions.user
plugin:
1 // ...
2 bootstrap({ strapi }: { strapi: Core.Strapi }) {
3 //...
4 strapi.db.lifecycles.subscribe({
5 models: ["plugin::users-permissions.user"],
6 async afterUpdate(event) {
7 emitEvent("user.updated", event as AfterCreateEvent);
8 },
9 async afterCreate(event) {
10 emitEvent("user.created", event as AfterCreateEvent);
11 },
12 });
13 },
To test this out, open a new Postman Socket.io window.
Then, connect to your Strapi backend by entering the Strapi API URL. Click the Events tab and enter the lifecycle events we created in the Strapi backend. Check the Listen boxes for all the events you want to monitor, and click the Connect button.
Now return to your Strapi Admin panel, navigate to Content Manager -> Video -> + Create new entries, and create new video entries.
Once you perform actions in your Strapi admin panel that trigger the lifecycle events you've set, such as creating, updating, and deleting your collections, you should see notifications show up in Postman. This will enable you to verify that your Strapi backend emits events and that your WebSocket connection functions as expected.
We're done with part one of this blog series. Stay tuned for Part 2, where we'll continue this tutorial by building the frontend with Flutter and consuming the APIs to implement a functional YouTube clone application. The code for this Strapi backend is available on my Github repository. We've split the tutorial into three parts, each in its own branch for easier navigation. The main branch contains the Strapi code. The part_2 branch holds the Flutter code for state management and app services, but if you run it, you'll only see the default Flutter app since these logics are connected to the UI in part_3. The part_3 branch contains the full Flutter code with both the UI and logic integrated.
In part one of this tutorial series, we learned how to set up the Strapi backend with collections, create data relationships, create custom endpoints for liking, commenting, and viewing videos, set up Socket.io, and create lifecycle methods to listen to real-time updates on the collections.
In the next part, we will learn how to build the frontend with Flutter.
I am Software Engineer and Technical Writer. Proficient Server-side scripting and Database setup. Agile knowledge of Python, NodeJS, ReactJS, and PHP. When am not coding, I share my knowledge and experience with the other developers through technical articles