Adding Lwt code to the Dream main loop

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!

Prior to your call to Dream.run (or Lwt_main.run @@ Dream.serve ..., depending), just schedule your other concurrent background tasks via Lwt.async.

2 Likes

This looks promising. Thank you.

Lwt.async is misleadingly named.

Yep.

2 Likes

I see what you did there. :+1:t3:

6 Likes