# Sending metadata when joining match with Defold (SOLVED)

**URL:** https://forum.heroiclabs.com/t/sending-metadata-when-joining-match-with-defold-solved/1435
**Category:** Game Design
**Tags:** server-framework
**Created:** [March 12, 2021, 3:19pm UTC](https://forum.heroiclabs.com/t/sending-metadata-when-joining-match-with-defold-solved/1435 "2021-03-12T15:19:02Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![totebo](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/totebo/32/528_2.png) [@totebo](https://forum.heroiclabs.com/u/totebo)
#### Post date: [March 12, 2021, 3:19pm UTC](https://forum.heroiclabs.com/t/sending-metadata-when-joining-match-with-defold-solved/1435/1 "2021-03-12T15:19:02Z")

</div>

Using the Defold SDK I’m using this code to send metadata with the join request:

```
local metadata = {}
metadata.equipped_weapon_type = 1
local metadata_json = json.encode(metadata) -- {"equipped_weapon_type":1}
local message = nakama.create_match_join_message(match_id, token, metadata_json)

--[[
-- message:
{ 
  match_join = { --[[0x120d6fe00]]
    match_id = "9b8d2c60-25e5-4ddb-8e26-d0717d47dfb4.nakama-node-1",
    metadata = "{"equipped_weapon_type":1}"
  }
}
--]]

local result = nakama.socket_send(socket, message) -- This call fails

```

Which results in this server error:

`{"level":"debug","ts":"2021-03-12T14:29:54.210Z","msg":"Error reading message from client","uid":"4b4b9e18-8c26-4a62-a857-9f97f994ac7c","sid":"66a02717-d678-4b5a-9855-7f030c5136c1","error":"websocket: close 1006 (abnormal closure): unexpected EOF"}`

If the metadata is sent as a lua table instead, the server reports a different error:

`{"level":"warn","ts":"2021-03-12T15:01:19.829Z","msg":"Received malformed payload","uid":"c5922789-ae72-4c54-8ec1-b475b3aa4358","sid":"d5101fde-45ce-4ac5-86e2-97b7d8076624","data":"eyJjaWQiOiIyIiwibWF0Y2hfam9pbiI6eyJtZXRhZGF0YSI6eyJ3ZWFwb25fdHlwZSI6MX0sIm1hdGNoX2lkIjoiZWNjMDFkYjAtZjE0MC00MDk0LTk0YzYtNmJlOWUxYzU1ODMyLm5ha2FtYS1ub2RlLTEifX0="}`

If the metadata parameter is omitted, everything works as it should and there are no server errors.

Any idea how to successfully send metadata using Defold? Also posted this on the [Defold forums](https://forum.defold.com/t/nakama-official-client/65337/35).

---

<div class="post-metadata">

### Author: ![lugehorsam](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/lugehorsam/32/314_2.png) [@lugehorsam](https://forum.heroiclabs.com/u/lugehorsam)
#### Post date: [March 12, 2021, 3:55pm UTC](https://forum.heroiclabs.com/t/sending-metadata-when-joining-match-with-defold-solved/1435/2 "2021-03-12T15:55:32Z")

</div>

@totebo it looks like you need to add escape characters to your raw json string.

For your table example, also make sure you are calling `json.encode` on the table you are passing as metadata.

---

<div class="post-metadata">

### Author: ![totebo](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/totebo/32/528_2.png) [@totebo](https://forum.heroiclabs.com/u/totebo)
#### Post date: [March 12, 2021, 5:15pm UTC](https://forum.heroiclabs.com/t/sending-metadata-when-joining-match-with-defold-solved/1435/3 "2021-03-12T17:15:53Z")

</div>

Thanks @lugehorsam!

> [@lugehorsam](#):
>
> For your table example, also make sure you are calling `json.encode` on the table you are passing as metadata.

I believe this is handled internally by the Defold Nakama extension. Check out [this line](https://github.com/heroiclabs/nakama-defold/blob/master/nakama/engine/defold.lua#L152) from Github.

> [@lugehorsam](#):
>
> @totebo it looks like you need to add escape characters to your raw json string.

If using a json string I get double quotes (like above) back from Defold when using the `nakama.create_match_join_message()` function. If using a table instead it looks like this:

```
{ 
  match_join = { 
    match_id = "79fe7b2e-8ac7-45cd-945c-77238f956217.nakama-node-1",
    metadata = { --[[0x124eee4e0]]
      equipped_weapon_type = 1
    }
  }
}

```

But causes a different server error (see above). Is there a way of getting more detailed errors somehow?

---

<div class="post-metadata">

### Author: ![lugehorsam](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/lugehorsam/32/314_2.png) [@lugehorsam](https://forum.heroiclabs.com/u/lugehorsam)
#### Post date: [March 12, 2021, 6:15pm UTC](https://forum.heroiclabs.com/t/sending-metadata-when-joining-match-with-defold-solved/1435/5 "2021-03-12T18:15:53Z")

</div>

@totebo aha, what happens if you try a string instead of an integer for the value in your metadata?

---

<div class="post-metadata">

### Author: ![totebo](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/totebo/32/528_2.png) [@totebo](https://forum.heroiclabs.com/u/totebo)
#### Post date: [March 12, 2021, 6:27pm UTC](https://forum.heroiclabs.com/t/sending-metadata-when-joining-match-with-defold-solved/1435/6 "2021-03-12T18:27:08Z")

</div>

Same errors as above I’m afraid.

---

<div class="post-metadata">

### Author: ![lugehorsam](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/lugehorsam/32/314_2.png) [@lugehorsam](https://forum.heroiclabs.com/u/lugehorsam)
#### Post date: [March 12, 2021, 6:47pm UTC](https://forum.heroiclabs.com/t/sending-metadata-when-joining-match-with-defold-solved/1435/7 "2021-03-12T18:47:19Z")

</div>

@totebo Could you please share the malformed data payload reported by the server as well as the code you are using to send the data?

---

<div class="post-metadata">

### Author: ![totebo](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/totebo/32/528_2.png) [@totebo](https://forum.heroiclabs.com/u/totebo)
#### Post date: [March 13, 2021, 12:41am UTC](https://forum.heroiclabs.com/t/sending-metadata-when-joining-match-with-defold-solved/1435/8 "2021-03-13T00:41:01Z")

</div>

> [@lugehorsam](#):
>
> Could you please share the malformed data payload reported by the server as well as the code you are using to send the data?

The error messages from the Nakama server, as well as the code I use to send it, is in my first post.

To try to get the most relevant message, I added a print to see what the actual message passed to the Websocket extension is, before it reaches Nakama.

This line:

> <https://github.com/heroiclabs/nakama-defold/blob/master/nakama/engine/defold.lua#L161>

Is sent the following parameters:

```
userdata: 0x7fb2d17fe400,
{"cid":"2","match_join":{"match_id":"261d5e37-eee4-4ab9-926c-b1e03867a508.nakama-node-1","metadata":{"equipped_weapon_type":1}}},
{ --[[0x11460f600]]
  type = 1
}

```

Other than that I’m not sure how to get any more information, but would be happy to try any suggestion!

---

<div class="post-metadata">

### Author: ![lugehorsam](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/lugehorsam/32/314_2.png) [@lugehorsam](https://forum.heroiclabs.com/u/lugehorsam)
#### Post date: [March 13, 2021, 1:03am UTC](https://forum.heroiclabs.com/t/sending-metadata-when-joining-match-with-defold-solved/1435/9 "2021-03-13T01:03:26Z")

</div>

@totebo I’m asking for the most recent version of the code with a string used as a value in the metadata, as well as the server logs. You showed two different types of your code, one with a table and one with a raw string. Let’s just go with the table example – could you pass in a table, share the client side log as you just did, and then the server log. The server logs will be different by virtue of the payload being different.

---

<div class="post-metadata">

### Author: ![totebo](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/totebo/32/528_2.png) [@totebo](https://forum.heroiclabs.com/u/totebo)
#### Post date: [March 13, 2021, 10:03am UTC](https://forum.heroiclabs.com/t/sending-metadata-when-joining-match-with-defold-solved/1435/10 "2021-03-13T10:03:04Z")

</div>

Okay, got you. Thanks for your help (and patience!). This is the same message, with the latest code, showing what goes out from the client and what the server log reports. I included a little before and after just in case it might help figure this out.

Client:

```
DEBUG:SCRIPT: Sending match_join message
DEBUG:SCRIPT: 
{ --[[0x120c5ddf0]]
  match_join = { --[[0x11fad6020]]
    match_id = "e0a4d2d6-7156-4c17-b15c-fd948a82d890.nakama-node-1",
    metadata = { --[[0x120c49860]]
      weapon_type = 1
    }
  }
}
DEBUG:SCRIPT: websocket.send(),
userdata: 0x7fe540075000,
{"cid":"2","match_join":{"match_id":"e0a4d2d6-7156-4c17-b15c-fd948a82d890.nakama-node-1","metadata":{"weapon_type":1}}},
nil
WARNING:WEBSOCKET: Sent buffer: '\81\f7\b8EU\16' 6 bytes
WARNING:WEBSOCKET: Sent buffer: '\c3g6\7f\dcgo4\8agy4\d5$!u\d0\1a?y\d1+w,\c3g8w\cc&=I\d1!w,\9a ew\8c!gr\8ehb'\8dsx"\dbtb;\dat`u\95#1/\8c}4.\8a!m/\88k;w\d3$8w\95+:r\ddhd4\94g8s\cc$1w\cc$w,\c3g"s\d95:x\e71,f\ddgo'\c58(' 119 bytes
WARNING:WEBSOCKET: Received bytes: '\88\00' 2 bytes
WARNING:WEBSOCKET: PushMessage 'Server closing (1005). Reason: ''' 33 bytes
WARNING:WEBSOCKET: Server closing (1005). Reason: ''
WARNING:WEBSOCKET: Sent buffer: '\88\80\9d\e6\91\db' 6 bytes
WARNING:WEBSOCKET: STATE_CONNECTED -> STATE_DISCONNECTED
WARNING:WEBSOCKET: PushMessage 'Server closing (1005). Reason: ''' 33 bytes
DEBUG:SCRIPT: EVENT_DISCONNECTED: Server closing (1005). Reason: ''
WARNING:WEBSOCKET: DestroyConnection: 0x7fe540075000

```

Server:

```
{"level":"info","ts":"2021-03-13T09:59:06.117Z","msg":"Matched user '205dd015-318f-419f-93f6-649c53b161f3' named 'CPZhaDPLSl'"}
{"level":"info","ts":"2021-03-13T09:59:06.118Z","msg":"Matched user 'c6158bef-edd3-45e8-bdbf-8f1a455edb4b' named 'uzKycRDWzs'"}
{"level":"info","ts":"2021-03-13T09:59:06.120Z","msg":"match_init","mid":"e0a4d2d6-7156-4c17-b15c-fd948a82d890"}
{"level":"info","ts":"2021-03-13T09:59:06.120Z","msg":"Match started","mid":"e0a4d2d6-7156-4c17-b15c-fd948a82d890"}
{"level":"warn","ts":"2021-03-13T09:59:06.148Z","msg":"Received malformed payload","uid":"c6158bef-edd3-45e8-bdbf-8f1a455edb4b","sid":"21e02c35-dff7-4163-ae8a-2b4f6b59c634","data":"eyJjaWQiOiIyIiwibWF0Y2hfam9pbiI6eyJtZXRhZGF0YSI6eyJ3ZWFwb25fdHlwZSI6MX0sIm1hdGNoX2lkIjoiZTBhNGQyZDYtNzE1Ni00YzE3LWIxNWMtZmQ5NDhhODJkODkwLm5ha2FtYS1ub2RlLTEifX0="}
{"level":"info","ts":"2021-03-13T09:59:06.148Z","msg":"Cleaning up closed client connection","uid":"c6158bef-edd3-45e8-bdbf-8f1a455edb4b","sid":"21e02c35-dff7-4163-ae8a-2b4f6b59c634"}
{"level":"info","ts":"2021-03-13T09:59:06.149Z","msg":"Cleaned up closed connection matchmaker","uid":"c6158bef-edd3-45e8-bdbf-8f1a455edb4b","sid":"21e02c35-dff7-4163-ae8a-2b4f6b59c634"}
{"level":"info","ts":"2021-03-13T09:59:06.149Z","msg":"Cleaned up closed connection tracker","uid":"c6158bef-edd3-45e8-bdbf-8f1a455edb4b","sid":"21e02c35-dff7-4163-ae8a-2b4f6b59c634"}
{"level":"info","ts":"2021-03-13T09:59:06.149Z","msg":"Cleaned up closed connection session registry","uid":"c6158bef-edd3-45e8-bdbf-8f1a455edb4b","sid":"21e02c35-dff7-4163-ae8a-2b4f6b59c634"}
{"level":"info","ts":"2021-03-13T09:59:06.149Z","msg":"Closed client connection","uid":"c6158bef-edd3-45e8-bdbf-8f1a455edb4b","sid":"21e02c35-dff7-4163-ae8a-2b4f6b59c634"}
{"level":"warn","ts":"2021-03-13T09:59:06.179Z","msg":"Received malformed payload","uid":"205dd015-318f-419f-93f6-649c53b161f3","sid":"80251629-796f-49e1-b736-4acc0ea67832","data":"eyJjaWQiOiIyIiwibWF0Y2hfam9pbiI6eyJtYXRjaF9pZCI6ImUwYTRkMmQ2LTcxNTYtNGMxNy1iMTVjLWZkOTQ4YTgyZDg5MC5uYWthbWEtbm9kZS0xIiwibWV0YWRhdGEiOnsid2VhcG9uX3R5cGUiOjF9fX0="}
{"level":"info","ts":"2021-03-13T09:59:06.179Z","msg":"Cleaning up closed client connection","uid":"205dd015-318f-419f-93f6-649c53b161f3","sid":"80251629-796f-49e1-b736-4acc0ea67832"}
{"level":"info","ts":"2021-03-13T09:59:06.179Z","msg":"Cleaned up closed connection matchmaker","uid":"205dd015-318f-419f-93f6-649c53b161f3","sid":"80251629-796f-49e1-b736-4acc0ea67832"}
{"level":"info","ts":"2021-03-13T09:59:06.179Z","msg":"Cleaned up closed connection tracker","uid":"205dd015-318f-419f-93f6-649c53b161f3","sid":"80251629-796f-49e1-b736-4acc0ea67832"}
{"level":"info","ts":"2021-03-13T09:59:06.179Z","msg":"Cleaned up closed connection session registry","uid":"205dd015-318f-419f-93f6-649c53b161f3","sid":"80251629-796f-49e1-b736-4acc0ea67832"}
{"level":"info","ts":"2021-03-13T09:59:06.180Z","msg":"Closed client connection","uid":"205dd015-318f-419f-93f6-649c53b161f3","sid":"80251629-796f-49e1-b736-4acc0ea67832"}
```

---

<div class="post-metadata">

### Author: ![novabyte](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/novabyte/32/1324_2.png) [@novabyte](https://forum.heroiclabs.com/u/novabyte)
#### Post date: [March 13, 2021, 11:28am UTC](https://forum.heroiclabs.com/t/sending-metadata-when-joining-match-with-defold-solved/1435/11 "2021-03-13T11:28:25Z")

</div>

@totebo Metadata passed into match join attempt must be a flat JSON object of string keys to string values. In your case the code should not be:

```lua
metadata = {
    equipped_weapon_type = 1
}

```

Instead it should be:

```lua
metadata = {
    equipped_weapon_type = "1"
}

```

This is a constraint of the API because in Protocol Buffers its exposed as a map type:

[nakama-common/realtime.proto at master · heroiclabs/nakama-common · GitHub](https://github.com/heroiclabs/nakama-common/blob/master/rtapi/realtime.proto#L343)

Hope this helps.

---

<div class="post-metadata">

### Author: ![totebo](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/totebo/32/528_2.png) [@totebo](https://forum.heroiclabs.com/u/totebo)
#### Post date: [March 13, 2021, 12:38pm UTC](https://forum.heroiclabs.com/t/sending-metadata-when-joining-match-with-defold-solved/1435/12 "2021-03-13T12:38:58Z")

</div>

Hi @novabyte, yes that certainly did help! I tried it before, but only when I used JSON rather than a table. The payload needs to be a table with strings as values. Thanks!
