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