Redis, Python send message over the network
I have the following code written in Python using the Redis PubSub messaging system.
Publication:
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 to run the publisher and subscriber code on two separate machines, changing host = 'localhost' to host = 'target machine's IP address' But when I ran it, the subscriber program just sat around and didn't receive any messages.
How do you run Redis over a network, not just a local machine? Do I need to list the target computer's IP address on both the pub and sub-parties? I cannot find any specific example on the internet, everything I believe just assumes that you are running it on localhost.
source to share
Both the publisher and the consumer must connect to the same Redis server.
I'm not familiar with the pubsub method .get_message()
, but I'm pretty sure it .listen()
works fine.
You can check what commands are being sent to the Redis server using the command MONITOR
(for example redis-cli -h 1.2.3.4 MONITOR
, where 1.2.3.4 is the address of the Redis server).
source to share