Nakama official client

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.

2 Likes

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

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

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.

1 Like
    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

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?

3 Likes

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

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

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

1 Like

Posted on the Nakama forum too.

1 Like

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.

4 Likes

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

Are they absent from the current Nakama defold integration?

Good question! The code snippets in the docs only show Unity examples. Post on the Nakama forum as well and ask!

@Lex The IAP validation stuff was moved to Lua utils a while back and that page should not be reachable in our main documentation (I think it’s left behind because of a CDN). We are working to bring back the official API for it though with new improvements to handle subscription callbacks with Battle Pass style purchases. The new IAP system with support AppStore, Play Store, and AppGallery (from Huawei).

In the meantime have a look at this code for what you need:

3 Likes

Thanks for the help!
Yes I figured it out and implemented it correctly.

By the way, I had the add the query parameter unwrap=1 when calling RPC functions, otherwise it wouldn’t work.

@novabyte Do you have any example of working Nakama+Defold Google login?
I can’t make it work, because I can’t get the Oauth token from the Google Play Game Services Defold SDK.
Is there any other way to get the Google Oauth token? (I posted a thread Google Play Game service: access token for Nakama but can’t get help on this).

1 Like

I saw the PR for this here: https://github.com/heroiclabs/nakama-defold/pull/27

I added a comment about this since the PR modified generated code which would get overwritten the next time the API was generated.

i try to send message, but nothing happen:

function M.sendMessage()
	nakama.sync(function()
		local data = json.encode({
			row = 11,
			col = 22,
		})
		data=base64.encode(data)
		local op_code = 1
		local result = nakama_socket.match_data_send(socket, matchID, op_code, data)
		print("sended")
		if result.error then
			print(result.error.message)
			pprint(result)
		else
			print("ok!!!")
		end
	end)
end

“sended” - is not print. Please help! What i do wrong?

Which Nakama version are you using? The fact that you do not see your print indicates that something crashed inside the coroutine. But there console should show the error. You can try sending it without a coroutine (ie not inside nakama.sync)

Also, they should be no need to base 64 encode the data. It is handled automatically:

I used 3.0.3, then I saw on github that I needed to use the master. Yes indeed b64 is no longer needed.
if i send without “sync” i have error:

ERROR:SCRIPT: nakama/util/async.lua:19: assertion failed!
stack traceback:
  [C]:-1: in function assert
  nakama/util/async.lua:19: in function match_data_send
  main/multiplayer.lua:122: in function sendMessage
  main/main.gui_script:31: in function callback
  druid/event.lua:90: in function trigger
  druid/base/button.lua:88: in function on_button_click
  druid/base/button.lua:159: in function on_input
  druid/system/druid_instance.lua:183: in function process_input
  druid/system/druid_instance.lua:359: in function <druid/system/druid_instance.lua:354>

Did you provide a callback function? Required if you are not running from a coroutine.