socket.io-redis-adapter: uncaught error: 348 trailing bytes

I am using socket.io-emitter to broadcast an event to a set of channels with a for loop:

In the file, I have:

// in the file
var io = require('socket.io-emitter')({
  host: 'localhost',
  port: 6379
});

module.exports = {
    exampleFunction: function(req, res, next) {
      var channels = req.param('channels'),
            data = req.param('data');

      for (var i=0; i<channels.length; i++) {
        io.to(channels[i]).emit('example event', data)
      }
    }

}

In app.js, I have socket.io-redis:

io.adapter(socketio_redis({ 
  host: 'localhost', 
  port: 6379,
  pubClient: redis.createClient(6379, '127.0.0.1'),
  subClient: redis.createClient(6379, '127.0.0.1')
}))

When I try to run exampleFunction, I get the following uncaught error in my console:

Error: 348 trailing bytes
    at Object.decode (C:\Users\Zachary\Documents\GitHub\thegraduate_backend\node
_modules\socket.io-redis\node_modules\msgpack-js\msgpack.js:200:47)
    at Redis.onmessage (C:\Users\Zachary\Documents\GitHub\thegraduate_backend\no
de_modules\socket.io-redis\index.js:93:24)
    at RedisClient.EventEmitter.emit (events.js:106:17)
    at RedisClient.return_reply (C:\Users\Zachary\Documents\GitHub\thegraduate_b
ackend\node_modules\redis\index.js:672:22)
    at ReplyParser.<anonymous> (C:\Users\Zachary\Documents\GitHub\thegraduate_ba
ckend\node_modules\redis\index.js:309:14)
    at ReplyParser.EventEmitter.emit (events.js:95:17)
    at ReplyParser.send_reply (C:\Users\Zachary\Documents\GitHub\thegraduate_bac
kend\node_modules\redis\lib\parser\javascript.js:300:10)
    at ReplyParser.execute (C:\Users\Zachary\Documents\GitHub\thegraduate_backen
d\node_modules\redis\lib\parser\javascript.js:211:22)
    at RedisClient.on_data (C:\Users\Zachary\Documents\GitHub\thegraduate_backen
d\node_modules\redis\index.js:534:27)
    at Socket.<anonymous> (C:\Users\Zachary\Documents\GitHub\thegraduate_backend
\node_modules\redis\index.js:91:14)

When I change data to be something smaller, like:

var data = { testing: true }

I get the same error, but to a smaller number of trailing bytes. e.g. 95 trailing bytes.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 1
  • Comments: 33

Commits related to this issue

Most upvoted comments

Hi ! Actually the bug exists if you use the same redis client in socket.io-redis and in a “classic” usage. Just create two more redisClient to pub and sub.

var ioPub = redis.createClient(redisPort, redisHost, {detect_buffers: true, return_buffers: false});
var ioSub = redis.createClient(redisPort, redisHost, {return_buffers: true});

var pub = redis.createClient(redisPort, redisHost, {detect_buffers: true, return_buffers: false});
var sub = redis.createClient(redisPort, redisHost, {detect_buffers: true, return_buffers: false});

io.adapter(redisIO({
  host: config.redis.host,
  port: config.redis.port,
  pubClient: ioPub,
  subClient: ioSub
}));

io.on('connection', function(socket) {
  console.log('connected');
  socket.on('getMessages', function(){
    console.log('published');
    pub.publish('coolMessages', 'yolo message');
  });
});

sub.subscribe('coolMessages');

sub.on('message', function(channel, data){
  console.log('message!'); // this doesn't run
});

I was able to install the v5 version of msgpack-js-v5, npm i msgpack-js-v5 --save

then change the requires statement in node_modules/socket.io-redis/index.js (around line 8) from var msgpack = require(‘msgpack-js’); to var msgpack = require(‘msgpack-js-v5’);

and that worked in my setup.