Hey Heroic,
We’re using a FindMatch RPC function to create matches based on a provided room identifier. Users with the same room identifier need to connect to the same match. The room identifier is set as a label to the match. This is working fine when users are joining slowly. Unfortunately when users are joining rapidly, multiple matches with the same room identifier are created. This results in users ending up in the wrong match.
We’ve debugged this issue and it seems that the matchList
function is not returning the newly created match. We first thought this was due to latency between Nakama and SQL, but I think both the matchList
and MatchCreate
functions are working with in-memory data.
How can we fix this issue?
Our RPC function:
- Find match with
nk.matchList
with the provided room identifier - If no matches are found, create a new match with
nk.matchCreate
- Return the match found or the created match
Nakama Version: 3.12.0
Runtime Language: TypeScript
if (!ctx.userId) {
throw Error('No user ID in context');
}
if (!payload) {
throw Error('Expects payload.');
}
let request = {} as IRpcFindMatchRequest;
try {
request = JSON.parse(payload);
} catch (error) {
logger.error('Error parsing json message: %q', error);
throw error;
}
let matches: nkruntime.Match[];
try {
const query = `+label.open:1 +label.roomId:${request.roomId}`;
matches = nk.matchList(1, true, null, null, 12, query);
} catch (error) {
logger.error('Error listing matches: %v', error);
throw error;
}
if (matches.length === 0) {
const matchId = nk.matchCreate(serverSettings.moduleName, { roomId: request.roomId });
const matchIds = [matchId];
const res: IRpcFindMatchResponse = { matchIds };
return JSON.stringify(res);
}
let matchIds: string[] = [];
matchIds = matches.map((m) => m.matchId);
const res: IRpcFindMatchResponse = { matchIds };
return JSON.stringify(res);