Mustard Tiger
Mustard Tiger

Reputation: 3671

Redis, Python send message over network

I have the following code written in Python using the Redis PubSub messaging system.

Publish side:

 import redis
 def func1(m):

 queue = redis.StrictRedis(host='localhost', port=6379, db=0)
 channel = queue.pubsub()
 queue.publish("message", m)

 if __name__=='__main__':    
 while True:

     m = raw_input('Enter message: ')
     func1(m)

Subscriber side:

import redis
r = redis.StrictRedis(host='localhost', port=6379, db=0)
p = r.pubsub()
p.subscribe('message')

while True:
    message = p.get_message()
    if message:
        print  message['data']

I want to be able to send messages from one computer to another that are on the same network. I tried running the publisher and subscriber code on two separate machines, changing the host='localhost' to host='IP address of target machine' But when I ran it the subscribers program just sat idle and did not receive any messages.

How do you use Redis over a network instead of just on a local machine? Do I need to specify target machine IP's on both the pub and sub sides? I can not find any concrete example online , everything I find just assumes that you are running it on a localhost.

Upvotes: 4

Views: 2416

Answers (1)

Tommaso Barbugli
Tommaso Barbugli

Reputation: 12031

Both publisher and consumer must connect to the same Redis server.

I am not familiar with pubsub's .get_message() method, but I am pretty sure .listen() works just fine.

You can inspect what commands are being sent to the Redis server using the MONITOR command (eg. redis-cli -h 1.2.3.4 MONITOR where 1.2.3.4 is the address of the Redis server).

Upvotes: 0

Related Questions