How to Set Up a Scalable Likes System?

Is this even possible with bubble? Like for example, I can use Algeria to search for & store the full number of users who liked a post, and only show this data when I need itt, however, I’m not sure how to display individually, that a user has already liked a post.

I feel like having a list of likes on a user’s profile directly would be bad design.

Using a User-Container (satellite data type) and storing the likes in there would be slightly better except for the fact that I’d be constantly referencing the data type in order to conditionally show that a user has already liked a post via a red heart icon.

saving a list of users to the post itself, as “likes” and then grabbing the user from that list is what I do right now but its not scalable. What happens when I have a list of 1000+ likes? For every single post. bubble would be downloading a list of users who have liked the post.

Conversely, Instagram only downloads that data when you specifically ask to look at all of the likes a post has gotten. The minute you leave that page, they get rid of things.

This is the only scalable way because you can rely on the :count operator to not needlessly download more data

You can still stick a number of likes field on each post so you can sort by # of likes

2 Likes

I would go with a separate Like data type with 2 attributes: User and Post (or whatever users are going to see and giving likes for)
So for showing the number of likes under a post you’ll just need to count the number of Like items where Post is the post displayed.
Need to show “red heart” if current user liked some post? Check if “user - post” entry exists in Like table.
Need to show all users that liked the post? Extract the list of users from Like table where post is displayed post.

P.S. That’s what Tyler described in details above while I was typing :grinning:

2 Likes

You need to have a separate datatype for the likes, which links a User to the thing being liked.

Same answer as those that came before me with info on how to implement a like count on the Post:

Like data type.

Database trigger when Like is created: Make changes to Like’s Post (likes = This Post’s likes + 1).

Database trigger when Like is deleted: Make changes to Like’s Post (likes = This Post’s likes - 1)

To set the state of a like icon, do what Tyler said where it’s Do a search for Likes:count >= 1.

2 Likes

And to expand on this if you’re doing karma like from Reddit, you can have upvotes have a number field value 1, and downvotes -1, so you can search for all votes for a post and sum the values to get the karma

This is exactly what i do, but i have a privacy rule so a user can only see their own likes. Much smaller dataset to search through each time!

1 Like

I do this as well as the backend avoids race conditions. But I also use a state to set the heart, so the instant the user clicks it, it changes (avoiding the delay). I only allow the element to click if user is logged in.

1 Like

how would you visually show that a post has already been liked though? Like a current post?

Like within my repeating group of posts i’m showing the user, how can I show that an individual post has already been liked?

Would I try to reference the all_likes RG and then filter it? I mean even having an all_likes RG in the first place feels unscalable if you’re returning a lot of results. Maybe it’s all in my head

could u explain this again? this is what i want to do but I’m not exactly following the logic

also when you mention table…do you mean the new bubble element? I haven’t used tables yet and honestly still don’t know when // how to evaluate their use case

So in conditional tab of the icon you need to add expression like Do a search for Likes:count > 0. Inside search expression use constraints user = Current user and post = current cell's post. So if :count > 0 - that means Current user liked the post.

Nah, by table I mean tables where you can check your app data :slight_smile:

3 Likes

In my implementation, i have my reusable header searching for Likes (and again, with privacy rules, it’ll only be current user’s Likes). So even within a repeating group, my search is if current cell’s thing is in Headers Likes and Like is Yes, Set State to Yes. My condition on that heart is if custom state Like = Yes, this element is Red.

Now even if that element was already liked, or liked by that user in another tab or another system, it’ll be live/ update across all instances (and upon page load).

1 Like