1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Why does redis report no subscribers to a channel after I subscribe to that channel...

Discussão em 'Python' iniciado por Stack, Setembro 27, 2024 às 15:52.

  1. Stack

    Stack Membro Participativo

    I am trying to send realtime feedback over websockets, and I'm doing that by sending data over redis from a rest-api server to a websocker server (both written in python/django). In the websocket server, I subscribe to a channel named "events" like this:

    redis_connection = redis.Redis(
    host=settings.REDIS_HOST,
    port=settings.REDIS_PORT,
    db=settings.REDIS_CACHE_DB,
    )
    pubsub = redis_connection.pubsub()
    pubsub.subscribe(events=process_event)


    In the rest-api server, I publish data to the same redis channel like this:

    connection.publish("events", json.dumps({"user_email": user.email, "message": message}))


    While trying to figure out why no events ever made it to the "process_event" handler, I found that the publish method returns the number of subscribers to a channel, so I updated that line to this:

    count = connection.publish("events", json.dumps({"user_email": user.email, "message": message}))
    print(f"Published to events channel with {count} subscribers")


    The result is always 0, even after the subscription above. I went inside the redis-stack Docker container saw the following when I directly tried to query the subscriber count:

    redis-cli


    127.0.0.1:6379> PUBSUB NUMSUB events

    1. "events"
    2. (integer) 0

    127.0.0.1:6379>

    The logs of the redis-stack itself doesn't seem to recognize any subscription or publication. However, if I use the command "redis-cli monitor" I see both show up:


    1727460056.950779 [0 172.17.0.1:58210] "CLIENT" "SETINFO" "LIB-NAME" "redis-py" 1727460056.952892 [0 172.17.0.1:58210] "CLIENT" "SETINFO" "LIB-VER" "5.0.8" 1727460056.954072 [1 172.17.0.1:58210] "SELECT" "1"


    1727460056.956442 [1 172.17.0.1:58210] "SUBSCRIBE" "events"


    1727460122.677189 [0 172.17.0.1:45874] "CLIENT" "SETINFO" "LIB-NAME" "redis-py" 1727460122.678973 [0 172.17.0.1:45874] "CLIENT" "SETINFO" "LIB-VER" "5.0.8" 1727460122.681286 [1 172.17.0.1:45874] "SELECT" "1"


    1727460122.683120 [1 172.17.0.1:45874] "PUBLISH" "events" "{"user_email": "admin@domain.com", "message": "Message content"}"

    After all that, PUBSUB NUMSUB events still gives the same result.

    Continue reading...

Compartilhe esta Página