Schedule an Alarm with handle_cast

The handle_cast callback is an asynchronous message, a one-way message. Use it when you don’t need a reply from a server, and you want to send the message through the GenServer API. We’ll amend this advice a bit at the end of the chapter. The client calls the server and does not bother waiting for a response. We’ll use a cast message to implement the schedule message for our timer.

Scheduling a new alarm will require a name, the duration for a timer, and a function to call when the alarm triggers. We’ll implement the cast with a handle_cast.

Implement the handle_cast Callback

Key the following code into the server module. Note that our callback has a similar shape to the handle_info callback:

 def​ handle_cast({​ ...

Get Programmer Passport: OTP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.