# Adding Lwt code to the Dream main loop

**URL:** https://discuss.ocaml.org/t/adding-lwt-code-to-the-dream-main-loop/9773
**Category:** Learning
**Tags:** lwt, postgresql, dream
**Created:** [May 3, 2022, 11:50am UTC](https://discuss.ocaml.org/t/adding-lwt-code-to-the-dream-main-loop/9773 "2022-05-03T11:50:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Haudegen](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/haudegen/32/716_2.png) [@Haudegen](https://discuss.ocaml.org/u/Haudegen)
#### Post date: [May 3, 2022, 11:50am UTC](https://discuss.ocaml.org/t/adding-lwt-code-to-the-dream-main-loop/9773/1 "2022-05-03T11:50:09Z")

</div>

I don’t know if this is the best forum to ask questions about the Dream web framework. Please point me to the correct place, if necessary.

I’d like to add some code to the Dream main loop. This code should not run in the context of the request / response cycle, but it would connect to a PostgreSQL database and listen to events via a listen / notify channel. Dream’s job would be to react to certain database notifications by sending messages over connected websockets.

I can’t find an obvious way to inject my Lwt code into the `Lwt_main.run` call in `http.ml`. Am I missing something?

I’m aware of the Git project `aantron/dream-serve` which, if I understand correctly, does a similar thing (sending websocket messages triggered by external events) by defining a custom Lwt\_engine. This approach seems to be sparsely documented by the Lwt project (I’m happy to be proven wrong).

If Dream doesn’t allow extending it’s main loop, I’ll have to use two separate processes for websocket and PostgreSQL connections respectively, which communicate via HTTP.

Do you have any pointers for me? Thank you!

---

<div class="post-metadata">

### Author: ![cemerick](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/cemerick/32/1383_2.png) [@cemerick](https://discuss.ocaml.org/u/cemerick)
#### Post date: [May 3, 2022, 1:23pm UTC](https://discuss.ocaml.org/t/adding-lwt-code-to-the-dream-main-loop/9773/2 "2022-05-03T13:23:06Z")

</div>

Prior to your call to `Dream.run` (or `Lwt_main.run @@ Dream.serve ...`, depending), just schedule your other concurrent background tasks via [`Lwt.async`](https://ocsigen.org/lwt/latest/api/Lwt#VALasync).

---

<div class="post-metadata">

### Author: ![Haudegen](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/haudegen/32/716_2.png) [@Haudegen](https://discuss.ocaml.org/u/Haudegen)
#### Post date: [May 3, 2022, 1:54pm UTC](https://discuss.ocaml.org/t/adding-lwt-code-to-the-dream-main-loop/9773/3 "2022-05-03T13:54:57Z")

</div>

This looks promising. Thank you.

> Lwt.async is misleadingly named.

Yep.

---

<div class="post-metadata">

### Author: ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)
#### Post date: [May 3, 2022, 2:41pm UTC](https://discuss.ocaml.org/t/adding-lwt-code-to-the-dream-main-loop/9773/4 "2022-05-03T14:41:29Z")

</div>

> [@Haudegen](#):
>
> This looks promising

I see what you did there. 👍🏼
