Skip to main content

Working with protobuf event descriptor

Last updated on June 20, 2024
info

Extend is in Open Beta for AGS Premium Clients! This means that the Extend add-on is available for you to try in your development environment. You can submit your feedback via our Extend Open Beta feedback form.

Overview

This guide provides information on how to modify the Extend Event Handler sample app to listen and handle different events from AccelByte Gaming Services (AGS).

Prerequisites

Clone an Extend Event Handler sample app.

git clone https://github.com/AccelByte/extend-event-handler-go.git

Identify and download specific event descriptors

Here is how you can get the protobuf event descriptor for the AGS event you are looking for.

  • Identify your AGS event: Go to the API Events page and find the AGS event you are looking for.

  • Locate the Protobuf event descriptor: After finding the AGS event, get the URL to the protobuf (*.proto) file on the same page.

  • Download the protobuf event descriptor: As an example for this guide, we need the userLoggedIn event. Therefore, we need to download the IAM directory which contains the userLoggedIn event.

Generate stub from event descriptor

Put the iam directory you have downloaded in pkg/proto/accelbyte-asyncapi directory in your Extend Event Handler project.

...
|__ main.go
|__ pkg
...
├── proto
│ └── accelbyte-asyncapi
│ └── iam # Directory structure containing protobuf files
├── pb
...
...

Next, in the top-level directory of your Extend Event Handler project, run the following command.

make proto

You will see your stub generated at pkg/pb/accelbyte-asyncapi.

...
|__ main.go
|__ pkg
...
├── proto
│ └── accelbyte-asyncapi
│ └── iam # Directory containing protobuf files
├── pb
│ └── accelbyte-asyncapi
│ └── iam # Directory containing "stub" generated code
...
...

Creating your callback implementation

  • Create a New Go File: Go to directory pkg/service and create a new Go file. For example loginHandler.go.

  • Embed the Generated Stub: In the new Go file, define a struct type that embeds the stub type. For example, if your event is userLoggedIn you will see UnimplementedUserAuthenticationUserLoggedInServiceServer stub and your struct declaration might look something like the following.

    import pb "extend-event-handler/pkg/pb/accelbyte-asyncapi/iam/account/v1"

    type LoginHandler struct {
    pb.UnimplementedUserAuthenticationUserLoggedInServiceServer
    // your fields here
    }
  • Define OnMessage Methods: Define a method on your new struct named OnMessage. This method should have the same signature as the OnMessage method in the stub, but with your own implementation. This is the method that will be invoked when the AGS event is received.

    import pb "extend-event-handler/pkg/pb/accelbyte-asyncapi/iam/account/v1"

    func (o *LoginHandler) OnMessage(ctxt context.Context, msg *pb.UserLoggedIn) (*emptypb.Empty, error) {

    // your event handling code here

    return &emptypb.Empty{}, nil
    }

Registering the event handler struct into the service

Go to main.go in the Event Handler project and register the gRPC service we have created as follows.

import "extend-event-handler/pkg/service"

// gRPC server that you already initialized in the service
s := grpc.NewServer(...)

// Register IAM Handler
loginHandler := service.LoginHandler{} // or service.NewLoginHandler() if you created the constructor method
pb.RegisterUserAuthenticationUserLoggedInServiceServer(s, loginHandler)