# Nakama official client

**URL:** <https://forum.defold.com/t/nakama-official-client/65337>\
**Category:** The Defoldmine\
**Created:** [May 25, 2020, 8:05pm UTC](https://forum.defold.com/t/nakama-official-client/65337 "2020-05-25T20:05:24Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![MrZak-dev](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mrzak-dev/32/20569_2.png) [@MrZak-dev](https://forum.defold.com/u/MrZak-dev)\
**Post date:** [January 4, 2021, 2:14pm UTC](https://forum.defold.com/t/nakama-official-client/65337/22 "2021-01-04T14:14:55Z")

</div>

I couldn’t find Lua reference to create a Match (not a matchmaker) , i used the javascript api as refence .  
To [Create Match](https://heroiclabs.com/docs/gameplay-multiplayer-realtime/#create-a-match) in js you have to use

```js
var response = await socket.createMatch();
console.log("Created match with ID:", response.match.match_id);

```

You can share the match id with other players to join .  
i want to do the same thing using Defold and Lua .  
if i am not mistaken , the same should be in Lua :

```lua
local match_create_message = {
        match_create = {} -- empty , no arguments		
}
		
local match_result = nakama.socket_send(socket, match_create_message,callback)

```

This caused the server to disconnect with :

```bash
{"level":"warn","ts":"2021-01-04T11:23:32.727Z","msg":"Received malformed payload","uid":"51fc50d1-da4e-4f4c-b74a-ea60e75bad37","sid":"71335a58-9d9d-4d72-8cd4-3942df576269","data":"eyJjaWQiOiIxIiwibWF0Y2hfY3JlYXRlIjpbXX0="}

```

after decoding the data

```json
{"cid":"1","match_create":[]}

```

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [January 4, 2021, 2:31pm UTC](https://forum.defold.com/t/nakama-official-client/65337/23 "2021-01-04T14:31:27Z")

</div>

Please have a look here: [Nakama official client - #12 by Alex\_8BitSkull](https://forum.defold.com/t/nakama-official-client/65337/12)

---

<div class="post-metadata">

**Author:** ![MrZak-dev](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mrzak-dev/32/20569_2.png) [@MrZak-dev](https://forum.defold.com/u/MrZak-dev)\
**Post date:** [January 4, 2021, 2:42pm UTC](https://forum.defold.com/t/nakama-official-client/65337/24 "2021-01-04T14:42:29Z")

</div>

> [@britzl](#):
>
> Please have a look here: [Nakama official client](https://forum.defold.com/t/nakama-official-client/65337/12)

I have checked the Official defold client , i couldn’t find a way through making a custom Match(not a match maker) , similar to [Realtime Multiplayer - Nakama server (heroiclabs.com)](https://heroiclabs.com/docs/gameplay-multiplayer-realtime/#create-a-match)

---

<div class="post-metadata">

**Author:** ![kiprono](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/kiprono/32/21981_2.png) [@kiprono](https://forum.defold.com/u/kiprono)\
**Post date:** [January 4, 2021, 4:49pm UTC](https://forum.defold.com/t/nakama-official-client/65337/25 "2021-01-04T16:49:45Z")

</div>

The error seems to be this

```lua
{"cid":"1","match_create":[]}

```

It should be

```lua
{"cid":"1","match_create":{}}

```

Edit.  
I have tested and this is the problem.

---

<div class="post-metadata">

**Author:** ![kiprono](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/kiprono/32/21981_2.png) [@kiprono](https://forum.defold.com/u/kiprono)\
**Post date:** [January 4, 2021, 4:51pm UTC](https://forum.defold.com/t/nakama-official-client/65337/26 "2021-01-04T16:51:06Z")

</div>

To fix it, you to have to patch nakama.engine.defold.lua  
You properly have to copy manually the nakama modules to your project.

Change function M.socket\_send to be

```
function M.socket_send(socket, message, callback)
	assert(socket and socket.ws, "You must provide a socket")
	assert(message, "You must provide a message to send")
	socket.cid = socket.cid + 1
	message.cid = tostring(socket.cid)

	socket.requests[message.cid] = callback
	local data = json.encode(message)
	local clean_data = string.gsub(data, "%[%]", "{}")
	pprint("Todo remove", clean_data)
	socket.ws:send(clean_data) 
end

```

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [January 4, 2021, 5:46pm UTC](https://forum.defold.com/t/nakama-official-client/65337/27 "2021-01-04T17:46:41Z")

</div>

Please create a ticket in the Nakama repo and I’ll make sure to fix it! It should be solved in the Nakama client.

---

<div class="post-metadata">

**Author:** ![kiprono](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/kiprono/32/21981_2.png) [@kiprono](https://forum.defold.com/u/kiprono)\
**Post date:** [January 4, 2021, 5:54pm UTC](https://forum.defold.com/t/nakama-official-client/65337/28 "2021-01-04T17:54:26Z")

</div>

I don’t know if it’s a bug. It come down to how to encode lua’s {} to json. Should it be encoded as “{}” or “”.

---

<div class="post-metadata">

**Author:** ![MrZak-dev](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mrzak-dev/32/20569_2.png) [@MrZak-dev](https://forum.defold.com/u/MrZak-dev)\
**Post date:** [January 4, 2021, 5:55pm UTC](https://forum.defold.com/t/nakama-official-client/65337/29 "2021-01-04T17:55:47Z")

</div>

> [@kiprono](#):
>
> Change function M.socket\_send to be

So this instead of sending a json empty object it sent an empty array .

> [@britzl](#):
>
> Please create a ticket in the Nakama repo and I’ll make sure to fix it! It should be solved in the Nakama client.

I have already created a ticket about this topic

[Creating a Match in Defold · Issue #17 · heroiclabs/nakama-defold (github.com)](https://github.com/heroiclabs/nakama-defold/issues/17)

---

<div class="post-metadata">

**Author:** ![kiprono](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/kiprono/32/21981_2.png) [@kiprono](https://forum.defold.com/u/kiprono)\
**Post date:** [January 4, 2021, 6:10pm UTC](https://forum.defold.com/t/nakama-official-client/65337/30 "2021-01-04T18:10:15Z")

</div>

Yes, that is the issue. I will properly create a pull request.  
Talking of which the nakama API seems to have changed, For example in the authenticate\_email callback, it does not return result.error but result.code.

Edit.  
I have created a pull request in github.

---

<div class="post-metadata">

**Author:** ![MrZak-dev](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mrzak-dev/32/20569_2.png) [@MrZak-dev](https://forum.defold.com/u/MrZak-dev)\
**Post date:** [January 5, 2021, 6:52pm UTC](https://forum.defold.com/t/nakama-official-client/65337/31 "2021-01-05T18:52:33Z")

</div>

> [@kiprono](#):
>
> Edit.  
> I have created a pull request in github.

[https://github.com/heroiclabs/nakama-defold/pull/18](https://github.com/heroiclabs/nakama-defold/pull/18)

---

<div class="post-metadata">

**Author:** ![MrZak-dev](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mrzak-dev/32/20569_2.png) [@MrZak-dev](https://forum.defold.com/u/MrZak-dev)\
**Post date:** [January 6, 2021, 7:48am UTC](https://forum.defold.com/t/nakama-official-client/65337/32 "2021-01-06T07:48:54Z")

</div>

> [@kiprono](#):
>
> Edit.  
> I have created a pull request in github.

@kiprono  
What if actually we meant to send an empty array for example , this will force all `[]` to be `{ }` without exceptions .  
e.g :

```lua
local match_data = {
    match_data_send = {
        match_id = match.match.match_id,
        op_code = 1337,
        data = {
            players = {} -- i want to receive it as an empty array
        }
    }
}

```

if i am not mistaken, in this case the players will be converted to Json object instead of an empty array of players

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [January 6, 2021, 8:39am UTC](https://forum.defold.com/t/nakama-official-client/65337/33 "2021-01-06T08:39:42Z")

</div>

> [@MrZak-dev](#):
>
> What if actually we meant to send an empty array for example , this will force all `[]` to be `{ }` without exceptions

Yep, this is a problem with json encoding an empty table from Lua since you can’t tell if it is supposed to be an empty array or an empty object. I don’t have a good solution for it.

---

<div class="post-metadata">

**Author:** ![MrZak-dev](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mrzak-dev/32/20569_2.png) [@MrZak-dev](https://forum.defold.com/u/MrZak-dev)\
**Post date:** [January 6, 2021, 9:39am UTC](https://forum.defold.com/t/nakama-official-client/65337/34 "2021-01-06T09:39:18Z")

</div>

```lua
    local data = json.encode(message)
    local message
    local is_match_create = string.find(data,"match_create")
    if is_match_create ~= nil then -- match_create message
        message = string.gsub(data, "%[%]", "{}") -- array to json object
    end
	
    socket.ws:send(message)

```

based on @kiprono solution  
this works in case a user send a `match_create` message , i guess this type of message is the only one that’s being sent without params

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [March 12, 2021, 2:42pm UTC](https://forum.defold.com/t/nakama-official-client/65337/35 "2021-03-12T14:42:52Z")

</div>

Hey guys, first of all I want to say that Nakama is AWESOME. Being able to write server code in lua has enabled me to set up a simple realtime multiplayer game quickly. This is so much fun.

I bumped into a small problem when attempting to join a match. Sending this from the client:

```
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

```

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 parameter is `nil` everything works as it should. I haven’t found any documentation about how the metadata should be formatted, hence this post. Any ideas?

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [March 12, 2021, 2:58pm UTC](https://forum.defold.com/t/nakama-official-client/65337/36 "2021-03-12T14:58:12Z")

</div>

What if you don’t json.encode the metadata and instead send it as a table?

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [March 12, 2021, 3:02pm UTC](https://forum.defold.com/t/nakama-official-client/65337/37 "2021-03-12T15:02:30Z")

</div>

I tried that first by mistake actually! It produces 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="}

```

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [March 12, 2021, 3:06pm UTC](https://forum.defold.com/t/nakama-official-client/65337/38 "2021-03-12T15:06:36Z")

</div>

Hmm, perhaps @novabyte sees and can help us out. Otherwise I’d recommend posting on the Nakama forum to get help.

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [March 12, 2021, 3:19pm UTC](https://forum.defold.com/t/nakama-official-client/65337/39 "2021-03-12T15:19:33Z")

</div>

Posted on the [Nakama forum](https://forum.heroiclabs.com/t/sending-metadata-when-joining-match-with-defold/1435) too.

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [March 13, 2021, 12:55pm UTC](https://forum.defold.com/t/nakama-official-client/65337/40 "2021-03-13T12:55:57Z")

</div>

Solved! The metadata has to be a table containing only strings as values. Like this:

```
local metadata = {}
metadata.equipped_weapon_type = "1"
local message = nakama.create_match_join_message(match_id, token, metadata)

```

More info on [Nakama’s forums](https://forum.heroiclabs.com/t/sending-metadata-when-joining-match-with-defold-solved/1435/11).

---

<div class="post-metadata">

**Author:** ![Lex](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/lex/32/23856_2.png) [@Lex](https://forum.defold.com/u/Lex)\
**Post date:** [March 15, 2021, 12:56pm UTC](https://forum.defold.com/t/nakama-official-client/65337/41 "2021-03-15T12:56:20Z")

</div>

I can’t find the method to validate in-app purchases from both Google and iOS as mentioned here:  
[https://heroiclabs.com/docs/in-app-purchase-validation/#recovering-purchases](https://heroiclabs.com/docs/in-app-purchase-validation/#recovering-purchases)

Are they absent from the current Nakama defold integration?

[Previous page](https://forum.defold.com/t/nakama-official-client/65337.md?page=1)

[Next page](https://forum.defold.com/t/nakama-official-client/65337.md?page=3)
