PHP – How to display messages from an array (API) with a timeout between them but push new incoming messages at the end of the queue [duplicate]

Let’s say I have an API response as an array.

$data = [
  ['id' => 1, 'message' => 'Message 1', 'displayed' => false ],
  ['id' => 2, 'message' => 'Message 2', 'displayed' => false ],
]

I want to display the messages to the frontend with a sleep between them.

But this data is real-time data so that new messages can be inserted from external systems. I want those new messages to be inserted at the end of the queue (before undisplayed messages).

How can I create a function that displays the $currentMessage?