PHP Classes


Recommend this page to a friend!
  Classes of Jorge Castro  >  UsagiMQ  >  >  Download  
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: UsagiMQ
Manage message queues stored using Redis
Author: By
Last change:
Date: 2 years ago
Size: 4,888 bytes



Class file image Download



A simplest Message Queue by using Redis and PHP in a single box (one class)

Why i should use a Message Queue (MQ)?

Lets say the next example, a system where one system sends information to another, for example a web client and a web service.

If the webservice is doing a slow operation and its receiving the information of many clients at once then, sooner or later, the system could collapses or bottleneck.

For example, if every client uses 0.1 second to do the operation (receiving the information and storing in the database), and we have 1000 customers then, every operation could take 1.6 minutes.

Web Service

The solution is to add a Message Queue to the system. A Message Queue is only a server that stores messages/operations received by a PUBLISHER and later a SUBSCRIBER could executes.

For the same example, a PUBLISHER (former client) could uses 0.001 to call the MQ. Then the SUBSCRIBER could do all the operations, for example every hour/at night without worry if all the operations take many minutes or hours.


The MQ should be as fast as possible and be able to listen and store the request (envelope) of every publisher. However, the MQ is not doing the end operation, its similar to a email server. Later, a subscriber could read this information and process as correspond.

The drawback of this method is adding a delay, the process is not executed syncronously but asyncronously, and the PUBLISHER don't know really if the information was processed correctly by the SUBSCRIBER.


This library uses Redis. Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker.

Why UsagiMQ?

While there are many Message Queue in the market (including open source / freeware / commercial) but most of them are heavyweight. UsagiMQ is lightweight, it was developed thinking in customization. You could optimize and customize it for your needing, for example, changing the structure of envelope.

UsagiMQ lightweight:

  • One class, it requires Redis and nothing more.
  • < 500 lines of code.
  • Easy customization.

It requires a single file (UsagiMQ.php)

Envelope structure

id = the identified of the envelope (required). from = who send the envelope (optional) body = the content of the envelope (required). date = date when it was received. (required, generated) try = number of tries. (required, for use future, generated)

MQ Server

Who is listening and storing the envelope (request). It should be as fast as possible.


include "UsagiMQ.php";  
$usa=new UsagiMQ("",6379,1);
if ($usa->connected) {
  echo $usa->receive();
} else {
  echo "not connected";

Publisher (who sends the request/envelope)

A publisher requires to send a POST to the page. So it could be a CURL, Javascript, PHP, Java, C# and many more.

The url should be as follow: mq.php?id=ID&op=OP&from=FROM

while the body (content) should be sends as POST.

  • ID = is an identified of the message (required but not specifically should be unique)
  • OP = the operation to do. Its some sort of folder or category.
  • FROM = who is sending the message. It could be used for authentication.
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL,            "http://localhost/UsagiMQ/mq.php?id=200&op=INSERTCUSTOMER&from=SALESSYSTEM" );
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
   curl_setopt($ch, CURLOPT_POST,           1 );
   curl_setopt($ch, CURLOPT_POSTFIELDS,     "IT IS WHAT WE WANT TO SEND" );
   curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: text/plain'));
   $result=curl_exec ($ch);
   echo $result; // if returns OKI then the operation was successful. Otherwise, it an error.

Subscriber (local)

Its a local subscriber (has access to Redis). However, it could be used for to create a remote subscriber.

May be it could runs as a daemon / in a schedule task / or at request.


// its a local subscriber
include "UsagiMQ.php";

$usa=new UsagiMQ("",6379,1);
if (!$usa->connected) {
    echo "not connected";


foreach($listEnveloper as $id) {
    // here we process the envelope
    // and if its right then we could delete.



> $usa=new UsagiMQ($IPREDIS,$PORT,$DATABASE);

$IPREDIS indicates the IP of where is the redis server. $PORT indicates the REDIS port. $DATABASE (optional), indicates the database of redis (0 is the default value)


  • Error control / Log
  • Readme missing the command
  • Readme missing the PUBLISHER.
  • Proofreading