matchJoin / dispatcher.broadcastMessage order for a lobby proof of concept

I have the following ‘matchJoin’ in my matchhandler which seems to be working but I have a question about the order of events and how to handle the ‘users’ in a lobby and sync the ‘ready state’ of those users.

const matchJoin = function (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, presences: nkruntime.Presence[]) : { state: nkruntime.MatchState } | null {
    presences.forEach(function (p) { 
      state.presences[p.sessionId] = p;
    for (const userId in state.readyStates) {
      if (state.readyStates.hasOwnProperty(userId)) {
        const isReady = state.readyStates[userId];        
        var opCodeToUse = isReady ? MatchOpCode.ReadyStateUpdatedYes : MatchOpCode.ReadyStateUpdatedNo
        dispatcher.broadcastMessage(opCodeToUse, JSON.stringify({ userId: userId }), presences);

    return {

I add the players currently in the match to a list in unity with the information I get returned from the matchJoin call. The issue is inside this call on the server side of things I broadcast the state of all the connected players to set the ‘initial ready state’ for a newly connected player.

The issue is that the order of events is a bit random / sometimes I have the player ID already added to the list and it works and sometimes the player isn’t in the list yet and reacting on the ReceiveMatchState function is giving an error because the ‘player object’ is not yet initialized and in the list so It can’t be updated.

I think the same problem should be in the ‘Heroic Labs Documentation | Creating a Lobby System’ lobby example in the documentation.

I might be mis understanding things for sure! but IF sending matchstate message from within the matchJoin handler wouldn’t it always be a problem that the result of the matchJoin call isn’t on the client yet before the match state reaches the client?