iron-io/iron_mq
PHP client for IronMQ (API v3), an elastic cloud message queue. Composer installable and PSR-4 namespaced (v4.* recommended; Laravel 5.1/5.2 compatible). Send/receive messages and manage queues with Iron.io credentials.
Installation
composer require iron-io/iron_mq
Add your IronMQ credentials to .env:
IRONMQ_PROJECT_ID=your_project_id
IRONMQ_TOKEN=your_token
First Use Case: Publishing a Message
use IronMQ\IronMQ;
$mq = new IronMQ(config('ironmq.project_id'), config('ironmq.token'));
$mq->post('queue_name', 'Hello, IronMQ!');
Where to Look First
examples/ in the GitHub repo for quick-start snippetssrc/IronMQ.php for core class methodsProducer Pattern
$mq = new IronMQ(config('ironmq.project_id'), config('ironmq.token'));
$mq->post('orders', json_encode(['order_id' => 123, 'status' => 'pending']));
Consumer Pattern (Blocking)
$mq = new IronMQ(config('ironmq.project_id'), config('ironmq.token'));
$message = $mq->peek('orders'); // Non-blocking
if ($message) {
$mq->pop('orders', $message->id); // Remove from queue
processOrder($message->body);
}
Delayed Messages
$mq->post('delayed_queue', 'Process later', 30); // Delay in seconds
Error Handling & Retries
try {
$mq->post('critical_queue', 'Important data');
} catch (IronMQException $e) {
// Log and retry with exponential backoff
sleep(2 ** $attempt);
}
Laravel Queue Integration
Extend Illuminate\Queue\Queue to wrap IronMQ calls:
class IronMQQueue extends Queue implements QueueContract {
public function push($job, $data, $queue = null) {
$mq = new IronMQ(config('ironmq.project_id'), config('ironmq.token'));
$mq->post($queue ?: 'default', $data);
}
}
Event-Driven Architecture Use IronMQ for async event publishing:
event(new OrderCreated($order));
// In a separate worker:
$mq->peek('order_events')->then(function ($msg) {
$event = json_decode($msg->body);
// Handle event
});
Monitoring & Metrics Track queue stats via:
$stats = $mq->stats('queue_name');
logger()->info("Queue length: {$stats['messages']}");
Connection Timeouts
$retryAfter = $e->getRetryAfter() ?: 1;
sleep($retryAfter + rand(0, 100) / 100);
Message Size Limits
part_1, part_2).Visibility Timeouts
$mq->pop('queue', $msgId, 300); // 5-minute visibility
Token Expiry
ironmq token create --project-id=your_id --description="PHP Worker Token"
Enable Debug Mode
$mq = new IronMQ($projectId, $token, ['debug' => true]);
Logs HTTP requests/responses to stderr.
Common HTTP Errors
| Error Code | Cause | Fix |
|---|---|---|
| 401 | Invalid token/project ID | Verify .env credentials |
| 404 | Queue doesn’t exist | Create queue via dashboard |
| 429 | Rate limited | Add delays between requests |
Custom Middleware Add pre/post-processing to messages:
$mq->setMiddleware(function ($msg) {
$msg->body = gzdecode($msg->body); // Auto-decompress
});
Webhook Integration Use IronMQ’s webhooks to trigger external actions:
$mq->post('webhook_queue', 'Trigger webhook', 0, [
'webhook_url' => 'https://your-app.com/hook'
]);
Queue Prioritization Simulate priority queues with suffixes:
$mq->post('orders_high', $data); // High-priority
$mq->post('orders_low', $data); // Low-priority
Local Testing Use the IronMQ Sandbox for testing:
IRONMQ_PROJECT_ID=sandbox
IRONMQ_TOKEN=sandbox_token
How can I help you explore Laravel packages today?