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

弄技术要弄通-公司reis的pub/sub怎么使用的呢?的更多相关文章

  1. 打工心态废掉了很多人,包括你吗?(你把现在这家公司的业务都弄清楚、弄懂了吗?君子报仇十年不晚!不离不弃!)good

    我只拿这点钱,凭什么去做那么多工作,我傻呀. 我为公司干活,公司付我一份报酬,等价交换而已,我不欠谁的. 我只要对得起这份薪水就行了,多一点我都不干,做了也白做. 工作嘛,又不是为自己干,说得过去就行 ...

  2. 互联网技术笔试总通不过?leetcode刷对了么

    https://36kr.com/p/5084645 Leetcode,绕都绕不过去的程序员刷题神器 编者按:本文来自逆行求职(ID:nixingjihua). 对所有求职技术岗位的童鞋来说,有这么一 ...

  3. 泛泰A820L (高通公司MSM8660 cpu) 3.4内核CM10.1(Android 4.2.2) 测试版第二版

    欢迎关注泛泰非盈利专业第三方开发团队 VegaDevTeam  (本team 由 syhost suky zhaochengw(z大) xuefy(大星星) tenfar(R大师) loogeo cr ...

  4. Nubia Z5S(高通公司MSM8974) QHSUSB_BULK砖的方法节省模式(随着win7在恢复recovery分区案例)

    Nubia Z5S在某些异常情况或按组合键进入QHSUSB_BULK状态, 这种模式的现象, 猜想windows(实例win7)即使在数据线, 它会出现在计算机n载,甚至会提示要格式化某些分区(这里要 ...

  5. 泛泰A860(高通公司8064 cpu 1080p) 拂4.4中国民营recovery TWRP2.7.1.2文本(通过刷第三版)

    专业第三方开发团队 VegaDevTeam  (本team 由 syhost suky zhaochengw(z大) xuefy(大星星) tenfar(R大师) loogeo crazyi(天下无雪 ...

  6. 高通公司 MSM8K GPT异常原因分析无法开机的问题

    问题分析过程如下面: 一. MSM8916台gpt概率问题:采用QPST emmc software download下载软件工具后,无法开机.例如下面的附图: log分析是userdata分区未成功 ...

  7. pytorch加载数据的方法-没弄,打算弄

    参考:https://www.jianshu.com/p/aee6a3d72014 # 网络,netg为生成器,netd为判别器 netg, netd = NetG(opt), NetD(opt) # ...

  8. 入坑IT十年(二)技术以外

    上一篇博客里提到:技术越来越简单,发布后不久,就看到<技术并不是越来越简单>,这显然是打擂台来了. 技术究竟是不是越来越简单?其实这个问题,要看你究竟是以什么角度来思考这个问题.我们可以举 ...

  9. 手机CPU

    说起手机CPU的历史,笔者给大家提一个问题:"世界上第一款智能手机是什么呢?"相信很多人的答案是爱立信的R380或诺基亚的7650,但都不对,真正的首款智能手机是由摩托罗拉在200 ...

随机推荐

  1. 富通天下(W 笔试)

    纸质算法题目 1.给你一个字符串,找出其中第一个只出现过一次的字符及其位置 正解:一层for循环,循环按序取出字符串中的单个字符,循环体内部使用String类的indexOf(),从当前字符下标往后搜 ...

  2. react开启一个项目 webpack版本出错

    npx create-react-app my-app cd my-app npm start 在命令行里执行以上语句就可(前两天刚刚发现,最新版的react对webpack的版本要了新要求,大概是他 ...

  3. Vue 几种常见开局方式

    vue的开局方式五花八门,这里列几种常见的. 我们先建立一个app.vue来当入口文件,即所有页面都会以这个组件为模板. <template> <div id="app&q ...

  4. 【搜索】P1032 字串变换

    题目描述 已知有两个字串A,B及一组字串变换的规则(至多6个规则): A1​ ->B1​ A2​ -> B2​ 规则的含义为:在 A中的子串 A1​ 可以变换为B1​,A2​ 可以变换为  ...

  5. QT_6_QMainWindow

    QMainWindow 1.1. 菜单栏 1.1.1. 只有一个 1.1.2. QMenuBar *bar = MenuBar(); 1.1.3. 设置到窗口中 setMenuBar(bar); 1. ...

  6. postman使用--批量执行测试用例和数据驱动

    批量执行 在我们测试接口的时候,有时候希望执行所有的测试用例,前面讲的都是测试单个的接口,postman提供了我们批量执行接口的功能 点击Runner 然后我们点击run 执行完会统计出我们的结果,失 ...

  7. 如何给run()方法传参数

    实现的方式主要有三种 1.构造函数传参 2.成员变量传参 3.回调函数传参 问题:如何实现处理线程的返回值? 1.主线程等待法(优点:实现起来简单,缺点:需要等待的变量一多的话,代码就变的非常臃肿.而 ...

  8. In line copy and paste to system clipboard

    On the Wiki Wiki Activity Random page Videos Photos Chat Community portal To do    Contribute  Watch ...

  9. (9) openssl enc(对称加密)

    对称加密工具,了解对称加密的原理后就很简单了,原理部分见下文. openssl   enc  -ciphername   [-in filename]   [-out filename]   [-pa ...

  10. 实验:iscsi共享存储

    实验名称: iscsi共享存储 实验环境: 我们需要准备一个磁盘,对于这个磁盘我们需要使用,将这个磁盘空间共享给iscsi客户端: 实验需求: 我们这里使用两台服务器来实现iscsi共享存储: 1.指 ...