Reputation: 274
I am using https://github.com/redis/node-redis together with https://github.com/protobufjs/protobuf.js and I am having trouble getting the buffer data properly from my redis db.
In the minimal example below (test3), I can actually get a valid buffer if I subscribe to the channel "channel-1" (this is the channel where the data was originally published).
I guess there is something that I am missing in my initialization that makes me get the wrong data unless i first subscribe to "channel-1". Can anyone see where I am going wrong?
var redis = require('redis')
let protobuf = require('protobufjs')
const initRedis = async () => {
const root = protobuf.loadSync('mymodel.proto')
const Message = root.lookupType('MyModel')
const redisClient = redis.createClient({
url,
})
await redisClient.connect()
const getAndDecodeValue = async (key) => {
const value = await redisClient.get(key)
console.log(Message.decode(value))
}
if (currentTest === 'test1') {
// Throws Error: illegal buffer
getAndDecodeValue(key)
} else if (currentTest === 'test2') {
// Throws Error: illegal buffer
await getAndDecodeValue(key)
} else if (currentTest === 'test3') {
// Works and decodes the value correctly.
getAndDecodeValue(key)
redisClient.subscribe('channel-1', async (key, channel) => {
console.log(key, channel)
})
} else if (currentTest === 'test4') {
// Throws Error: illegal buffer
await getAndDecodeValue(key)
redisClient.subscribe('channel-1', async (key, channel) => {
console.log(key, channel)
})
} else if (currentTest === 'test5') {
// Throws Error: [ErrorReply: ERR Can't execute 'get': only (P|S)SUBSCRIBE / (P|S)UNSUBSCRIBE / PING / QUIT / RESET are allowed in this context]
redisClient.subscribe('channel-1', async (key, channel) => {
console.log(key, channel)
})
getAndDecodeValue(key)
}
}
For reference, this is the C++-code that puts the data into redis:
const std::string channel_name = "channel-1";
_redis->set(sw::redis::StringView(key), sw::redis::StringView(data));
_redis->publish(sw::redis::StringView(channel_name), sw::redis::StringView(key));
Upvotes: 0
Views: 252
Reputation: 274
I finally found the solution. Apparently, the redisClient silently converted the buffers to ASCII in test1, test2, and test4. Why test3 worked is beyond me, it seems like the async subscribe call somehow inactivated the ASCII-conversion so I could retrieve the original buffer.
By replacing the get call with the following line, the code works for test1, test2, test3 and test4:
const value = await redisClient.get(redis.commandOptions({ returnBuffers: true }), key)
Upvotes: 0