Pub/Sub in Redis using PHP
I would like to put an example together about the pub/sub using php in Redis; there is only API documentation available in phpredis, the PHP client I am using (http://redis.io/clients).
0. Setup
First setup a Redis Server. I set up a Redis server in my local box using port 6378 (myredisserver.test.com:6378).
1. Publish: push a message to a channel.
This part is relatively easy. The following is a php script “publish.php“.
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php //publish.php $redis = new Redis(); $redis ->pconnect( 'myredisserver.test.com' ,6378); $redis ->publish( 'chan-1' , 'hello, world!' ); // send message to channel 1. $redis ->publish( 'chan-2' , 'hello, world2!' ); // send message to channel 2. print "
" ; $redis ->close(); ?> |
1.5 Checkpoint: Monitor in Redis server.
Let’s take a break now and see what will happen if we run the script “publish.php” from a client side.
Because I use a non default port, “-p” option is used with “redis-cli” command.
Open a new terminal at Redis Server, and issue “MONITOR” command in redis “console”:
1
2
3
4
5
6
7
|
%redis-cli -p 6378 redis 127.0.0.1:6378> MONITOR OK 1321312790.866271 "MONITOR" 1321312792.221599 "PING" 1321312796.330376 "PUBLISH" "chan-1" "hello, world!" # after run "publish.php" 1321312796.330482 "PUBLISH" "chan-2" "hello, world2!" # after run "publish.php" |
2. Subscribe: Listen to a channel (or some channels).
Here is the complete php script “subscribe.php” after I did some debugging. Thanks to the info here: https://github.com/nicolasff/phpredis/issues/36.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<?php //subscribe.php function f( $redis , $chan , $msg ) { switch ( $chan ) { case 'chan-1' : print "get $msg from $chan
" ; break ; case 'chan-2' : print "get $msg FROM $chan
" ; break ; case 'chan-3' : break ; } } ini_set ( 'default_socket_timeout' , -1); $redis = new Redis(); $redis ->pconnect( 'myredisserver.test.com' ,6378); $redis ->subscribe( array ( 'chan-1' , 'chan-2' , 'chan-3' ), 'f' ); print "
" ; ?> |
2.5
1) run script “subscribe.php“:
1
|
%php subscribe.php |
of course, nothing happens.
2) In another terminal window, run script “publish.php” twice, and come back to have a look!
1
2
3
4
5
|
%php subscribe.php get hello, world! from chan-1 #newly displayed get hello, world2! FROM chan-2 #newly displayed get hello, world! from chan-1 #newly displayed get hello, world2! FROM chan-2 #newly displayed |
3. Breakpoint: Revisit the MINTOR window in Redis Server:
1
2
3
4
5
6
7
8
9
10
11
|
%redis-cli -p 6378 redis 127.0.0.1:6378> MONITOR OK 1321313546.882528 "MONITOR" 1321313552.848569 "PING" 1321313553.458541 "SUBSCRIBE" "chan-1" "chan-2" "chan-3" 1321313556.223800 "PUBLISH" "chan-1" "hello, world!" 1321313556.223862 "PUBLISH" "chan-2" "hello, world2!" 1321313557.597914 "PUBLISH" "chan-1" "hello, world!" 1321313557.598061 "PUBLISH" "chan-2" "hello, world2!" 1321313562.851878 "PING" |
4. Test with multiple publishers and multiple subscribers!
Reference: http://robots.thoughtbot.com/post/6325247416/redis-pub-sub-how-does-it-work