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“.

<?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”:

%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.

<?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“:

%php subscribe.php

of course, nothing happens.

2) In another terminal window, run script “publish.php” twice, and come back to have a look!

%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:

%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

Posted in Uncategorized | Tagged | 1 Comment

one cannot be a slave for multi-masters

one can only be one master’s slave. :D

Today I set up three redis server in this way: Redis-Server-A:6379, Redis-Server-B:6379, Redis-Server-C:6378.

Each is up and running fine by my testing of set and get.

Then I set up a Master-Slave using: Redis-Server-A:6379 (Master), Redis-Server-B:6379 (Slave).

It is working like a magic :). I mean instantly data is replicated from Master to Slave.

Then I configured the redis.conf file in Reids-Server-B to be slave of Redis-Server-C:6378 like this:
slaveof Redis-Server-A 6379
slaveof Redis-Server-C 6378

Restart Redis-Server-B, and all the content in Redis-Server-B are the same as Redis-Server-C, all the previous records replicated form Redis-Server-A have been wiped out.

So, one cannot be a slave for multi masters!

PART two: One Master can have multiple slaves!
I configured Redis-Server-A as Master, Redis-Server-B slaveof Redis-Server-A, Redis-Server-C slaveof Redis-Server-A. A’s records are replicated to B and C both.

Posted in Uncategorized | Tagged | Leave a comment

memcache

1. how to do replication

http://repcached.lab.klab.org/

1.5 how to do multiple to multiple replication?
repcache is only one-to-one?

2. how to do dump and reload

http://shairosenfeld.com/blog/index.php/2010/10/memcached-dump-and-reload/

Posted in Uncategorized | Leave a comment

AsnycTask in Android

data flow :

Posted in Uncategorized | Leave a comment

Android books

Since I started learning Android programming, I have read several books and learned everything from zero background.
Here is the list:
1) Programming Android

2) Beginning Androidâ„¢ Application Development

By: Wei-Meng Lee
Publisher: Wrox
Pub. Date: April 19, 2011
Print ISBN: 978-1-118-01711-1
Web ISBN: 1-118017-11-0

Posted in Android Beginner | Tagged | Leave a comment

The mysteries about “upload photo” using facebook-android-sdk

After my first try, I now want to expand the functionality of my “MyCreateApp” to upload photo. It took me quite some time to figure it out. The downloaded example code package provided by facebook is not working without some tweaking. I found it is quite interesting to fix it here and there and make it run eventually.

http://forum.developers.facebook.net/viewtopic.php?id=104678

and good tip: https://www.facebook.com/topic.php?uid=74769995908&topic=16667

Posted in Android Beginner | Tagged , | Leave a comment

build my first Facebook android app using facebook-android-sdk! – III

So long ! well, don’t give up. It is only halfway.

Let’s check where we are now from the official facebook blog: https://developers.facebook.com/docs/guides/mobile/#android

Register your app in Facebook
Visit this URL to register your app: https://developers.facebook.com/apps

If you have not verified your account, you need to verify your account either by SMS or credit card number first.

I named my first registered app as “MyCreateApp”, and enter the signature generated in last step. Up to this point, all the non-coding preparation is done.

According to the facebook blog, next step is “Installing the Facebook Android App” in emulator, but this is optional. I skipped this one for now.

SSO (Single-Sign-On)

Let’s modify the MyCreateAppActivity.java file in our Android project “MyCreateApp“. As I did not install the Facebook.apk in my emulator, it is slightly different from the sample code in tutorial:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.facebook.android.*;
import com.facebook.android.Facebook.*;

public class MyCreateAppActivity extends Activity {
	 Facebook facebook = new Facebook("YOUR OWN APP ID");

	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.main);

	        facebook.dialog(this,"feed",
	        	      new DialogListener() {
	        	           public void onComplete(Bundle values) {}

	        	           public void onFacebookError(FacebookError error) {}

	        	           public void onError(DialogError e) {}

	        	           public void onCancel() {}
	        	      }
	        	);
	    }

	    @Override
	    public void onActivityResult(int requestCode, int resultCode, Intent data) {
	        super.onActivityResult(requestCode, resultCode, data);

	        facebook.authorizeCallback(requestCode, resultCode, data);
	    }
}

The following is the screenshot when running the project in an Android2.2 Emulator. It will ask for user name and password first:

After input user name and password, a new window will pop up and if you enter some status and click “Share” button, the newest update will be posted to the user’s wall.

Verify it in facebook to see whether the post is on the wall ! — END

Posted in Android Beginner | Tagged , | Leave a comment