9951 explained code solutions for 126 technologies


predisHow do I use a Redis message queue with PHP?


Using Redis message queue with PHP is quite easy.

  1. First, install the Predis library:
composer require predis/predis
  1. Then, create an instance of the client:
$redis = new Predis\Client();
  1. To add an item to the queue:
$redis->rpush('messages', 'Hello, world!');
  1. To retrieve an item from the queue:
$message = $redis->lpop('messages');
// Output: Hello, world!
  1. To check the length of the queue:
$queue_length = $redis->llen('messages');
  1. To delete the queue:
$redis->del('messages');
  1. To close the connection:
$redis->quit();

Edit this code on GitHub