wiki'd

by JoKeru

Server to server realtime communication using Redis publish/subscribe

If you have a master / slave(s) architecture and you need the master to continuously update the slave(s), the publish/subscribe paradigm can be used. With pub/sub, one application publishes some data to a digital channel. Applications that are interested in receiving the data can subscribe to the appropriate channel and receive all the data that is published to it.

The recipe for this master to slave(s) communication is:
- a redis server (or cluster if you need availability)
- one publisher (a python script using redis-py library)
- one (or many) subscriber(s) (a python script using redis-py library)

The suggested way of installing Redis is compiling it from sources as Redis has no dependencies other than a working GCC compiler and libc. Installing it using the package manager of your Linux distribution is somewhat discouraged as usually the available version is not the latest.
[cc lang='bash']
\$ cd /usr/src/
\$ wget http://download.redis.io/redis-stable.tar.gz
\$ tar -xvzf redis-stable.tar.gz
\$ cd redis-stable
\$ make distclean
\$ make
\$ cp src/redis-server /usr/local/bin/
\$ cp src/redis-cli /usr/local/bin/
# start redis
\$ redis-server &
[/cc]

On a slave:
[cc lang='bash']
\$ apt-get install python python-pip -y
\$ pip install redis
\$ cat \<\<'EOF' > sub.py
#!/usr/bin/python
import redis
import sys
r = redis.Redis("10.20.30.40")
ps_obj=r.pubsub()
ps_obj.subscribe(sys.argv[1])
for item in ps_obj.listen():
if item['data'] != 1:
print ("Master said: " + str(item['data']))
EOF
\$ chmod +x sub.py
\$ ./sub.py orders
[/cc]

On Master:
[cc lang='bash']
\$ apt-get install python python-pip -y
\$ pip install redis
\$ cat \<\<'EOF' > pub.py
#!/usr/bin/python
import redis
import sys
r = redis.Redis("10.20.30.40")
r.publish(sys.argv[1],sys.argv[2])
EOF
\$ chmod +x pub.py
\$ ./pub.py orders "hello slaves"
\$ ./pub.py orders "how are we feeling today ?"
[/cc]

And this will produce the following output on the slave:
[cc lang='bash']
\$ ./sub.py orders
Master said: hello slaves
Master said: how are we feeling today ?
[/cc]

Comments