# Parsing Match Loop message (Example code broken?)

**URL:** <https://forum.heroiclabs.com/t/parsing-match-loop-message-example-code-broken/2232>\
**Category:** Runtime Framework\
**Tags:** server-framework\
**Created:** [January 5, 2022, 8:21am UTC](https://forum.heroiclabs.com/t/parsing-match-loop-message-example-code-broken/2232 "2022-01-05T08:21:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![RunGrizzly](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/rungrizzly/32/918_2.png) [@RunGrizzly](https://forum.heroiclabs.com/u/RunGrizzly)\
**Post date:** [January 5, 2022, 8:21am UTC](https://forum.heroiclabs.com/t/parsing-match-loop-message-example-code-broken/2232/1 "2022-01-05T08:21:37Z")

</div>

I’m trying to send data objects to the server to modify the state of server authoritive matches. I understand the concept - I use SendMatchStateAsync, plugin a data object and parse it on the other end (MatchLoop)

Using the example code however, throws an error. Here it is from the Nakama project example:

> let msg = {} as MoveMessage;  
> try {  
> msg = JSON.parse(message.data);  
> }

It seems however, that you cannot simply parse the data, since it is of type UInt8Array

Am I missing something? Or is there some other syntax/typing that works to properly peel out the data?

---

<div class="post-metadata">

**Author:** ![LucasGaspar](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/lucasgaspar/32/407_2.png) [@LucasGaspar](https://forum.heroiclabs.com/u/LucasGaspar)\
**Post date:** [January 5, 2022, 1:01pm UTC](https://forum.heroiclabs.com/t/parsing-match-loop-message-example-code-broken/2232/2 "2022-01-05T13:01:06Z")

</div>

You are just missing one little thing

```auto
msg = JSON.parse(String(message.data));

```

---

<div class="post-metadata">

**Author:** ![RunGrizzly](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/rungrizzly/32/918_2.png) [@RunGrizzly](https://forum.heroiclabs.com/u/RunGrizzly)\
**Post date:** [January 5, 2022, 1:14pm UTC](https://forum.heroiclabs.com/t/parsing-match-loop-message-example-code-broken/2232/3 "2022-01-05T13:14:30Z")

</div>

Thanks for the reply,

So you can just cast the Uint8Array directly to string? This must mean that the example project is broken.

---

<div class="post-metadata">

**Author:** ![LucasGaspar](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/lucasgaspar/32/407_2.png) [@LucasGaspar](https://forum.heroiclabs.com/u/LucasGaspar)\
**Post date:** [January 5, 2022, 2:16pm UTC](https://forum.heroiclabs.com/t/parsing-match-loop-message-example-code-broken/2232/4 "2022-01-05T14:16:02Z")

</div>

I think it’s outdated, the data before was returned as string but when passing bytes to the MatchState the data was getting corrupted, so they changed it to Uint8Array on some of the latests releases

---

<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:** [January 5, 2022, 2:45pm UTC](https://forum.heroiclabs.com/t/parsing-match-loop-message-example-code-broken/2232/5 "2022-01-05T14:45:23Z")

</div>

@RunGrizzly The latest release of Nakama server (last one before Christmas) did change the field type which is returned to the runtime to provide better performance when the type does not need to be coerced into a string. We’ll update the project template. Thanks for the heads up 🙏

---

<div class="post-metadata">

**Author:** ![RunGrizzly](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/rungrizzly/32/918_2.png) [@RunGrizzly](https://forum.heroiclabs.com/u/RunGrizzly)\
**Post date:** [January 5, 2022, 4:59pm UTC](https://forum.heroiclabs.com/t/parsing-match-loop-message-example-code-broken/2232/6 "2022-01-05T16:59:58Z")

</div>

Awesome, thanks for the clarification.

---

<div class="post-metadata">

**Author:** ![RunGrizzly](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/rungrizzly/32/918_2.png) [@RunGrizzly](https://forum.heroiclabs.com/u/RunGrizzly)\
**Post date:** [January 5, 2022, 10:56pm UTC](https://forum.heroiclabs.com/t/parsing-match-loop-message-example-code-broken/2232/7 "2022-01-05T22:56:35Z")

</div>

Update: Casting to string does not work at all and simply throws the same error. The matchloop only sees the message data as [ObjectArrayBuffer]

The data object I’m trying to parse is a very simple struct with two integers.

A little digging tells me that the problem requires the TextDecoder JS class to create a string from the Array - but when implemented it triggers an early execute and logs this:

**Match Loop:**

> logger.debug('Match mod received with raw data: ’ + message.data);  
> var c = String(message.data);  
> logger.debug('Data as string looks like: ’ + c);  
> var enc = new TextDecoder(“utf-8”);  
> var d = enc.decode(message.data);  
> logger.debug('Decoded data looks like: ’ + d);

**Throws at decoder:**

> {“level”:“warn”,“ts”:“2022-01-05T22:51:29.326Z”,“caller”:“server/match\_handler.go:295”,“msg”:“Stopping match after error from match\_loop execution”,“mid”:“f91c836a-bc0f-4625-a418-4e401282e6e8”,“tick”:1,“error”:“ReferenceError: **TextDecoder is not defined at matchLoop** (index.js:94:27(72))”}

I wonder what the recommended process is here, considering the example is outdated, there must be a recommended way to pass data objects as messages to the match loop.

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [January 6, 2022, 3:25pm UTC](https://forum.heroiclabs.com/t/parsing-match-loop-message-example-code-broken/2232/8 "2022-01-06T15:25:35Z")

</div>

@RunGrizzly we’ve updated the template: [GitHub - heroiclabs/nakama-project-template: An example project on how to set up and write custom server code in Nakama server.](https://github.com/heroiclabs/nakama-project-template) (see the changes [here](https://github.com/heroiclabs/nakama-project-template/pull/14/files)).

Please note the use of the runtime provided function `nk.binaryToString` to convert the match data to a string.

---

<div class="post-metadata">

**Author:** ![RunGrizzly](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/rungrizzly/32/918_2.png) [@RunGrizzly](https://forum.heroiclabs.com/u/RunGrizzly)\
**Post date:** [January 6, 2022, 3:27pm UTC](https://forum.heroiclabs.com/t/parsing-match-loop-message-example-code-broken/2232/9 "2022-01-06T15:27:01Z")

</div>

Perfect! Just the method I’ve been looking for.
