RegisterEventSessionStart not fired

Hi all,

Thank you for amazing project.
I have a simple *.go script for registering event on session start. Event itself bans specific user:

package main

import 
(
	"context"
	"database/sql"
	"github.com/heroiclabs/nakama-common/api"
	"github.com/heroiclabs/nakama-common/runtime"
)

var nkm runtime.NakamaModule

func eventSessionStart(ctx context.Context, logger runtime.Logger, evt *api.Event) {
	logger.Debug("process eventSessionStart: %+v", evt)	
	if err := nkm.UsersBanId(ctx, []string { "963e11d1-c2cf-44c6-b8c7-c03af68026a4" }); err != nil {
		logger.Error("Ban failed: %s", err.Error())
	}
}

func InitModule(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, initializer runtime.Initializer) error {
	nkm = nk
	if err := initializer.RegisterEventSessionStart(eventSessionStart); err != nil {
		logger.Error("on_session_start event not registered")
		return err
	}
	return nil
}

Then I’m creating user session from unity:

private void Awake() 
{
    client = new Client("http", ip, 7350, "defaultkey");
}

// Start is called before the first frame update
async void Start()
{
    var deviceid = SystemInfo.deviceUniqueIdentifier;
    session = await client.AuthenticateDeviceAsync(deviceid);
    Debug.Log("" + session);
}

In server console and logs I can see that event successfully registered:

{“level”:“info”,“ts”:“2020-06-06T01:21:56.987+0200”,“caller”:“server/runtime.go:487”,“msg”:“Found runtime modules”,“count”:1,“modules”:[“on_session_start.so”]}
{“level”:“info”,“ts”:“2020-06-06T01:21:56.987+0200”,“caller”:“server/runtime.go:493”,“msg”:“Registered event function invocation”,“id”:“session_start”}

I expected that after auth the user will be banned, but nothing happened. Also, I don’t see any debug messages from *.go in logs or console (minimal logger.level: “debug” in *.yaml used) and no active sessions in Nakama’s web-browser developer console (Status -> Sessions column).

  1. Why user not banned?
  2. How to make visible script logs in console/logs?

Hi @skroopa welcome :wave:

Your code looks good. I think where the confusion comes in is that SessionStart event is only fired when a socket connection is opened between client and server. You can see more about the event in the docs:

https://heroiclabs.com/docs/advanced-events/#builtin-events_1

For your requirements to ban specific user IDs you should attach to one or more of the request lifecycle functions. Use a before hook function on the request pipeline:

https://heroiclabs.com/docs/runtime-code-basics/#before-hook

Hope this helps.

Hello @novabyte,

Works pretty cool, thanks!

As @novabyte mentioned you should create socket after auth:

async void Start()
{
    string email = "emai@exampl.cm";
    const string password = "3bc8f72e95a9";
    session = await client.AuthenticateEmailAsync(email, password);
    Debug.Log("" + session);

    socket = client.NewSocket();
    await socket.ConnectAsync(session);
    Debug.Log("Socket connected.");
}

Then session become visible in Nakama’s web-browser developer console and eventSessionStart fired.

offtopic: is it possible to catch stage before creating socket connection? I mean, does Nakama provide something built in for that?

offtopic: is it possible to catch stage before creating socket connection? I mean, does Nakama provide something built in for that?

@skroopa I answered that in my original reply. I’ll quote myself again and suggest you read the docs.

For your requirements to ban specific user IDs you should attach to one or more of the request lifecycle functions. Use a before hook function on the request pipeline:

https://heroiclabs.com/docs/runtime-code-basics/#before-hook

The exact conditions for when a user is banned and when that ban is applied is different across most games so we don’t provide the behaviour for it out of the box but we do provide the APIs like "UsersBanId" which you’ve found.

@novabyte
that what “before hook” mean, I see.
Thanks!

1 Like