|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of RedisClient. |
| 4 | + * git: https://github.com/cheprasov/php-redis-client |
| 5 | + * |
| 6 | + * (C) Alexander Cheprasov <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +/** |
| 13 | + * Using PubSub |
| 14 | + */ |
| 15 | + |
| 16 | +namespace Examples; |
| 17 | + |
| 18 | +require (dirname(__DIR__).'/src/autoloader.php'); |
| 19 | + |
| 20 | +use RedisClient\RedisClient; |
| 21 | + |
| 22 | +// Example 1. subscribe |
| 23 | + |
| 24 | +$Redis = new RedisClient([ |
| 25 | + 'timeout' => 0 // for waiting answer infinitely |
| 26 | +]); |
| 27 | + |
| 28 | +$Redis->subscribe('channel.name', function($type, $channel, $message) { |
| 29 | + // This function will be called on subscribe and on message |
| 30 | + if ($type === 'subscribe') { |
| 31 | + // Note, if $type === 'subscribe' |
| 32 | + // then $channel = <channel-name> |
| 33 | + // and $message = <count of subsribers> |
| 34 | + echo 'Subscribed to channel <', $channel, '>', PHP_EOL; |
| 35 | + } elseif ($type === 'message') { |
| 36 | + echo 'Message <', $message, '> from channel <', $channel, '>', PHP_EOL; |
| 37 | + if ($message === 'quit') { |
| 38 | + // return <false> for unsubscribe and exit |
| 39 | + return false; |
| 40 | + } |
| 41 | + } |
| 42 | + // return <true> for to wait next message |
| 43 | + return true; |
| 44 | +}); |
| 45 | + |
| 46 | +// Example 2. subscribe with timeout |
| 47 | + |
| 48 | +$Redis = new RedisClient([ |
| 49 | + 'timeout' => 10 // for waiting answer for 10 seconds |
| 50 | +]); |
| 51 | + |
| 52 | +$Redis->subscribe('channel.name', function($type, $channel, $message) { |
| 53 | + // This function will be called on subscribe, on message and on timeout |
| 54 | + if (!isset($type)) { |
| 55 | + echo 'No any message for 10 second... exit'. PHP_EOL; |
| 56 | + // return <false> for unsubscribe and exit |
| 57 | + return false; |
| 58 | + } |
| 59 | + // This function will be called on subscribe and on message |
| 60 | + if ($type === 'subscribe') { |
| 61 | + // Note, if $type === 'subscribe' |
| 62 | + // then $channel = <channel-name> |
| 63 | + // and $message = <count of subsribers> |
| 64 | + echo 'Subscribed to channel <', $channel, '>', PHP_EOL; |
| 65 | + } elseif ($type === 'message') { |
| 66 | + echo 'Message <', $message, '> from channel <', $channel, '>', PHP_EOL; |
| 67 | + } |
| 68 | + // return <true> for to wait next message |
| 69 | + return true; |
| 70 | +}); |
| 71 | + |
| 72 | +// Example 3. subscribe with external timeout |
| 73 | + |
| 74 | +$Redis = new RedisClient([ |
| 75 | + 'timeout' => 0.25 // for waiting answer for 0.25 seconds |
| 76 | +]); |
| 77 | + |
| 78 | +$time = microtime(true); |
| 79 | +$Redis->subscribe(['channel.name1', 'channel.name2'], function($type, $channel, $message) use ($time) { |
| 80 | + // This function will be called on subscribe, on message and on timeout (every 0.25 seconds) |
| 81 | + if (!isset($type)) { |
| 82 | + echo 'No any message for 0.25 seconds...'. PHP_EOL; |
| 83 | + |
| 84 | + // Will unsubscribe and exit after 10 seconds of timeout |
| 85 | + if (microtime(true) - $time > 10) { |
| 86 | + // return <false> for unsubscribe and exit |
| 87 | + return false; |
| 88 | + } |
| 89 | + } |
| 90 | + // This function will be called on subscribe and on message |
| 91 | + if ($type === 'subscribe') { |
| 92 | + // Note, if $type === 'subscribe' |
| 93 | + // then $channel = <channel-name> |
| 94 | + // and $message = <count of subsribers> |
| 95 | + echo 'Subscribed to channel <', $channel, '>', PHP_EOL; |
| 96 | + } elseif ($type === 'message') { |
| 97 | + echo 'Message <', $message, '> from channel <', $channel, '>', PHP_EOL; |
| 98 | + } |
| 99 | + // return <true> for to wait next message |
| 100 | + return true; |
| 101 | +}); |
| 102 | + |
| 103 | + |
| 104 | +// Example 4. psubscribe |
| 105 | + |
| 106 | +$Redis = new RedisClient([ |
| 107 | + 'timeout' => 0 // for waiting answer infinitely |
| 108 | +]); |
| 109 | + |
| 110 | +$Redis->psubscribe('channel.*', function($type, $pattern, $channel, $message) { |
| 111 | + // This function will be called on subscribe and on message |
| 112 | + if ($type === 'psubscribe') { |
| 113 | + // Note, if $type === 'psubscribe' |
| 114 | + // then $pattern = <channel-name> |
| 115 | + // and $channel = <count of subsribers> |
| 116 | + echo 'Subscribed to channel <', $pattern, '>', PHP_EOL; |
| 117 | + } elseif ($type === 'pmessage') { |
| 118 | + echo 'Message <', $message, '> from channel <', $channel, '> by pattern <', $pattern, '>', PHP_EOL; |
| 119 | + if ($message === 'quit') { |
| 120 | + // return <false> for unsubscribe and exit |
| 121 | + return false; |
| 122 | + } |
| 123 | + } |
| 124 | + // return <true> for to wait next message |
| 125 | + return true; |
| 126 | +}); |
0 commit comments