Static/default matches

Hi,
How can I let the server automatically create several authoritative matches when it initiates?

Thank you,

Hello @mautoan11, you can use the CreateMatch function within InitModule to create matches when the server starts. See Server authoritative multiplayer - Heroic Labs Documentation.

Hope this helps.

Hi @sesposito, thank you for your reply.
I tried to create a default match by:
_, err := nk.MatchCreate(ctx, modulename, params)

However, I got the error:
error creating match: not found

I think you haven’t registered your match handlers. Take a look here: GitHub - heroiclabs/nakama-project-template: An example project on how to set up and write custom server code in Nakama server. for a complete example.

Hello, I am looking for the same solution, I am developing a server module with Typescript, I am sure that I’ve registered the match and tried to create default matches in “InitModule” function with correct module name, but it won’t work, the error says “error creating match: not found”, so I try to create it later by set timeout but “setTimeout is not defined”. Don’t know what I can do to create default matches properly?

Hello @insthync, could you please share a code snippet on how you’re registering and creating the match?

const InitModule: nkruntime.InitModule = (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, initializer: nkruntime.Initializer) =>
{
    initializer.registerRpc("getChannels", getChannels);
    initializer.registerMatch("default", {
        matchInit,
        matchJoinAttempt,
        matchJoin,
        matchLeave,
        matchLoop,
        matchTerminate,
        matchSignal,
    });
    // TODO: Read scene/channel list, then create match (the default one) following the list
    nk.matchCreate("default");
}

I am going to remake the server modules with Golang because the Typescript can’t store global variables, so I can’t store match Id to a global list variables.

same issue with automatically create authoritative matches when server initiates

You should avoid global state as it can create inconsistent states between the JS VMs. Although the same problem won’t apply to Go, global state is a bad practice and should be avoided. Why do you need to store the match Ids in a global when there’s a match listing function? Nakama: Query Syntax | Heroic Labs Documentation

This use case should be supported, I’ve created an issue for it: [BUG] JS registered match handlers not available within InitModule fn · Issue #849 · heroiclabs/nakama · GitHub.

1 Like

I think it is a bit more convenient for me to store match ID to the list, but never mind, I can use Query Syntax.

1 Like

Just FYI [BUG] JS registered match handlers not available within InitModule fn · Issue #849 · heroiclabs/nakama · GitHub has been resolved and will be part of the next release.

2 Likes

Hi,

I’m encountering a similar issue. When I add the following code to the InitModule, the server halts during the Go runtime initialization:

func InitModule(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, initializer runtime.Initializer) error {
	
	// Register match handler
	if err := initializer.RegisterMatch("default", func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule) (runtime.Match, error) {
		return &Match{}, nil
	}); err != nil {
		logger.Error("Unable to register match: %v", err)
		return err
	}


	//  Create default match
	_, err := nk.MatchCreate(ctx, "default", nil)
	if err != nil {
		logger.Error("Error creating official match: %v", err)
		return err
	}

	return nil
}

Here is the server log:

