PHPackages                             br0sk/yiiron - 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. [Caching](/categories/caching)
4. /
5. br0sk/yiiron

ActiveYii-extension[Caching](/categories/caching)

br0sk/yiiron
============

Yiiron is a Yii extension that integrates the services of iron.io in the Yii Famework

1.0.6(11y ago)71.2k[1 issues](https://github.com/br0sk/yiiron/issues)New BSD LicensePHP

Since Mar 24Pushed 11y ago3 watchersCompare

[ Source](https://github.com/br0sk/yiiron)[ Packagist](https://packagist.org/packages/br0sk/yiiron)[ Docs](http://www.yiiframework.com/extension/yiiron/)[ RSS](/packages/br0sk-yiiron/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (2)Dependencies (5)Versions (8)Used By (0)

Yii + iron.io = Yiiron
======================

[](#yii--ironio--yiiron)

What is Yiiron?
---------------

[](#what-is-yiiron)

Yiiron is a Yii extension that integrates the services of [iron.io](http://iron.io?rc=je1) in the [Yii Famework](http://www.yiiframework.com).

[Iron.io](http://iron.io?rc=je1) offers three services:

- **IronMQ** - A message Queue for the Cloud (10 Million free API Requests / month Unlimited Queues)
- **IronWorkers** - A worker platform that runs tasks in the background, in parallel, and at massive scale (200 Free Hours per month, that is over 6 hours per day, 50 concurrent Tasks, 25 Scheduled Jobs)
- **IronCache** - Key/Value Data Cache (offers: 100 MB storage and 10 Million API Requests /month)

Why should you use Yiiron?
--------------------------

[](#why-should-you-use-yiiron)

If you ask your self any of these questions you should probably try it:

- How can I easily integrate an MQ service in my Yii application?
- Hmm, these cronjobs are really draining my server. Should I spin up another server just to run the cronjobs?
- My app is starting to gain a lot of traction. I really need to start caching properly but I don’t want to install and manage [Memcached](http://memcached.org/) or a similar service. I wonder if someone can manage the cache for me?
- I really like the iron.io services but I don’t have time to develop a Yii component for it.
- My site is growing quickly and the background tasks I run can no longer keep up with being run in one thread. Is there a way of running my workers in parallel without installing and maintaining something like [gearman.org](http://gearman.org)
- I am on [AWS](http://aws.amazon.com/) and I want to use an MQ, workers or cache but I want someone else to manage scaling and management of those services.

Requirements
------------

[](#requirements)

- Your application needs to be hosted on AWS. This is not actually a requirement but all the [iron.io](http://iron.io?rc=je1) services are hosted on AWS and to get the full speed of the services and as little latency as possible your applications should be hosted on the AWS infrastructure. The services are based on REST so in theory you can use them from any computer that is connected to the internet and for instance adding data to IronMQ usually works fine with low latency in a cross cloud environment.

Resources
---------

[](#resources)

- GitHub page(for forks and trouble reports):
- Packagist page(for Composer download):
- iron.io dev center(for full details on the services including full API documentation) -

Getting Started
---------------

[](#getting-started)

If you don’t already have an [iron.io](http://iron.io?rc=je1) account please sign up for a free account [here](http://www.iron.io?rc=je1).

Go to the [hud.iron.io/dashboard](https://hud.iron.io/dashboard) and create a new project.

When the project is created click the key icon and take a note of the token and the project id.

Unzip `yiiron.zip` file that you downloaded from the [Yiiron Yii extension page](http://www.yiiframework.com/extension/yiiron) and put all the files in the extensions directory. It would look something like this: `/var/www/myapp/protected/extensions/yiiron`

**Note:** You can also install the the extension using Composer. It uses the new [Yii Composer installer](https://github.com/composer/installers/pull/61). Remember to set the composer flag for the extension to false if you used the zip file and set it to true if you have installed using composer. This is needed since the iron.io classes will be found in different locations based on how it was installed.

Add this to your config file (don't forget to add it to the console.php if you want to use the IronWorkers)

```
import'=>array('ext.yiiron.*',)

```

Add this to the component section...

```
'yiiron'=> array(
	'class' =>'EYiiron',
	'token'  => ‘your_iron_io_token’,
	'projectId'  => 'your_iron_io_project_id',
	'services'   => array('mq','worker',’cache’),
	'composer'	=> false,
	'workerFileCopyOptions' => array('exclude' => array('.git','.csv','.svn', '.zip', "/runtime", "/config")),
	'configFile' => 'config/console_ironworker.php',
	'stack' => 'php-5.5'
 	),

```

This should be all!

As you can see you can set the PHP version you want to use when the code is executed as an IronWorker. Find the valid versions [here](http://dev.iron.io/worker/reference/environment/#default_language_versions).

Now test it by adding this to one of the actions in a controller:

```
Yii::app()->yiiron->mqPostMessage("yii_demo", "First Value");

```

Load the action in a browser.

Now go to the back to [hud.iron.io/dashboard](https://hud.iron.io/dashboard) and click the MQ button next to the project you created. If everything is fine you should see a “Queues” tab. Click it!

You should now see your freshly created queue with one message added.

Congrats everything is working and you can start to use the iron.io services!

How to use the services
-----------------------

[](#how-to-use-the-services)

You can now call the different services since we have verified the connection is OK in “Getting started” above.

The iron.io PHP API was used to build Yiiron. All the public methods of the API's [github.com/iron-io/iron\\\_mq\\\_php](https://github.com/iron-io/iron_mq_php), [github.com/iron-io/iron\\\_worker\\\_php](https://github.com/iron-io/iron_worker_php), [github.com/iron-io/iron\\\_cache\\\_php](https://github.com/iron-io/iron_cache_php) have been wrapped in the Yiiron component.

To make use of the auto complete function in most editors I suggest that you initialise the Yiiron component once and save it in a local variable like this:

```
/**
 * @var $yiiron EYiiron The iron.io connector
 */
 $yiiron = Yii::app()->yiiron;

```

Doing so will give you access to auto complete for the variable `$yiiron`. This makes it very easy to find the wrapped API methods.

I have prefixed the wrapper methods with **mq**, **worker** and **cache**.

So to get all the methods for IronMQ just type:

```
$yiiron->mq

```

and auto complete should bring up all the methods starting with mq.

For the IronWorkers type:

```
$yiiron->worker

```

and auto complete should bring up all the methods starting with worker.

For the IronCache:

```
$yiiron->cache

```

and auto complete should bring up all the methods starting with cache.

**tip:** If you don't want to use my wrapper functions you can get a reference to each of the services directly like this.

```
$myWorker = Yii::app()->yiiron->getRawWorker();
$myCache = Yii::app()->yiiron->getRawCache();
$myMq = Yii::app()->yiiron->getRawMq();

```

How to use IronMQ
-----------------

[](#how-to-use-ironmq)

To use IronMQ you can simply call all the methods starting with mq. Here is an example of the most common scenario for an generic MQ (put/get/delete).

```
/**
 * @var $yiiron EYiiron The iron.io connector
 */
$yiiron = Yii::app()->yiiron;

//Adds a message to the queue
$yiiron->mqPostMessage("yii_demo", "First Value");

//Get the message from the queue and reserve it for 10 seconds(default is 60 seconds)
$message = $yiiron->mqGetMessage("yii_demo",10);

//Print the body and id of the message to the screen
echo("Message id=".$message->id." message body=". $message->body);

//The message has been consumed now remove it from the queue so it is not put back on the queue when the time out of 10 seconds have passed
$yiiron->mqDeleteMessage("yii_demo", $message_get_after_release->id);

```

For more examples check the unit test classes.

How to use IronWorkers
----------------------

[](#how-to-use-ironworkers)

You can use the IronWorkers in two ways. The first way is to simply use the wrapped API directly. You probably want to do that if you are writing your own integration on top of Yiiron or you have a very specific use case.

The second and preferred way of using IronWorkers is to run the worker as a Yii command line application ([Yii CLI documentation](http://www.yiiframework.com/doc/guide/1.1/en/topics.console)). By extending the class **EIronWorkersCommand** instead of **CConsoleCommand** directly you will now get access to extra functionality that will allow you to run your command line actions directly as an IronWorker without making any changes to the the Yii API.

This basically means that you can run your currently existing command actions as IronWorkers only by changing the class that your command extends.

### Here is a practical example step by step.

[](#here-is-a-practical-example-step-by-step)

Create a separate config file called **console\_ironworker.php** and place it in the config folder of your application. Here is a simple version of this file.

```
