2

Below is a code for redis pubsub in node.js. I publish a binary data after reading a file and a plain text, is there a way to identify if the buffer received on message is binary or text?

var redis = require("redis"),
    pub = redis.createClient(null, null, {
        detect_buffers: true
    }),
    sub = redis.createClient(null, null, {
        detect_buffers: true
    }),
    fs = require("fs");

sub.subscribe("abc");

sub.on("subscribe", function(channel, count) {
    fs.readFile("IMG_2693.JPG", function(err, data) {
        pub.publish("abc", data);
    });
    pub.publish("abc", "hello");

});

sub.on("message", function(channel, data) {
    // Can I identify if it is binary data or text?
    console.dir(data);
});

Thanks!

Vishal
  • 19,879
  • 23
  • 80
  • 93
  • this might help http://stackoverflow.com/questions/10225399/check-if-a-file-is-binary-or-ascii-with-node-js – vinayr Oct 10 '12 at 01:18

1 Answers1

0

Redis is agnostic regarding data encoding. It does not care if the data is binary or text, and cannot return or forward this information to a client.

I think a classical way to handle this problem is to add a header to your payload containing meta information (a few bytes). For instance, you could also store the data type, an indicator to know whether the payload is compressed or not, etc ...

Didier Spezia
  • 70,911
  • 12
  • 189
  • 154