+ /nakama/nakama migrate up --database.address root@cockroachdb:26257
{"level":"info","ts":"2024-10-23T15:57:42.614Z","caller":"server/db.go:140","msg":"Database information","version":"CockroachDB CCL v23.1.27 (x86_64-pc-linux-gnu, built 2024/10/02 05:11:25, go1.19.13)"}
{"level":"info","ts":"2024-10-23T15:57:42.615Z","caller":"migrate/migrate.go:109","msg":"Applying database migrations","limit":-1}
{"level":"info","ts":"2024-10-23T15:57:42.631Z","caller":"migrate/migrate.go:116","msg":"Successfully applied migration","count":0}
+ exec /nakama/nakama --name nakama1 --database.address root@cockroachdb:26257 --logger.level DEBUG --session.token_expiry_sec 7200 --metrics.prometheus_port 9100
{"level":"warn","ts":"2024-10-23T15:57:42.680Z","caller":"server/config.go:323","msg":"WARNING: insecure default parameter value, change this for production!","param":"console.username"}
{"level":"warn","ts":"2024-10-23T15:57:42.681Z","caller":"server/config.go:327","msg":"WARNING: insecure default parameter value, change this for production!","param":"console.password"}
{"level":"warn","ts":"2024-10-23T15:57:42.681Z","caller":"server/config.go:331","msg":"WARNING: insecure default parameter value, change this for production!","param":"console.signing_key"}
{"level":"warn","ts":"2024-10-23T15:57:42.681Z","caller":"server/config.go:335","msg":"WARNING: insecure default parameter value, change this for production!","param":"socket.server_key"}
{"level":"warn","ts":"2024-10-23T15:57:42.681Z","caller":"server/config.go:339","msg":"WARNING: insecure default parameter value, change this for production!","param":"session.encryption_key"}
{"level":"warn","ts":"2024-10-23T15:57:42.681Z","caller":"server/config.go:343","msg":"WARNING: insecure default parameter value, change this for production!","param":"session.refresh_encryption_key"}
{"level":"warn","ts":"2024-10-23T15:57:42.681Z","caller":"server/config.go:347","msg":"WARNING: insecure default parameter value, change this for production!","param":"runtime.http_key"}
{"level":"info","ts":"2024-10-23T15:57:42.681Z","caller":"main.go:136","msg":"Nakama starting"}
{"level":"info","ts":"2024-10-23T15:57:42.681Z","caller":"main.go:137","msg":"Node","name":"nakama1","version":"3.23.0+1811efb0","runtime":"go1.22.5","cpu":4,"proc":4}
{"level":"info","ts":"2024-10-23T15:57:42.681Z","caller":"main.go:138","msg":"Data directory","path":"/nakama/data"}
{"level":"info","ts":"2024-10-23T15:57:42.681Z","caller":"main.go:149","msg":"Database connections","dsns":["root@cockroachdb:26257"]}
{"level":"debug","ts":"2024-10-23T15:57:42.683Z","caller":"server/db.go:116","msg":"Complete database connection URL","raw_url":"postgres://root@cockroachdb:26257/nakama?sslmode=prefer"}
{"level":"info","ts":"2024-10-23T15:57:42.694Z","caller":"server/db.go:140","msg":"Database information","version":"CockroachDB CCL v23.1.27 (x86_64-pc-linux-gnu, built 2024/10/02 05:11:25, go1.19.13)"}
{"level":"info","ts":"2024-10-23T15:57:42.713Z","caller":"server/metrics.go:178","msg":"Starting Prometheus server for metrics requests","port":9100}
{"level":"info","ts":"2024-10-23T15:57:42.732Z","caller":"server/leaderboard_rank_cache.go:140","msg":"Initializing leaderboard rank cache"}
{"level":"info","ts":"2024-10-23T15:57:42.732Z","caller":"server/leaderboard_rank_cache.go:201","msg":"Leaderboard rank cache initialization completed successfully","cached":[],"skipped":[],"duration":"235.189µs"}
{"level":"info","ts":"2024-10-23T15:57:42.734Z","caller":"server/runtime.go:646","msg":"Initialising runtime","path":"/nakama/data/modules"}
{"level":"info","ts":"2024-10-23T15:57:42.734Z","caller":"server/runtime.go:653","msg":"Initialising runtime event queue processor"}
{"level":"info","ts":"2024-10-23T15:57:42.734Z","caller":"server/runtime.go:655","msg":"Runtime event queue processor started","size":65536,"workers":8}
{"level":"info","ts":"2024-10-23T15:57:42.735Z","caller":"server/runtime_go.go:2794","msg":"Initialising Go runtime provider","path":"/nakama/data/modules"}
{"level":"error","ts":"2024-10-23T15:57:42.761Z","caller":"zarbe-runtime/main.go:52","msg":"Error creating official match: error creating match: not found","runtime":"go","source":"zarr.be/zarbe-runtime/main.go:52"}
{"level":"fatal","ts":"2024-10-23T15:57:42.761Z","caller":"server/runtime_go.go:2812","msg":"Error returned by InitModule function in Go module","name":"backend","error":"error creating match: not found"}

Hello @lucille, what version of Nakama are you observing this on? Have you tried upgrading to latest?

1 Like

Hello @sesposito,

I’m currently using Nakama Docker 3.23.0, and after upgrading to the latest version 3.24.2, I’m still encountering the same issue. The server halts after reaching the line Initialising Go runtime provider and then exits unexpectedly.

Here are the relevant server logs:

