Implement prepare_messages/2

Learn to receive a list of messages through the prepare_messages callback function.

When it comes to preloading data, the prepare_messages/2 callback is here to help.

The prepare_messages/2 callback function

It’s very common to have to do some work in bulk when receiving messages. For example, we may want to fetch or preload information from the database. Rather than doing this in handle_message/3 for each message, we can run a single database query from prepare_messages/2.

The prepare_messages/2 callback runs before handle_message/3 and receives a list of messages. This means you can iterate over the list, look at the data, and update it with more information. It also receives a user-defined context as the second argument, which we’ll ignore.

Implementation

Let’s implement prepare_messages/2 in BookingsPipeline:

Get hands-on with 1200+ tech skills courses.