|
| 1 | +# Redis transport |
| 2 | + |
| 3 | +The transport uses [Redis](https://redis.io/) as a message broker. |
| 4 | +It creates a collection (a queue or topic) there. Pushes messages to the tail of the collection and pops from the head. |
| 5 | +The transport works with [phpredis](https://github.com/phpredis/phpredis) php extension or [predis](https://github.com/nrk/predis) library. |
| 6 | +Make sure you installed either of them |
| 7 | + |
| 8 | +**Limitations** It works only in auto ack mode hence If consumer crashes the message is lost. |
| 9 | + |
| 10 | +* [Installation](#installation) |
| 11 | +* [Create context](#create-context) |
| 12 | +* [Send message to topic](#send-message-to-topic) |
| 13 | +* [Send message to queue](#send-message-to-queue) |
| 14 | +* [Consume message](#consume-message) |
| 15 | +* [Delete queue (purge messages)](#delete-queue-purge-messages) |
| 16 | +* [Delete topic (purge messages)](#delete-topic-purge-messages) |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +* With php redis extension: |
| 21 | + |
| 22 | +```bash |
| 23 | +$ apt-get install php-redis |
| 24 | +$ composer require enqueue/redis |
| 25 | +``` |
| 26 | + |
| 27 | +* With predis library: |
| 28 | + |
| 29 | +```bash |
| 30 | +$ composer require enqueue/redis predis/predis:^1 |
| 31 | +``` |
| 32 | + |
| 33 | +## Create context |
| 34 | + |
| 35 | +* With php redis extension: |
| 36 | + |
| 37 | +```php |
| 38 | +<?php |
| 39 | +use Enqueue\Redis\RedisConnectionFactory; |
| 40 | + |
| 41 | +$connectionFactory = new RedisConnectionFactory([ |
| 42 | + 'host' => 'localhost', |
| 43 | + 'port' => 6379, |
| 44 | + 'vendor' => 'phpredis', |
| 45 | +]); |
| 46 | + |
| 47 | +$psrContext = $connectionFactory->createContext(); |
| 48 | +``` |
| 49 | + |
| 50 | +* With predis library: |
| 51 | + |
| 52 | +```php |
| 53 | +<?php |
| 54 | +use Enqueue\Redis\RedisConnectionFactory; |
| 55 | + |
| 56 | +$connectionFactory = new RedisConnectionFactory([ |
| 57 | + 'host' => 'localhost', |
| 58 | + 'port' => 6379, |
| 59 | + 'vendor' => 'predis', |
| 60 | +]); |
| 61 | + |
| 62 | +$psrContext = $connectionFactory->createContext(); |
| 63 | +``` |
| 64 | + |
| 65 | +## Send message to topic |
| 66 | + |
| 67 | +```php |
| 68 | +<?php |
| 69 | +/** @var \Enqueue\Redis\RedisContext $psrContext */ |
| 70 | + |
| 71 | +$fooTopic = $psrContext->createTopic('aTopic'); |
| 72 | +$message = $psrContext->createMessage('Hello world!'); |
| 73 | + |
| 74 | +$psrContext->createProducer()->send($fooTopic, $message); |
| 75 | +``` |
| 76 | + |
| 77 | +## Send message to queue |
| 78 | + |
| 79 | +```php |
| 80 | +<?php |
| 81 | +/** @var \Enqueue\Redis\RedisContext $psrContext */ |
| 82 | + |
| 83 | +$fooQueue = $psrContext->createQueue('aQueue'); |
| 84 | +$message = $psrContext->createMessage('Hello world!'); |
| 85 | + |
| 86 | +$psrContext->createProducer()->send($fooQueue, $message); |
| 87 | +``` |
| 88 | + |
| 89 | +## Consume message: |
| 90 | + |
| 91 | +```php |
| 92 | +<?php |
| 93 | +/** @var \Enqueue\Redis\RedisContext $psrContext */ |
| 94 | + |
| 95 | +$fooQueue = $psrContext->createQueue('aQueue'); |
| 96 | +$consumer = $psrContext->createConsumer($fooQueue); |
| 97 | + |
| 98 | +$message = $consumer->receive(); |
| 99 | + |
| 100 | +// process a message |
| 101 | +``` |
| 102 | + |
| 103 | +## Delete queue (purge messages): |
| 104 | + |
| 105 | +```php |
| 106 | +<?php |
| 107 | +/** @var \Enqueue\Redis\RedisContext $psrContext */ |
| 108 | + |
| 109 | +$fooQueue = $psrContext->createQueue('aQueue'); |
| 110 | + |
| 111 | +$psrContext->deleteQueue($fooQueue); |
| 112 | +``` |
| 113 | + |
| 114 | +## Delete topic (purge messages): |
| 115 | + |
| 116 | +```php |
| 117 | +<?php |
| 118 | +/** @var \Enqueue\Redis\RedisContext $psrContext */ |
| 119 | + |
| 120 | +$fooTopic = $psrContext->createTopic('aTopic'); |
| 121 | + |
| 122 | +$psrContext->deleteTopic($fooTopic); |
| 123 | +``` |
| 124 | + |
| 125 | +[back to index](index.md) |
0 commit comments