nk.matchList return empty

Hello,

I’m developing my Nakama server using TS runtime.
I have a rpc match.create for creating a match, a rpc match.search for searching matches.
Originally my match.search is complex with query by match labels inside, I use nk.matchList for searching but the results is empty anyway.
So I simplify nk.matchList to use default filters, but the results is still empty. I could not figure out what’s wrong, please help.

What I expect is I firstly create a match, then using nk.matchList to search, the results should have at least 1 item - the match that I just created.
I confirm that the created match is exist, by using nk.matchGet with the matchId from creating the match. But nk.matchList returns .

Here is my snippet:


// match.rpc.ts
export function rpcCreateMatch(
  context: nkruntime.Context,
  logger: nkruntime.Logger,
  nk: nkruntime.Nakama,
  payload: string
): string {
const matchId = nk.matchCreate("starter");  // I have a match handler named "starter" match for handling this
return JSON.stringify({matchId: matchId});
}

export function rpcSearchMatch(context: nkruntime.Context,
  logger: nkruntime.Logger,
  nk: nkruntime.Nakama,
  payload: string) {
const results = nk.matchList(10);  // Limit 10, remaining filters default
return JSON.stringify({data: results});
}

// main.ts
initializer.registerRpc("match.create", rpcCreateMatch);
initializer.registerRpc("match.search", rpcSearchMatch);

// Here is my test using Jest, nakama js SDK
// test.spec.ts

const response1 = await user1.socket.rpc("match.create");
expect(JSON.parse(response1.payload!).matchId).toBeTruthy();

await sleep(1000);

const response2 = await user1.socket.rpc("match.search");
expect(JSON.parse(response2.payload!).results.length).toBeGreaterThan(0);  // Test fail, the results length is 0

Hey @nghiaht if you look at the developer console, do you see the match running? Is it possible your user is leaving the match before your match.search RPC finishes?

The match is still running.
My test is user1 create the match, then user2 call match.search (using nk.matchlist to find that old one) but the array is , you can see in the log (The line below - Try to find existing matches...)

