# Does lwt provide unbounded channels?

**URL:** <https://discuss.ocaml.org/t/does-lwt-provide-unbounded-channels/12300>\
**Category:** Learning\
**Created:** [June 2, 2023, 9:41am UTC](https://discuss.ocaml.org/t/does-lwt-provide-unbounded-channels/12300 "2023-06-02T09:41:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [June 2, 2023, 9:41am UTC](https://discuss.ocaml.org/t/does-lwt-provide-unbounded-channels/12300/1 "2023-06-02T09:41:02Z")

</div>

If not, what is the closest thing it provides ?

1. I am looking at [Lwt manual](https://ocsigen.org/lwt/latest/manual/manual) and can not find an obvious match.

2. I am looking for something with approximately three functions:

```auto
create: 'a chan
push: 'a chan -> 'a -> unit
get: 'a chan -> 'a Lwt.t

```

create/push always succeeds  
get: if there is an elem, return it; otherwise, suspend current Lwt green thread, waking when element is pushed to channel

XY problem: converting JS .on\_message hook to channel like API

---

<div class="post-metadata">

**Author:** ![beajeanm](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/beajeanm/32/189_2.png) [@beajeanm](https://discuss.ocaml.org/u/beajeanm)\
**Post date:** [June 2, 2023, 10:19am UTC](https://discuss.ocaml.org/t/does-lwt-provide-unbounded-channels/12300/2 "2023-06-02T10:19:21Z")

</div>

Does channels here bear any ressemble to stdlib channels?  
If not, then I think [Module Lwt\_stream](https://ocsigen.org/lwt/2.7.0/api/Lwt_stream) is what you are looking for, isn’t it?

```auto
let chan, push = Lwt_stream.create ();;
let () = push (Some x);;
let delayed_x = Lwt_stream.get chan;;

```

I believe Lwt\_stream can be bounded, but aren’t by default.

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [June 2, 2023, 10:25am UTC](https://discuss.ocaml.org/t/does-lwt-provide-unbounded-channels/12300/3 "2023-06-02T10:25:04Z")

</div>

I think you are right.

I saw the from / from\_direct constructors … and moved on.

One more line and i would have seen the `create` constructor. 🙂

---

<div class="post-metadata">

**Author:** ![zbaylin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zbaylin/32/4047_2.png) [@zbaylin](https://discuss.ocaml.org/u/zbaylin)\
**Post date:** [June 2, 2023, 2:07pm UTC](https://discuss.ocaml.org/t/does-lwt-provide-unbounded-channels/12300/4 "2023-06-02T14:07:18Z")

</div>

There’s also [`Lwt_mvar`](https://ocsigen.org/lwt/latest/api/Lwt_mvar) which has a similar API:

- `val create_empty : unit -> 'a t`
- `val put : 'a t -> 'a -> unit Lwt.t`
- `val take : 'a t -> 'a Lwt.t`

The main difference here being that `put` will block if there’s already a value in the “mailbox”, which is why it returns a `unit Lwt.t`.
