Search code examples
pythonredisnosqlpublish-subscribe

Redis pubsub a complex data structure


I would like to use redis PUBSUB functionality with a complex data structure like a hash table. All the examples I see online only publish strings.

I would like to be able to do this:

redis> HSET dog name "Fido" weight 65
redis> PUBLISH pets dog

When I try this the response I get is "dog", not the hash table.

Is there a way I can do this?

One alternative I've considered is something like this: have the publisher do the same commands as above, then the subscriber will receive the text, then immediately issue a HGETALL command for that key. But that way there are multiple calls to redis being made, instead of sending the hash table in a single shot.

Is there a way I can send a complex data structure over redis PUBSUB?


Solution

  • Is there a way I can do this?

    No way. You can only publish a string. And your alternative is a solution to work around it.

    Is there a way I can send a complex data structure over redis PUBSUB?

    In order to avoid multiple calls to Redis, you can serialize the hash into a JSON string, e.g. {"name" : "Fido", "weight" : 65}, and publish the JSON string.