Project

General

Profile

1
<?php
2

    
3
/*
4
 * This file is part of the Predis package.
5
 *
6
 * (c) Daniele Alessandri <suppakilla@gmail.com>
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
require 'SharedConfigurations.php';
13

    
14
// Redis 2.0 features new commands that allow clients to subscribe for
15
// events published on certain channels (PUBSUB).
16

    
17
// Create a client and disable r/w timeout on the socket
18
$client = new Predis\Client($single_server + array('read_write_timeout' => 0));
19

    
20
// Initialize a new pubsub context
21
$pubsub = $client->pubSubLoop();
22

    
23
// Subscribe to your channels
24
$pubsub->subscribe('control_channel', 'notifications');
25

    
26
// Start processing the pubsup messages. Open a terminal and use redis-cli
27
// to push messages to the channels. Examples:
28
//   ./redis-cli PUBLISH notifications "this is a test"
29
//   ./redis-cli PUBLISH control_channel quit_loop
30
foreach ($pubsub as $message) {
31
    switch ($message->kind) {
32
        case 'subscribe':
33
            echo "Subscribed to {$message->channel}\n";
34
            break;
35

    
36
        case 'message':
37
            if ($message->channel == 'control_channel') {
38
                if ($message->payload == 'quit_loop') {
39
                    echo "Aborting pubsub loop...\n";
40
                    $pubsub->unsubscribe();
41
                } else {
42
                    echo "Received an unrecognized command: {$message->payload}.\n";
43
                }
44
            } else {
45
                echo "Received the following message from {$message->channel}:\n",
46
                     "  {$message->payload}\n\n";
47
            }
48
            break;
49
    }
50
}
51

    
52
// Always unset the pubsub context instance when you are done! The
53
// class destructor will take care of cleanups and prevent protocol
54
// desynchronizations between the client and the server.
55
unset($pubsub);
56

    
57
// Say goodbye :-)
58
$info = $client->info();
59
print_r("Goodbye from Redis v{$info['redis_version']}!\n");
(10-10/17)