skip to Main Content

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?

2

Answers


  1. 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.

    Login or Signup to reply.
  2. Yes we can do it.
    First create your hash. Hash always consist of unique key, and unique key will be a string after that publish your key of hash.

    Instead of going with serialization of hash. Above method is simple and clear.

    One big advantage of publishing hash key is, in future we can easily get the latest/updated hash object if we updated some fields and if we were already stored the published key in list or set.

    Its save our bandwidth and network calls.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search