# Cannot send Uint8Array to Client

**URL:** https://forum.heroiclabs.com/t/cannot-send-uint8array-to-client/2834
**Category:** Runtime Framework
**Tags:** typescript, server-framework
**Created:** [July 14, 2022, 6:41pm UTC](https://forum.heroiclabs.com/t/cannot-send-uint8array-to-client/2834 "2022-07-14T18:41:31Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![JayArrowz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/jayarrowz/32/1228_2.png) [@JayArrowz](https://forum.heroiclabs.com/u/JayArrowz)
#### Post date: [July 14, 2022, 6:41pm UTC](https://forum.heroiclabs.com/t/cannot-send-uint8array-to-client/2834/1 "2022-07-14T18:41:31Z")

</div>

Hi,

I am using the server to send messages to client. Specifically i am using the `this.matchDispatcher.broadcastMessage`

```auto
        /**
         * Broadcast a message to match presences.
         *
         * @param opcode - Numeric message op code.
         * @param data - Opt. Data payload string, or null.
         * @param presences - Opt. List of presences (a subset of match participants) to use as message targets, or null to send to the whole match. Defaults to null.
         * @param sender - Opt. A presence to tag on the message as the 'sender', or null.
         * @param reliable - Opt. Broadcast the message with delivery guarantees or not. Defaults to true.
         * @throws {TypeError, GoError}
         */
        broadcastMessage(opcode: number, data?: Uint8Array | string | null, presences?: Presence[] | null, sender?: Presence | null, reliable?: boolean): void;

```

However when i use this where `data` is a UInt8Array i get this error:

```auto
{"level":"error","ts":"2022-07-14T18:38:23.452Z","caller":"server/runtime_javascript_logger.go:94","msg":"Error%!(EXTRA string=TypeError\n\tat github.com/heroiclabs/nakama/v3/server.(*RuntimeJavaScriptMatchCore).broadcastMessage.func1 (native)\n\tat index.js:7791:76(61)\n\tat github.com/dop251/goja.(*Runtime).boundCallable.func1 (native)\n\tat index.js:8550:45(62)\n\tat github.com/dop251/goja.(*Runtime).boundCallable.func1 (native)\n\tat matchLoop (index.js:8792:63(32))\n\tat native\n)","mid":"506c5ae9-9b7f-49d3-a0a3-87926b5e77a8"}

```

Here is the transpiled JS code it is erroring on:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/heroiclabs/original/2X/4/443aefac72f5d93e508a295dd03724ec37d97e69.png)

Here is the TS code:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/heroiclabs/original/2X/b/b5a12baad30901d4276eb5aac4909005336e4f44.jpeg)

It works if i use strings like this:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/heroiclabs/original/2X/3/3e3be6f48ea336273bdb55e86f887a6c838ff0ce.png)

Is it not possible to send bytes through the socket directly?

---

<div class="post-metadata">

### Author: ![flavio](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/flavio/32/2161_2.png) [@flavio](https://forum.heroiclabs.com/u/flavio)
#### Post date: [July 15, 2022, 9:20am UTC](https://forum.heroiclabs.com/t/cannot-send-uint8array-to-client/2834/2 "2022-07-15T09:20:03Z")

</div>

Hi JayArrow

It seems to be a problem on the Typescript runtime binding to the underlying Go code that we need to investigate and improve. Meanwhile, you will have to convert the data to string as you said.

Thank you for pointing it out.

---

