Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get messages published to Redis before subscribing to the channel

I am writing an application to get messages published to a channel in Redis and process them. It is a long-lived app which never basically listens to the channel.

def msg_handler():
    r = redis.client.StrictRedis(host='localhost', port=6379, db=0)
    sub = r.pubsub()
    sub.subscribe(settings.REDIS_CHANNEL)
    while True:
        msg = sub.get_message()
        if msg:
            if msg['type'] == 'message':
                print(msg)
def main():

    for i in range(3):
        t = threading.Thread(target=msg_handler, name='worker-%s' % i)
        print('thread {}'.format(i))
        t.setDaemon(True)
        t.start()
    while True:
        print('Waiting')
        time.sleep(1)

When I run this program, I notice that it is not getting messages that were published to the channel before the the program started. It works fine to get messages sent to the channel after the app subscribing to the channel.

In production, it is very likely there are some messages in the channel before the program starts. Is there a way to get these old messages?

like image 530
ddd Avatar asked Sep 06 '25 02:09

ddd


1 Answers

Redis PUB/SUB does not store messages that were published. It sends them to who was listening at the moment. If you need to have access to old messages, you can:

  1. Use Redis Streams. They are in beta now and coming for version 5.
  2. Use another PUBSUB system, for example nats.io
like image 88
Imaskar Avatar answered Sep 07 '25 20:09

Imaskar