# Pass elm/bytes through a port?

**URL:** <https://discourse.elm-lang.org/t/pass-elm-bytes-through-a-port/3874>\
**Category:** Learn\
**Created:** [June 25, 2019, 10:13am UTC](https://discourse.elm-lang.org/t/pass-elm-bytes-through-a-port/3874 "2019-06-25T10:13:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [June 25, 2019, 10:13am UTC](https://discourse.elm-lang.org/t/pass-elm-bytes-through-a-port/3874/1 "2019-06-25T10:13:34Z")

</div>

I take it there is no way to send `Bytes` through a port:

```
38| port sendBytes : Bytes.Bytes -> Cmd msg
         ^^^^^^^^^
I cannot handle that. The types that CAN flow in and out of Elm include:

    Ints, Floats, Bools, Strings, Maybes, Lists, Arrays, tuples, records, and
    JSON values.

```

These functions could be added to `elm-json`:

```
-- In Json.Decode

bytes : Decoder Bytes

-- In Json.Encode

bytes : Bytes -> Value 

```

They would map onto the underlying `DataView` or `ArrayBuffer` on the JS side.

Can anyone see reasons why allowing this would be a bad idea? Would this be a worthwhile addition to the platform?

---

<div class="post-metadata">

**Author:** ![folkertdev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/folkertdev/32/133_2.png) [@folkertdev](https://discourse.elm-lang.org/u/folkertdev)\
**Post date:** [June 25, 2019, 10:27am UTC](https://discourse.elm-lang.org/t/pass-elm-bytes-through-a-port/3874/2 "2019-06-25T10:27:20Z")

</div>

In general I think this should be added. Converting to anything else to pass it over a port negates any efficiency benefits of using bytes. There have been several cases where people ran into this limitation.

I’m not sure if adding to `elm/json` makes sense (creates a dependency to `elm/bytes` for `elm/json`).

A problem is that the `elm-bytes` package currently uses a `DataView` under the hood, which is a view on a byte array (i.e. elements are 8 bits wide). Therefore it is not obvious what should happen to a typed array with larger elements (e.g. Int32Array). Silently converting seems like a bad idea, but really any kind of conversion is bad because the whole goal of these data types is to be efficient. So either we don’t support typed arrays for the moment, or we need to be able to efficiently get them into the elm world.

We could also only support ArrayBuffer/DataView. A little against the elm spirit of finding a good general solution (that covers all low-level array types), but it would be practical.

---

<div class="post-metadata">

**Author:** ![joecorkerton](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/joecorkerton/32/1346_2.png) [@joecorkerton](https://discourse.elm-lang.org/u/joecorkerton)\
**Post date:** [June 25, 2019, 3:20pm UTC](https://discourse.elm-lang.org/t/pass-elm-bytes-through-a-port/3874/3 "2019-06-25T15:20:42Z")

</div>

I ran into this issue recently, I was using a js package to parse a binary file format.

I worked around it by converting the bytes into a base64 string and passing that through the port, but I agree that it would have been nice to pass the bytes straight through the port

---

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [June 25, 2019, 5:59pm UTC](https://discourse.elm-lang.org/t/pass-elm-bytes-through-a-port/3874/4 "2019-06-25T17:59:21Z")

</div>

It has also been suggested that passing through as a list of ints might be more performant than base64. I will likely use one of these solutions despite being quite horrible. Interested to understand if we can have a better way though.

---

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [June 26, 2019, 9:09am UTC](https://discourse.elm-lang.org/t/pass-elm-bytes-through-a-port/3874/5 "2019-06-26T09:09:51Z")

</div>

> [@folkertdev](#):
>
> I’m not sure if adding to `elm/json` makes sense (creates a dependency to `elm/bytes` for `elm/json` ).

Where would you put these functions if not in `elm/json`? If `Bytes` is now a fundamental data type in Elm, I don’t really see having it as a common dependency is much of an issue. Perhaps `Bytes` should even migrate into `elm/core`?

> [@folkertdev](#):
>
> A problem is that the `elm-bytes` package currently uses a `DataView` under the hood, which is a view on a byte array (i.e. elements are 8 bits wide). Therefore it is not obvious what should happen to a typed array with larger elements (e.g. Int32Array).

I think here you are talking about the case where on the JS side you have an `Int32Array` that you are passing into a port. When wrapped by `DataView` this gets presented as an 8-bit byte array. I also notice that `elm/bytes` understands what the host endianness is. So if you were to call `Bytes.Decode.signedInt32` on these `Bytes` it should correctly decode to the right integer value.

---

<div class="post-metadata">

**Author:** ![jreusch](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@jreusch](https://discourse.elm-lang.org/u/jreusch)\
**Post date:** [June 26, 2019, 10:11am UTC](https://discourse.elm-lang.org/t/pass-elm-bytes-through-a-port/3874/6 "2019-06-26T10:11:06Z")

</div>

> [@rupert](#):
>
> If `Bytes` is now a fundamental data type in Elm, I don’t really see having it as a common dependency is much of an issue. Perhaps `Bytes` should even migrate into `elm/core` ?

The same argument could be made about `elm/json`. In fact, you cannot compile an Elm program without referencing `elm/json` as a (indirect) dependency, even though `elm/core` doesn’t depend on it.

The reason `elm/json` is such a fundamental package is because flags and ports can automatically generate code to decode/encode Javascript objects to Elm values and vice-versa. However, Javascript objects are infinitely more powerful than JSON. This thread for example asks for the ability to pass Bytes to Javascript, which have no native representation in JSON, but could be represented as a Javascript object (e.g. the native DataView or some TypedArray). Before `elm/file` existed, we (at work) passed raw `Json.Encode.Value`s from the event handler through the port to process the Javascript `File` object there.

So, maybe we need to seperate communication with `JSON` things (like Web APIs) and communication with the host environment (basically the browser, so Javascript) more?

---

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [June 26, 2019, 10:50am UTC](https://discourse.elm-lang.org/t/pass-elm-bytes-through-a-port/3874/7 "2019-06-26T10:50:43Z")

</div>

> [@jreusch](#):
>
> So, maybe we need to seperate communication with `JSON` things (like Web APIs) and communication with the host environment (basically the browser, so Javascript) more?

Its a good point. If you really did want to put binary data in JSON format for sending over a Web API, you would most likely base64 encode it. This is already perfectly possibly with the existing APIs.

Perhaps what is needed is simply to allow `Bytes` to be passed over ports, but not to bother with encoders/decoders that turn them into `Value`s representing javascript objects. The `Bytes` would have to be top-level arguments to the port functions and not embedded deeper as a field of some object.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [July 6, 2019, 10:50am UTC](https://discourse.elm-lang.org/t/pass-elm-bytes-through-a-port/3874/8 "2019-07-06T10:50:44Z")

</div>

Closed on July 6, 2019, 10:50am UTC

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
