Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

node.js store objects in redis

node.js store objects in redis

Problem

Here is the thing - I want to store native JS (node.js) objects (flash sockets references) in redis under a certain key. When I do that with simple client.set() it's stored as a string. When I try to get value I get [object Object] - just a string.

Any chance to get this working? Here's my code:

  addSocket : function(sid, socket) {
    client.set(sid, socket);
  },

  getSocket : function(sid) {
    client.get(sid, function(err, reply) {
      // cant't get an object here. All I get is useless string
    });
  },
Problem courtesy of: Pono

Solution

Downvoters: the context here is SET command and ability to store arbitrary objects.

No, you can't do that. You should accept the fact that Redis stores everything as a string (the protocol is text-based, after all). Redis may perform some optimizations and convert some values to integers, but that's his business, not yours.

If you want to store arbitrary objects in Redis, make sure that you serialize them before saving and de-serialize after retrieving.

I'm not sure if you can do that with socket objects, though.

Solution courtesy of: Sergio Tulentsev

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

node.js store objects in redis

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×