Pub/Sub in Redis using PHP

Posted on November 14, 2011by xmeng

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 "\n";
  $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\n";
            break;
        case 'chan-2':
            print "get $msg FROM $chan\n";
            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 "\n";
 
?>

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

最新文章

  1. ObjectAnimator属性动画应用demo
  2. WPS显示无法创建对象,请确认对象已在系统注册表中注册
  3. P1032 字串变换
  4. Java根据条件删除Map中元素
  5. UVa 572 油田(DFS求连通块)
  6. redis rdb
  7. spring 计划任务:cron表达式
  8. [jumping to the gate] 娱乐向setjmp
  9. $(&amp;#39;#checkbox&amp;#39;).attr(&amp;#39;checked&amp;#39;); 回报checked或undefined该解决方案
  10. IT职场经纬 |阿里web前端面试考题,你能答出来几个?
  11. Python报错:IndentationError: expected an indented block
  12. SiteMesh在项目中的配置
  13. 浏览器css隐藏滚动条的方法!除了IE一般都支持
  14. 本地yum仓库搭建,使用163yum源
  15. vue批量验证提交表单的数据是否合规
  16. Java代码优化小结(二)
  17. C语言中几个常用数学计算函数ceil(), floor(), round()的用法
  18. Eclipse常见设置与操作
  19. ANTLR#1:描述一个简单计算器
  20. SVN版本服务器搭建

热门文章

  1. python基础一 day5 知识点
  2. mkdir touch vim
  3. docker部署xxl-job
  4. Shell数值比较
  5. 普通平衡树(treap)
  6. 夯实Java:从面向对象说起
  7. PAT 1010. 一元多项式求导
  8. luogu3415 祭坛
  9. ul标签中,li标签的移除、属性值获取
  10. POJ-2689 Prime Distance,区间素数筛法