# Sending Initial Game State in Match Join

**URL:** <https://forum.heroiclabs.com/t/sending-initial-game-state-in-match-join/2340>\
**Category:** Runtime Framework\
**Tags:** server-framework, lua\
**Created:** [February 4, 2022, 9:02am UTC](https://forum.heroiclabs.com/t/sending-initial-game-state-in-match-join/2340 "2022-02-04T09:02:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattkanwisher](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/mattkanwisher/32/976_2.png) [@mattkanwisher](https://forum.heroiclabs.com/u/mattkanwisher)\
**Post date:** [February 4, 2022, 9:02am UTC](https://forum.heroiclabs.com/t/sending-initial-game-state-in-match-join/2340/1 "2022-02-04T09:02:07Z")

</div>

When users join a match, I want the game state to be broadcasted to them so they can see where all the other players are.

We tried doing dispatcher.broadcast\_message in the match\_join event, but it seems like I can only broadcast to the existing users. The user currently joining I can’t send a message to ?

Is there a common pattern for client getting initial game state on joining match?

1. Nakama version 3.10.0 on linux
2. Lua Server Module

```auto
function M.match_join(context, dispatcher, tick, state, presences)
    for _, presence in ipairs(presences) do
        nk.logger_info(string.format("Presence %s named %s and state %s", presence.user_id, presence.username, nk.json_encode(presence)))
        state.presences[presence.session_id] = presence
        dummyinitialstate = {}
        dispatcher.broadcast_message(1, dummyinitialstate, nil, nil)
        -- or tried this also 
        dispatcher.broadcast_message(1, dummyinitialstate, { presence }, nil)
    end
    return state
end

```

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [February 4, 2022, 10:48am UTC](https://forum.heroiclabs.com/t/sending-initial-game-state-in-match-join/2340/2 "2022-02-04T10:48:41Z")

</div>

Hey @mattkanwisher, this is the correct approach, however the `broadcast_message` function expects the #2 argument to be a string, so you should use `nk.json_encode` on `dummyinitialstate` before passing it to the function.

Let me know if this helps.

---

<div class="post-metadata">

**Author:** ![mattkanwisher](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/mattkanwisher/32/976_2.png) [@mattkanwisher](https://forum.heroiclabs.com/u/mattkanwisher)\
**Post date:** [March 18, 2022, 5:03am UTC](https://forum.heroiclabs.com/t/sending-initial-game-state-in-match-join/2340/3 "2022-03-18T05:03:27Z")

</div>

It is a string, on Match\_Join the problem is that it will NOT be broadcasted to the newly joined player so they don’t get initial state. We ended up having to have an initial state message, that all clients call. I think this is a shortcoming of the api

---

<div class="post-metadata">

**Author:** ![mofirouz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/mofirouz/32/1523_2.png) [@mofirouz](https://forum.heroiclabs.com/u/mofirouz)\
**Post date:** [March 23, 2022, 10:42am UTC](https://forum.heroiclabs.com/t/sending-initial-game-state-in-match-join/2340/4 "2022-03-23T10:42:40Z")

</div>

@mattkanwisher This code path has been tested extensively with live projects; it’s highly unlikely that this could be a bug in Nakama match runtime.

This could be that the client isn’t configured properly; I’d suggest the following on the client:

1. Check that all network calls to Nakama are `async/await`ed - This means that you actually wait for the network request to complete between your logic runs further on the client.

2. You connect the socket listeners before you attempt to make network request (or join a match). To me this sounds like the most probable issue as looks like you are missing some messages until the listener is connected.