<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: [July 15, 2022, 9:40am UTC](https://forum.heroiclabs.com/t/cannot-send-uint8array-to-client/2834/3 "2022-07-15T09:40:58Z")

</div>

@JayArrowz what version of the server are you using?

---

<div class="post-metadata">

### Author: ![JayArrowz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/jayarrowz/32/1228_2.png) [@JayArrowz](https://forum.heroiclabs.com/u/JayArrowz)
#### Post date: [July 15, 2022, 5:10pm UTC](https://forum.heroiclabs.com/t/cannot-send-uint8array-to-client/2834/4 "2022-07-15T17:10:53Z")

</div>

Hi @flavio @sesposito thanks for the quick responses.

My server version is as follows:

```auto
FROM registry.heroiclabs.com/heroiclabs/nakama:3.12.0

```

My Nakama Runtime Dep:

```auto
    "nakama-runtime": "git+https://github.com/heroiclabs/nakama-common.git",

```

Rollup config incase you need it:

```auto
import resolve, { nodeResolve } from '@rollup/plugin-node-resolve';
import commonJS from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import babel from '@rollup/plugin-babel';
import typescript from '@rollup/plugin-typescript';
import pkg from './package.json';
import multiInput from 'rollup-plugin-multi-input';
import nodePolyfills from 'rollup-plugin-node-polyfills';

const extensions = ['.mjs', '.js', '.ts', '.json'];

export default {
    input: "./src/index.ts",
    external: ['nakama-runtime'],
    plugins: [
        // Allows node_modules resolution
        
       // nodeResolve(),
        nodePolyfills(),
        resolve({ extensions }),
        // Compile TypeScript
        typescript({
            noImplicitReturns: true,
            moduleResolution: "node",
            experimentalDecorators: true,
            esModuleInterop: true,
            noUnusedLocals: false,
            noUnusedParameters: false,
            removeComments: true,
            target: "es5",
            module: "ESNext",
            strict: false,
            downlevelIteration: true
        }),

        json(),

        // Resolve CommonJS modules
        commonJS({ extensions }),
        
    ],
    output: {
        dir: 'build',
    },
}

```

For now i will convert it to a string, but ideally when this is fixed we can remove this conversion. Thanks 🙂

---

<div class="post-metadata">

### Author: ![JayArrowz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/jayarrowz/32/1228_2.png) [@JayArrowz](https://forum.heroiclabs.com/u/JayArrowz)
#### Post date: [July 16, 2022, 3:22pm UTC](https://forum.heroiclabs.com/t/cannot-send-uint8array-to-client/2834/5 "2022-07-16T15:22:50Z")

</div>

The downside to also doing text decoding is atleast four times the bytes are sent over the wire. This is not ideal :.

The TextDecoder i am using is this one:  
[text-encoding-polyfill - npm (npmjs.com)](https://www.npmjs.com/package/text-encoding-polyfill)  
I have tried both ascii and utf8 options.

What server side sends:

Opcode 81 buffer normal size is 4

```auto
game-server-nakama-1 | {"level":"info","ts":"2022-07-16T15:18:19.842Z","caller":"server/runtime_javascript_logger.go:74","msg":"Sending Var Message opcode: 81 size: 4","mid":"67f029da-d50d-458b-8e28-685e1c1d0f05"}

```

When buffer is text decoded and sent the size becomes 6:

````auto
game-server-nakama-1 | {"level":"info","ts":"2022-07-16T15:18:19.843Z","caller":"server/runtime_javascript_logger.go:74","msg":"\u0000\u0000È‘\u0000\u0000 variable buffer LEN %!s(int64=6)","mid":"67f029da-d50d-458b-8e28-685e1c1d0f05"}```

````

Server sends Opcode 64 originally 2 bytes:

```auto
game-server-nakama-1 | {"level":"info","ts":"2022-07-16T15:14:48.222Z","caller":"server/runtime_javascript_logger.go:74","msg":"Sending Var Message opcode: 64 size: 2","mid":"56393fdf-7632-4ff6-b929-689bd66f8de1"}   

```

buffer decoded Opcode 64 becomes 16 bytes:

```auto
game-server-nakama-1 | {"level":"info","ts":"2022-07-16T15:14:48.222Z","caller":"server/runtime_javascript_logger.go:74","msg":"ÎN\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 fixed buffer LEN %!s(int64=16)","mid":"56393fdf-7632-4ff6-b929-689bd66f8de1"}

```

Client gets back (mismatch something is happening on the golang side of things to cause this?):

```auto
Opcode: 64
Data: [195,142,78,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Opcode: 81
Data: [0,0,195,136,226,128,152,0,0]

```

String.fromCharCode seems to produce the same results of the bytes inflating in size

Edit:  
Have figured this out, hex encoding works best

---

<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: [July 18, 2022, 9:57am UTC](https://forum.heroiclabs.com/t/cannot-send-uint8array-to-client/2834/6 "2022-07-18T09:57:00Z")

</div>

@JayArrowz we’ve updated the TypeScript definitions, could you please try to send your data in the `broadcastMessage` function as an `ArrayBuffer` instead and let us know how that goes?

---

<div class="post-metadata">

### Author: ![JayArrowz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/jayarrowz/32/1228_2.png) [@JayArrowz](https://forum.heroiclabs.com/u/JayArrowz)
#### Post date: [July 18, 2022, 8:13pm UTC](https://forum.heroiclabs.com/t/cannot-send-uint8array-to-client/2834/7 "2022-07-18T20:13:14Z")

</div>

Can confirm this has been fixed and accepting array buffer now, thank you 😃
