Open Source Software Technical Articles

Want the Best of the Wazi Blogs Delivered Directly to your Inbox?

Subscribe to Wazi by Email

Your email:

Connect with Us!

Current Articles | RSS Feed RSS Feed

Developing a pubsub application in Redis


Developers use the publish/subscribe (pubsub) messaging model a lot these days to decentralize application and server communications and to reduce single point of failure. With pubsub, 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. A classic use for this would be a group chat or IRC server, in which users subscribe (or join) channels (or groups) and can chat with other users present in the channels.

Often developers use message queuing applications such as RabbitMQ or ActiveMQ to implement pubsub, but these solutions tend to be resource-intensive. Though Redis supports only a subset of the features of traditional messaging queues, when it comes to pubsub, it works amazingly well and uses few system resources.

Redis is a multipurpose advanced key-value store that provides a high-performance alternative to relational databases. You could also call it a one-of-a-kind data structure server, because it can hold not only plain strings, but also hashes, lists, and other kinds of data as well. For top performance, Redis stores its entire operational data set in memory, but it provides ways for developers to store the data permanently on disk or in a database.

Redis is straightforward to install. Most Linux distributions have precompiled packages available in their software repositories. To install it under CentOS or Red Hat Linux, first add the Extra Packages for Enterprise Linux (EPEL) repository to your system by following the instructions on its FAQ page. Alternatively you can download the EPEL binary that matches the architecture of your machine and the version of your operating system, then install it using the rpm command:

# rpm -ivh epel-release-6-7.noarch.rpm

You can then install Redis with the command yum install redis.

If the version in the EPEL repository is old, you can install the current Redis version from source:

$ wget
$ tar xzf redis-2.6.16.tar.gz
$ cd redis-2.6.16
$ make

Start Redis with the command sudo service redis start if you performed an RPM installation, or src/redis-server after source installation.

Using Redis

Redis supports many modes of operations, including transaction and pubsub. Developers can use transactional mode when they need atomicity – that is, when they need an entire operation to happen. If one component of an operation fails – updating one of several tables involved, for instance – the entire transaction must be reverted. This is comment with monetary applications, when you debit one account and credit another.

In this article, however, we'll focus on pubsub. Let's build a simple chat application that uses a pubsub architecture. We'll use Python's redis-py library, which you can install with the pip package manager:

$ sudo pip install redis

The application has two components: one publisher and one subscriber. The code for is simple:

import redis
import sys

r = redis.Redis("localhost")


for item in ps_obj.listen():
    print item['data']

First we import the redis library, which provides pubsub functionality, and Python's system library, which we will use to manage the command-line arguments. We create a Redis object "r" that connects to the Redis server running on localhost. You can use the IP address or domain of any machine running Redis here. Next we create a pubsub object "ps_obj" that subscribes to whatever channel we pass to it as a command-line argument. The object listens to this channel, and we display all the data that we receive from the channel – that is, all the chat messages, creating a group chat effect.

The code looks like this:

import redis
import sys

r = redis.Redis("localhost")
r.publish(sys.argv[1],"["+sys.argv[2]+"] " + " ".join(sys.argv[3:]))

Here too we import the redis and the system libraries to get the same functionality they provide in the subscriber code. We create a Redis object that connects us to the localhost, and we publish data by using the publish method, which accepts two arguments: the channel name to which data is to be published and the data that is to be published. In the code above, sys.argv[1] represents the channel name and sys.argv[2] and sys.argv[3] constitute the second argument to pass to the publish method.

To publish data to the channel, invoke the code with Python. Let's see how it works by running both scripts in different windows. In the first window, run the subscriber:

$ python test-channel

In the second, run the publisher:

$ python test-channel aditya Hi everyone.
$ python test-channel aditya This is Aditya.
$ python test-channel aditya I like Python and Redis.

The subscriber window will look like this:

[aditya] Hi everyone.
[aditya] This is Aditya.
[aditya] I like Python and Redis.

While a subscriber always needs to be alive in order to receive the message, a publisher can go offline or come online anytime. This is in effect what users do – they come to chat rooms whenever they want and go offline at their convenience.

This is a very simple working example of pubsub using Redis and Python. For production applications, you can use Redis' data persistence features in conjunction with pubsub to ensure minimal or no data loss. Redis also has a cluster feature under development that will allow developers to create a cluster of Redis servers to provide high availability and more scalability.

This work is licensed under a Creative Commons Attribution 3.0 Unported License
Creative Commons License.


Nevertheless, not every one of these duplicates are satisfactorily carried out. Therefore, here are some suggestions that may help you discover a good Handbag Replicas from the Chanel Handbags. What is alluring a ample bulk of barter to Replica Luxuries handbags is their availability. A seek on the internet reveals sites that accord with Gucci Handbags. You can seek for Replica Louis Vuitton Handbags by designer. Spending beneath than two hundred dollars to get a replica backpack appeals to a bulk of women. Our Replica pieces are almost similar when the proper Rolex Replicas watches, and people ought to have a ridiculously hard time if she or he were trying to know the expense within the real you or simply not.
Posted @ Tuesday, December 31, 2013 2:34 AM by touble
I hope my advice have helped you how to protect your iphone 5. Picking one particular outside of a lot of iPhone 5 circumstances could be Ipad 2 Case described as a tricky activity. With this type of massive selection, it's not likely being straightforward to figure out which would get the job done most effective for Ipad 3 Case you personally. Prior to you go out and purchase the to start with interesting just one you see, feel about your needs. For anyone who is an active, on the go form of individual, you require to acquire a circumstance which can stand up in opposition to falls. Those individuals who will be a lot more relaxed and simply just choose to have one thing unique and attractive can glimpse into the iPhone five instances that offer you patterns and images.
Posted @ Thursday, April 24, 2014 2:39 AM by iPhone 5 Cases
No a single can promise that he can put his iphone four while in the ideal location at all time. Besides, it is actually a superb plan to change the looks of iphone four. As we all know, iphone four is just white or black. So you should to pay attention to the case for those who like other colors.Even so, the iphone four circumstance is not incredibly cheap, especially some famous manufacturers like BELKIN. How to purchase an economical and great cell cell phone circumstance? My suggestion is usually to visit the dealextreme that is an e-shop with high reputation. It gives several forms of cell cell phone circumstances with diverse colors and patterns. Cell telephone scenarios which are made by leather and plastic are by far the most popular. Generally speaking, business man can decide on the leather-based scenario to show their status and taste; while young individuals can select other instances with bright color to shoe their persona. What is more vital is that these mobile telephone situations are certainly not expensive, so you are able to choose any a single you like. You are going to not be disappointed as prolonged as you visit it. What do you think of smart apple iphone instances? The brand new iPhone5 is out and is so expensive, you'll be want get a cheaper iphone, the apple iphone four is actually a excellent choice. and you must also get a circumstance to maintain it shielded. 
Chanel iPhone 5 Cases 
Chanel iPhone 5s Cases 
hermes iPhone 5s Cases
Posted @ Wednesday, April 30, 2014 1:55 AM by iphone 4s case
If the recipient always wears gold for instance, you can wish to consider a Replica Rolex Watches that matches so that it fits in with their current accessories. Matching Swiss Replica Watches the personality should be fairly easy. No matter whether they may be sports people,Michael Kors Handbag Outlet fashion conscious or possibly a little bit luxury designer watches abstract with their fashion sense, you will be able to find the perfect watch to suit their style.Most fashion brands now also own a line of focal watches .
Posted @ Monday, September 29, 2014 4:12 AM by topsalesswiss
Post Comment
Website (optional)

Allowed tags: <a> link, <b> bold, <i> italics