logo
bg
return Blog

December 14, 2023

How to Implement a Like Feature in Just 10 Minutes

CorrectRoadCorrectRoad

Introduction

Like is a very common feature, but the implementation of the like function for QBS 1 and QBS 100000 is vastly different in difficulty. However, even the simplest like system might take about half an hour to develop from scratch. But there's an open-source and free Backend as a Service that can help you implement a like feature in your application in just 10 minutes.

Likit encapsulates different like implementations for different performance requirements, making it easy for developers to invoke through different methods like SDK, gRPC, and RESTful.

Let us help you implement a like feature in your application in less than 10 minutes through a journal.

Deploy Likit

You can deploy likit on Zeabur by this template.

Binding Domain

Likit needs to expose two ports to the outside. In Zeabur, this can be achieved by binding a domain.The first port handles HTTP requests. Through it, you can access the dashboard to manage Likit and also use RESTful to call APIs.The second port handles gRPC requests. In the SDKs for Golang and Java, please enter the domain name bound to this port.

Configure Likit

We click on the previously bound HTTP domain to enter Likit's dashboard.

You should copy the password for the Likit Dashboard from the left side of the Zeabur control panel. Afterward, click on the bound domain to access Likit, paste the password, and then enter the Dashboard.

You should copy the password of the Likit Dashboard from the left side of the Zeabur control panel, then click on the previously bound domain to enter Likit, paste the password, and then enter the Dashboard.

Here, we briefly explain three concepts that will often be encountered later: BusinessId, MessageId, and UserId.

  • BusinessId, refers to a specific business, such as comment likes or post votes. It is determined by the user when manually creating in Likit Dashboard. However, we recommend using all uppercase and connecting words with underscores, like COMMENT_LIKE.
  • MessageId, is the ID of the object being liked; it should be unique in each business.
  • UserId, is the unique ID of the user who likes.
  • Vote Engine, Each Business must specify a Vote Engine when created. Different Vote Engines have different algorithms and implementations, and require different components to meet the needs of different users. For example, later we will specify a Simple Vote Vote Engine, which is suitable for small applications and only needs a Redis.
  • Alright, let's create a new Business here. Fill in the Title and Id, choose a Simple Vote, and remember to select Redis as well.

Implement Like in your backend

Up to now, we have already created the Business, so everything related to Likit has been configured. Now, we can start calling the API in our backend code.

Java/Spring

If your backend is written in Java with Spring, then all you need to use is this library.

Golang

If your backend is written in Golang, then github.com/correctroadh/likit-go is all you need.

  • Install likit-go: `go get github.com/likit/likit-go`
  • Add likit to your code
import (
	"github.com/CorrectRoadH/likit-go"
)

type VideoServer struct {
	Store      *store.VideoStore
	likeServer *likit.LikitServer
}

func NewVideoServer(store *store.VideoStore) *VideoServer {
	return &VideoServer{
		Store:      store,
        // the likit server address. You should replace it with your own server address
        // NewLikitServer(host string, tls bool)
		likeServer: likit.NewLikitServer("likit-grpc.zeabur.app:443",true), 
		// if you deploy likit server in zeabur. tls should be true
	}
}

type Video struct {
    Id      string `json:"id"`
    LikeNum int    `json:"likeNum"`
    IsLike  bool   `json:"isLike"`
}

const businessId = "VIDEO_LIKE"

func (s *VideoServer) ListVideo(c echo.Context) error {
    userId := /// get user id from jwt or cookie

    find := // get find condition from request
	videos, err := s.Store.ListVideo(ctx, find)

	return c.JSON(http.StatusOK, lo.Map(videos, func(video domain.Video, index int) Video {
        isLike, err := s.likeServer.IsVote(ctx, businessId, video.Id, video.UserId)
        if err != nil {
            // handle error
        }

        likeNum, err := s.likeServer.Count(ctx, businessId, video.Id)
        if err != nil {
            // handle err
        }

        // just for example
        return Video{
            Id:      video.Id,
            LikeNum: likeNum,
            IsLike:  isLike,
        }
    }))
}

func (s *VideoServer) Like(c echo.Context) error {
    userId := // get user id from jwt or cookie
	videoId := // get video id from request

	count, err := s.likeServer.Vote(ctx, businessId, videoId, UserId)
	if err != nil {
		// handle error
	}
	return c.JSON(http.StatusOK, count)
}

func (s *VideoServer) Unlike(c echo.Context) error {
    userId := // get user id from jwt or cookie
	videoId := // get video id from request

	count, err := s.likeServer.UnVote(ctx, businessId, videoId, UserId)
	if err != nil {
		// handle error
	}

	return c.JSON(http.StatusOK, count)
}

Other Langage

If your backend is implemented neither in Golang nor Java, then I recommend using gRPC to call the Likit API. You can see the complete API definition of Likit and some gRPC SDKs for other languages here.

Conclusion

Likit is a very simple and easy-to-use Backend as a Service. If you want to implement a like, dislike, or voting feature in your new project, it's worth a try.