{"level":"debug","ts":"2022-11-26T16:16:47.086+0700","caller":"server/session_ws.go:395","msg":"Sending *rtapi.Envelope_Rpc message","uid":"1a89360c-e7cd-4ae5-b495-1a5c7da4506e","sid":"0d464210-6d6b-11ed-b040-006100a0eb06","envelope":"cid:\"1\"  rpc:{id:\"match.create\"  payload:\"{\\\"data\\\":{\\\"matchId\\\":\\\"8c55e455-e49b-4ec1-aeb7-610f83a91228.nakama\\\"}}\"}"}
{"level":"debug","ts":"2022-11-26T16:16:47.087+0700","caller":"server/pipeline.go:65","msg":"Received *rtapi.Envelope_MatchJoin message","uid":"1a89360c-e7cd-4ae5-b495-1a5c7da4506e","sid":"0d464210-6d6b-11ed-b040-006100a0eb06","cid":"2","message":{"MatchJoin":{"Id":{"MatchId":"8c55e455-e49b-4ec1-aeb7-610f83a91228.nakama"}}}}
{"level":"debug","ts":"2022-11-26T16:16:47.088+0700","caller":"server/runtime_javascript_logger.go:104","msg":"\"1a89360c-e7cd-4ae5-b495-1a5c7da4506e\" attempted to join match, metadata: map[]","mid":"8c55e455-e49b-4ec1-aeb7-610f83a91228"}
{"level":"debug","ts":"2022-11-26T16:16:47.089+0700","caller":"server/runtime_javascript_logger.go:104","msg":"tlmn.handler - match join - instatiate TLMNDI","mid":"8c55e455-e49b-4ec1-aeb7-610f83a91228"}
{"level":"debug","ts":"2022-11-26T16:16:47.089+0700","caller":"server/tracker.go:907","msg":"Processing presence event","joins":1,"leaves":0}
{"level":"debug","ts":"2022-11-26T16:16:47.089+0700","caller":"server/session_ws.go:395","msg":"Sending *rtapi.Envelope_Match message","uid":"1a89360c-e7cd-4ae5-b495-1a5c7da4506e","sid":"0d464210-6d6b-11ed-b040-006100a0eb06","envelope":"cid:\"2\"  match:{match_id:\"8c55e455-e49b-4ec1-aeb7-610f83a91228.nakama\"  authoritative:true  label:{value:\"{\\\"id\\\":\\\"8c55e455-e49b-4ec1-aeb7-610f83a91228.nakama\\\",\\\"status\\\":\\\"initial\\\",\\\"players\\\":[],\\\"leaderUserId\\\":null,\\\"gameplay\\\":\\\"tlmn\\\",\\\"competitive\\\":\\\"normal\\\",\\\"mode\\\":\\\"normal\\\",\\\"betGold\\\":10000}\"}  self:{user_id:\"1a89360c-e7cd-4ae5-b495-1a5c7da4506e\"  session_id:\"0d464210-6d6b-11ed-b040-006100a0eb06\"  username:\"NR79LO\"}}"}
{"level":"debug","ts":"2022-11-26T16:16:47.089+0700","caller":"server/runtime_javascript_logger.go:104","msg":"\"1a89360c-e7cd-4ae5-b495-1a5c7da4506e\" request to join match","mid":"8c55e455-e49b-4ec1-aeb7-610f83a91228"}
{"level":"debug","ts":"2022-11-26T16:16:47.092+0700","caller":"server/pipeline.go:65","msg":"Received *rtapi.Envelope_Rpc message","uid":"f1d26365-fd35-48ae-b6c7-25082ebfe2ca","sid":"0d4cd0e4-6d6b-11ed-b040-006100a0eb06","cid":"1","message":{"Rpc":{"id":"match.create","payload":"{\"gameplay\":\"tlmn\",\"payload\":{\"maxPlayers\":2,\"gameplay\":\"tlmn\"},\"useExisting\":true}"}}}
{"level":"debug","ts":"2022-11-26T16:16:47.097+0700","caller":"server/runtime_javascript_logger.go:104","msg":"tlmn - handler - send state message: map[command:55 context:MatchPlayerJoin data:{\"competitive\":\"normal\",\"mode\":\"normal\",\"betGold\":10000,\"id\":\"8c55e455-e49b-4ec1-aeb7-610f83a91228.nakama\",\"status\":\"initial\",\"players\":[\"1a89360c-e7cd-4ae5-b495-1a5c7da4506e\"],\"leaderUserId\":\"1a89360c-e7cd-4ae5-b495-1a5c7da4506e\",\"gameplay\":\"tlmn\"} sender:SYS], receivers: <nil>","mid":"8c55e455-e49b-4ec1-aeb7-610f83a91228"}  
{"level":"debug","ts":"2022-11-26T16:16:47.099+0700","caller":"server/runtime_javascript_logger.go:104","msg":"@@@@@@@@@@@@@@ Try to find existing matches []","rpc_id":"match.create"}

Here is my recap implementation

export function createMatch(
  context: nkruntime.Context,
  logger: nkruntime.Logger,
  nk: nkruntime.Nakama,
  dto: CreateMatchInputDto
): CreateMatchResultDto {

if (dto.useExisting) {
  const existings = nk.matchList(10, true, null, 2, 4);
  logger.debug("@@@@@@@@@@@@@@ Try to find existing matches %v", existings);  // Here result is empty
  if (existings?.[0]) {
    return { matchId: existings[0].matchId}
  }
}

// Fallback to create new one
const matchId = nk.matchCreate(TLMN_MODULE_NAME, props);

return {
  matchId: matchId,
};

}

Here is my jest test pseudocode

const user1;
const user2;


 const response1 = await user1.socket.rpc(
        rpcCreateMatchId, // match.create
        JSON.stringify({useExisting: false})
 );

const data1 = JSON.parse(
        String(response1.payload)
      ) as AppResponseDto<CreateMatchResultDto>;
      expect(data1.data.matchId).toBeDefined();

await user1.socket.joinMatch(data1.data.matchId);


const response2 = await user2.socket.rpc(
        rpcCreateMatchId, // match.create
        JSON.stringify({ useExisting: true })
);

const data2 = JSON.parse(
        String(response2.payload)
      ) as AppResponseDto<CreateMatchResultDto>;

expect(data2.data.matchId).toBe(data1.data.matchId);  // Failed because another match is created
await user2.socket.joinMatch(data2.data.matchId);

did you solve this? facing similar issue

Yes, I do.

In my test case, I tested so fast while the default match label update interval is 1000ms ( Heroic Labs Documentation | Configuration).

So please wait after that interval or change it. It it’s still incorrect, I think you should check the match query itself.

1 Like