Short-Video App with React Native and Supabase · Course book
Chapter 14: Follows and Friends
Following is what turns a collection of videos into a social network. When you follow someone, their videos appear in your feed; when two people follow each other, they become friends who can chat. This chapter builds the follow system for the short-video app: how a directed follow is stored, following and unfollowing with the right safeguards, counting followers and following, finding mutual followers to define friendships with simple set operations worked out by hand, and filtering the feed to show videos from the people a user follows. Along the way it covers the database queries that keep these features fast as the network grows.
Storing a follow
A follow is a directed relationship: if Ana follows Ben, that says nothing about whether Ben follows Ana. The Follower table stores one row per follow, with two user ids: the person being followed, user_id, and the person doing the following, follower_user_id, plus the time it happened. Thinking of users as points and follows as arrows between them, the whole network is a directed graph, and every feature in this chapter is a question about that graph.
| Question | Query on the Follower table |
|---|---|
| Who follows me? (my followers) | Rows where user_id = me; read follower_user_id |
| Whom do I follow? (my following) | Rows where follower_user_id = me; read user_id |
| Do I follow this person? | A row where user_id = them and follower_user_id = me exists |
| Are we friends? | Both that row and its reverse exist |
| Whose videos go in my following feed? | Videos whose user_id is in my following list |
The same safeguards used for likes apply. A unique constraint on the pair of ids stops duplicate follows, a check can stop users following themselves, and row level security allows a user to insert or delete only rows in which they are the follower. Indexes on both columns keep the two most common questions, "my followers" and "my following", fast however large the table grows.
Following and unfollowing
The follow button on a profile, and sometimes on the video overlay, is a toggle much like the like button. It shows "Follow" when the viewer does not follow the profile's owner, and "Following" or "Unfollow" when they do. Tapping inserts or deletes the corresponding row, and the button and counts update. The same optimistic approach used for likes works well: change the button immediately, send the request, and roll back if it fails.
The current user's following list is needed in many places, on every profile and in the feed, so, like the liked-video ids, it is loaded once and kept in a shared store as a set of user ids. Checking whether to show "Follow" or "Following" on any profile is then a local lookup. Updating the set on each follow and unfollow keeps every screen consistent.
Unfollowing deserves a moment of care in the interface. A quick tap that unfollows someone by accident is irritating, so many apps ask for confirmation, or show the change with an undo option for a few seconds. Either choice is reasonable; what matters is that the action is clear and reversible. Following someone from the video overlay, where a small plus badge on the creator's avatar is a common design, should give the same instant feedback as the profile button, typically turning the badge into a tick for a moment before it disappears.
Counting followers and following
Each profile shows two numbers: followers, how many people follow this user, and following, how many people this user follows. Both are counts on the Follower table with one filter each, and Supabase can return the count alone, without downloading the rows. On popular profiles the counts can be large, so they are displayed in compact form, such as 12.4K, using the same formatting as like counts.
Counts shown immediately after following or unfollowing should change by exactly one. If the screen refetches the count from the server, the new value arrives a moment later; if it updates optimistically, it adds or subtracts one locally first. Mixing the two carelessly can cause a count to jump by two, briefly, when the local change and the refetched value are both applied, a classic small bug worth testing for.
Tapping either number usually opens a list screen showing the people behind it, each row with a picture, a username and a follow button of its own. These lists can be long, so they load in pages, ordered by when each follow happened, newest first. The follow button on each row reads from the same shared following set as everywhere else, so following someone from a list instantly updates their profile, the feed and the counts.
Some apps add private accounts, where following requires approval. That changes the model slightly: a follow starts as a request with a status of pending, the account owner accepts or declines it, and only accepted follows count as followers or unlock the account's videos. Even if this app keeps every account public, it is worth knowing that the change is mainly a status column and some extra policy rules, not a redesign.
Mutual followers and friends
In this app a friend is a mutual follower: someone you follow who also follows you. Friends see a dedicated friends feed and are the only people you can chat with, which keeps private messaging to people who have chosen each other. Finding friends is an exercise in set intersection.
In code, the intersection can be computed on the phone from the two lists, by turning one into a set and keeping the members of the other that it contains, which is fast for lists of a few thousand. For larger networks, or to avoid downloading both lists, the database can compute it in one query by joining the Follower table to itself: rows where Ana follows someone, matched with rows where that same someone follows Ana. Wrapping that query in a database function lets the app call it like any other request.
Set operations power suggestions too. A simple "people you may know" list takes the people your friends follow, the union of their following sets, then removes yourself and everyone you already follow, a set difference. Counting how many of your friends follow each candidate gives a natural ranking: someone followed by four of your friends is a stronger suggestion than someone followed by one.
A feed from the people you follow
With the following list in hand, the feed can show only videos from followed users. The query filters videos with an in condition on the creator's id, such as .in("user_id", followingIds), ordered newest first, and a friends feed does the same with the friends list. For a user who follows nobody yet, the filtered feed would be empty, so a sensible fallback shows popular or recent videos from everyone, with suggestions of creators to follow.
Passing a long list of ids in a request has limits, because addresses and request sizes are bounded. For users who follow thousands of accounts, it is better to let the database do the filtering, with a query that joins videos to the Follower table where the follower is the current user. This keeps the request small and lets the database use its indexes, and it is the natural shape for the more advanced feed logic real apps use, such as mixing followed creators with recommendations.
Following also feeds other features. Each new follow is an event for the activity feed, "Eli started following you", which the next chapter builds, and the friends set determines who can be messaged in the chat chapter. Designing the follow system cleanly now makes both straightforward, and keeps later features from needing changes to the tables.
Practice tasks
- Ben follows {Ana, Chi, Eli, Fay} and is followed by {Ana, Dev, Fay}. List his friends, the people he follows who do not follow back, and those he could follow back.
- After Ben unfollows Fay, how do his following count, follower count and friends change?
- Write the two filters that count a user's followers and following on the Follower table.
- Explain why a follow is a directed relationship and a friendship is not.
- Describe the self-join that finds mutual followers in the database.
- What should the following feed show for a brand-new user who follows nobody, and why?
This course book is available to read online only at tech-academy.chrysolites.org.