Decoding message.data in MatchLoop throws exception

I got an exception while JSON parsing the message.data in MatchLoop function.

let msg = {} as MoveMessage;
                try {
                    msg = JSON.parse(nk.binaryToString(message.data));
                } catch (error) {
                    // Client sent bad data.
          
                    logger.debug('Bad data received: %v', error);
                    continue;
                }

From the nakama project template on github

However, if I just use
msg = JSON.parse(message.data.toString());

it works correctly. Is the code on github wrong or am I doing something incorrectly.

Hi @mdee5

Which error are you getting, and are you using the latest version of Nakama?

I have same issue with xoxo example (GitHub - heroiclabs/xoxo-phaserjs). This is because javascript SDK sends plain JSON instead of binary I think. And now I trying to understand how to work with nakama-js-protobuf package

Sorry for late reply.
I am using nakama 3.3.0 with godot.

Here a results of my research:

  1. Transport serialization type (plain json or protobuf) depends on client side. You should use the Protobuf Adapter with Nakama JS SDK nakama-js/packages/nakama-js-protobuf at master · heroiclabs/nakama-js · GitHub for this.

  2. But protobuf serialization works only for nakama build-in data messages. You should serialize your data by yourself (Protobuf and REST API - #2 by novabyte) because there is no way to extend nakama build-in protobuf schemas (Decode own protocol protobuf messages in JS client - #2 by mofirouz). Easiest way is msgpack (MessagePack: It's like JSON. but fast and small.), but there is a huge owerhead because msgpack data contains field names

  3. JS SDK have issues with sending binary data itself also. Discussion and solution here - Nakama js client has problems with decoding data

  4. Typescript server framework have wrong binary array data declarations. Discussion and solution here - [TS] Fix wrong binary array data declaration · Issue #66 · heroiclabs/nakama-common · GitHub

3 and 4 will be fixed soon i hope