PHPackages                             eftec/usagimq - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. eftec/usagimq

ActiveLibrary[Templating &amp; Views](/categories/templating)

eftec/usagimq
=============

The standalone version Blade Template Engine from Laravel in a single php file

1.4(7y ago)112.9k3MITPHPPHP &gt;=5.4

Since Nov 12Pushed 7y ago2 watchersCompare

[ Source](https://github.com/EFTEC/UsagiMQ)[ Packagist](https://packagist.org/packages/eftec/usagimq)[ Docs](https://github.com/EFTEC/UsagiMQ)[ RSS](/packages/eftec-usagimq/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (3)DependenciesVersions (4)Used By (0)

[![logo](visio/logo.png "logo")](visio/logo.png)

UsagiMQ
=======

[](#usagimq)

A minimalist (less than 500 lines of code) Message Queue by using Redis and PHP in a single box (one class)

Why I should use a Message Queue (MQ)?
--------------------------------------

[](#why-i-should-use-a-message-queue-mq)

Let’s 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](visio/WebService.jpg "Web Service")](visio/WebService.jpg)

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 execute.

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.

[![MQ](visio/MQ.jpg "MQ")](visio/MQ.jpg)

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 it adds a delay, the process is not executed synchronously but asynchronously, and the PUBLISHER don't know really if the information was processed correctly by the SUBSCRIBER.

Considerations
--------------

[](#considerations)

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?
------------

[](#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.

It is a software based solution that could be **re-programmed** as you want to. Instead, most MQ are **configured-based**.

For example, you could create easily a ORCHESTATION, CHOREOGRAPHY or A CLUSTER via software.

UsagiMQ lightweight:

- One class, it requires Redis and nothing more.
- &lt; 500 lines of code.
- Easy customization.
- **It requires a single file (UsagiMQ.php)**

Is it scalable?
---------------

[](#is-it-scalable)

Yes, but it requires a LOAD BALANCER that you could program or use a software or hardware solution. Its your call.

Envelope structure
------------------

[](#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
---------

[](#mq-server)

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

Example:

```
include "UsagiMQ.php";
$usa=new UsagiMQ("127.0.0.1",6379,1);
if ($usa->connected) {
    $info=$usa->receive();
    if ($info=='NO INFO') {
        $usa->webUI(); // if not information is send, then it opens the UI.  It is optional
    } else {
        echo $info; // show the result.
    }
} else {
echo "not connected";
}

```

Example MQ server that calls a worker (subscriber). See example : mqwithworker.php

### Publisher (who sends the request/envelope)

[](#publisher-who-sends-the-requestenvelope)

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**&amp;op=**OP**&amp;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.

```
