Sending metadata when joining match with Defold (SOLVED)

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.

@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.

Thanks @lugehorsam!

I believe this is handled internally by the Defold Nakama extension. Check out this line from Github.

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?

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

Same errors as above I’m afraid.

@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?

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:

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!

@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.

1 Like

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"}

@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:

metadata = {
    equipped_weapon_type = 1
}

Instead it should be:

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

Hope this helps.

2 Likes

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!

1 Like