What the idiomatic way to handle "callbacks"? Such as when a module needs to communicate some events asynchronously to a "client" module?

Finally, I could just accept a pid and send it messages. This seems the most straightforward, but you don’t get as much compile-time checking and it seems to go against the “OTP” way of doing things.

How so? Because that is exactly what cast/2 and handle_cast/2 are for.

You may find GenServer docs: “handle_cast … should be used sparingly” Why? of interest.

when a module needs to communicate some events asynchronously to a “client” module.

Modules namespace code, processes communicate and processes can maintain state - though even that is open to abuse.

If you have a Google account you may benefit from downloading the free sample of Designing for Scalability with Erlang/OTP: Implement Robust, Fault-Tolerant Systems - it contains “Chapter 3: Behaviors” and most of “Chapter 4 Generic Servers” and being exposed to these on a more basic level may be beneficial towards “thinking in processes” (I know it’s in Erlang but you mentioned Cowboy so I figured it’s OK).

5 Likes