+ /nakama/nakama migrate up --database.address root@cockroachdb:26257
{"level":"info","ts":"2024-10-23T16:06:29.228Z","caller":"server/db.go:140","msg":"Database information","version":"CockroachDB CCL v23.1.27 (x86_64-pc-linux-gnu, built 2024/10/02 05:11:25, go1.19.13)"}
{"level":"info","ts":"2024-10-23T16:06:29.229Z","caller":"migrate/migrate.go:109","msg":"Applying database migrations","limit":-1}
{"level":"info","ts":"2024-10-23T16:06:29.251Z","caller":"migrate/migrate.go:116","msg":"Successfully applied migration","count":0}
+ exec /nakama/nakama --name nakama1 --database.address root@cockroachdb:26257 --logger.level DEBUG --session.token_expiry_sec 7200 --metrics.prometheus_port 9100
{"level":"warn","ts":"2024-10-23T16:06:29.302Z","caller":"server/config.go:323","msg":"WARNING: insecure default parameter value, change this for production!","param":"console.username"}
{"level":"warn","ts":"2024-10-23T16:06:29.303Z","caller":"server/config.go:327","msg":"WARNING: insecure default parameter value, change this for production!","param":"console.password"}
{"level":"warn","ts":"2024-10-23T16:06:29.303Z","caller":"server/config.go:331","msg":"WARNING: insecure default parameter value, change this for production!","param":"console.signing_key"}
{"level":"warn","ts":"2024-10-23T16:06:29.303Z","caller":"server/config.go:335","msg":"WARNING: insecure default parameter value, change this for production!","param":"socket.server_key"}
{"level":"warn","ts":"2024-10-23T16:06:29.303Z","caller":"server/config.go:339","msg":"WARNING: insecure default parameter value, change this for production!","param":"session.encryption_key"}
{"level":"warn","ts":"2024-10-23T16:06:29.303Z","caller":"server/config.go:343","msg":"WARNING: insecure default parameter value, change this for production!","param":"session.refresh_encryption_key"}
{"level":"warn","ts":"2024-10-23T16:06:29.304Z","caller":"server/config.go:347","msg":"WARNING: insecure default parameter value, change this for production!","param":"runtime.http_key"}
{"level":"info","ts":"2024-10-23T16:06:29.304Z","caller":"main.go:136","msg":"Nakama starting"}
{"level":"info","ts":"2024-10-23T16:06:29.304Z","caller":"main.go:137","msg":"Node","name":"nakama1","version":"3.23.0+1811efb0","runtime":"go1.22.5","cpu":4,"proc":4}
{"level":"info","ts":"2024-10-23T16:06:29.304Z","caller":"main.go:138","msg":"Data directory","path":"/nakama/data"}
{"level":"info","ts":"2024-10-23T16:06:29.304Z","caller":"main.go:149","msg":"Database connections","dsns":["root@cockroachdb:26257"]}
{"level":"debug","ts":"2024-10-23T16:06:29.306Z","caller":"server/db.go:116","msg":"Complete database connection URL","raw_url":"postgres://root@cockroachdb:26257/nakama?sslmode=prefer"}
{"level":"info","ts":"2024-10-23T16:06:29.318Z","caller":"server/db.go:140","msg":"Database information","version":"CockroachDB CCL v23.1.27 (x86_64-pc-linux-gnu, built 2024/10/02 05:11:25, go1.19.13)"}
{"level":"info","ts":"2024-10-23T16:06:29.332Z","caller":"server/metrics.go:178","msg":"Starting Prometheus server for metrics requests","port":9100}
{"level":"info","ts":"2024-10-23T16:06:29.346Z","caller":"server/leaderboard_rank_cache.go:140","msg":"Initializing leaderboard rank cache"}
{"level":"info","ts":"2024-10-23T16:06:29.347Z","caller":"server/leaderboard_rank_cache.go:201","msg":"Leaderboard rank cache initialization completed successfully","cached":[],"skipped":[],"duration":"242.741µs"}
{"level":"info","ts":"2024-10-23T16:06:29.349Z","caller":"server/runtime.go:646","msg":"Initialising runtime","path":"/nakama/data/modules"}
{"level":"info","ts":"2024-10-23T16:06:29.350Z","caller":"server/runtime.go:653","msg":"Initialising runtime event queue processor"}
{"level":"info","ts":"2024-10-23T16:06:29.352Z","caller":"server/runtime.go:655","msg":"Runtime event queue processor started","size":65536,"workers":8}
{"level":"info","ts":"2024-10-23T16:06:29.352Z","caller":"server/runtime_go.go:2794","msg":"Initialising Go runtime provider","path":"/nakama/data/modules"}

Thank you!

@lucille are you using Docker?

Are you certain that your code changes are being loaded by the server correctly? I couldn’t reproduce this issue, and your latest logs show no error. The previous ones clearly state that there are no match handlers registered with name default. Where is the definition of Match?

Yes, I’m using Docker.

Apologies for any confusion. In my original post, I shared a simplified version of my code, which overlooked an important detail regarding data types. The issue was indeed related to the typing in MatchState.

Here’s the structure I was using:

type MatchState struct {
	Duration int64 `json:"duration"`
}

Initially, I attempted to pass parameters in this way:

params := map[string]interface{}{
	"duration": 0,
}

However, this caused the server to halt and exit unexpectedly. It turns out that the type mismatch was the root of the issue. To resolve it, I specified the type explicitly:

params := map[string]interface{}{
	"duration": int64(0),
}

Since there were no error messages or direct indicators, diagnosing the problem was challenging. Explicitly setting the type resolved the issue, and the server is now stable.

Regards.

1 Like