socket.io-redis-adapter icon indicating copy to clipboard operation
socket.io-redis-adapter copied to clipboard

uncaught error: 348 trailing bytes

Open zacharynevin opened this issue 12 years ago • 33 comments

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.

zacharynevin avatar Jul 13 '14 07:07 zacharynevin

So we have narrowed this down. https://github.com/automattic/socket.io-emitter#emitterclient-opts says to set return_buffers to true on your redis client. This should fix this issue.

We need to add support to socket.io-redis to see if the incoming/outcoming data is binary before sending it to msgpack.

alexprice1 avatar Jul 14 '14 20:07 alexprice1

It worked! Thank you!

zacharynevin avatar Jul 14 '14 21:07 zacharynevin

maybe on the README we could "enforce" a bit more that parameter!

i also missed the detect_buffers: true on the "Custom Client" section

hems avatar Jun 28 '15 03:06 hems

I have same problem. I use ruby socket emitter to broadcast an event:

emitter = SocketIO::Emitter.new(key: 'socket.io.dev', redis: Redis.new(url: 'redis://localhost:6379'))
data = {id: 100, username: 'peternguyen', content: Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s'}
emitter.emit('comment', data)

In app.js

var io = require('socket.io')(5001);
var request = require('request');
var redis = require('redis');
var redis_adapter = require('socket.io-redis');

var pub = redis.createClient(6379, 'localhost', {return_buffers: true, auth_pass: ''});
var sub = redis.createClient(6379, 'localhost', {return_buffers: true, auth_pass: ''});

io.adapter(redis_adapter({
    key: 'socket.io.dev',
    pubClient: pub,
    subClient: sub
}));

I get the following in my console:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: -2340102607 trailing bytes
    at Object.decode (/home/peter/node_modules/socket.io-redis/node_modules/msgpack-js/msgpack.js:200:47)
    at Redis.onmessage (/home/peter/node_modules/socket.io-redis/index.js:94:24)
    at RedisClient.emit (events.js:106:17)
    at RedisClient.return_reply (/home/peter/node_modules/redis/index.js:696:22)
    at ReplyParser.<anonymous> (/home/peter/node_modules/redis/index.js:321:14)
    at ReplyParser.emit (events.js:95:17)
    at ReplyParser.send_reply (/home/peter/node_modules/redis/lib/parser/javascript.js:300:10)
    at ReplyParser.execute (/home/peter/node_modules/redis/lib/parser/javascript.js:211:22)
    at RedisClient.on_data (/home/peter/node_modules/redis/index.js:547:27)
    at Socket.<anonymous> (/home/peter/node_modules/redis/index.js:102:14)

nnmduc avatar Jul 21 '15 06:07 nnmduc

peter chexk the return_buffers option, that should sort your issue.

hems avatar Jul 21 '15 18:07 hems

@peternguyen1991 please refer here: https://github.com/Automattic/socket.io-redis/pull/61#commits-pushed-8d7b40f

Your problem was that Ruby's SocketIO::Emitter packs the messages using the new msgpack-js v5, while the official socket.io-redis client only supports traditional msgpack-js. You could either clone from @burnssun's brilliant fork or manually edit your local socket.io-redis node module to reflect the necessary changes.

Thesephi avatar Jul 21 '15 20:07 Thesephi

i saw the trailing bytes issue the first time on my app today. when i set the return buffers option i get this error

TypeError: Object socket.io#VZCddR has no method 'split'

  at Redis.onmessage (/path/to/my/app/node_modules/socket.io-redis/index.js:92:26)

is there a permanent fix available for all these issues?

aschmid avatar Sep 22 '15 16:09 aschmid

ok found the issue. node redis v2.0.0 (released yesterday) has issues with the latest version of socket.io-redis causing the trailing bytes exeption. when setting return_buffers to true unfortunately it throws an exception like in my previous comment.

aschmid avatar Sep 22 '15 17:09 aschmid

@aschmid so is the solution to simply rollback one of these modules versions, or did you find another way?

stephengardner avatar Sep 26 '15 04:09 stephengardner

@stephengardner i didn't have time to look into a 'proper' solution yet so my fix was to use v1.0.0 of https://www.npmjs.com/package/redis.

aschmid avatar Sep 27 '15 17:09 aschmid

In my setup, node 4.1.1, socket-io 0.1.4, node_redis 2.0.1 the problem is that index.js line 92 expect "channel" to be a string, but it's a Buffer.

PerMoeller avatar Sep 28 '15 11:09 PerMoeller

set up redis ver. 1.0.0... is trailing bytes error fixed..

i think, that issue is node_redis 2.0.1 bug.

modify your file(pakage.json) redis version " 1.* " in dependencies

Mokbii avatar Oct 02 '15 07:10 Mokbii

I am still getting this issue even when I set my redis version to 1.0.0

zacharynevin avatar Oct 09 '15 10:10 zacharynevin

I used the same package versions as PerMoeller and got his exact issue:

channel.split("#") <!-- cannot read split of undefined

zacharynevin avatar Oct 10 '15 11:10 zacharynevin

I'm still having this error with redis client of 2.2.3 and socket.io of 1.3.7, socket.io-redis:0.1.4, even with the detect_buffers and return_buffers set to true https://github.com/vodolaz095/hunt/blob/master/lib/datastore/redisClient.js#L44 .

/home/projects/vodolaz095/hunt/node_modules/socket.io-redis/node_modules/msgpack-js/msgpack.js:200
  if (decoder.offset !== buffer.length) throw new Error((buffer.length - decoder.offset) + " trailing bytes");
                                        ^

Error: 75 trailing bytes
    at Object.decode (/home/projects/vodolaz095/hunt/node_modules/socket.io-redis/node_modules/msgpack-js/msgpack.js:200:47)
    at Redis.onmessage (/home/projects/vodolaz095/hunt/node_modules/socket.io-redis/index.js:94:24)
    at emitThree (events.js:97:13)
    at RedisClient.emit (events.js:175:7)
    at RedisClient.return_reply (/home/projects/vodolaz095/hunt/node_modules/redis/index.js:622:22)
    at /home/projects/vodolaz095/hunt/node_modules/redis/index.js:310:18
    at doNTCallback0 (node.js:417:9)
    at process._tickDomainCallback (node.js:387:13)

npm ERR! Linux 4.1.8-100.fc21.x86_64
npm ERR! argv "/usr/bin/node" "/bin/npm" "start"
npm ERR! node v4.2.1
npm ERR! npm  v2.14.7
npm ERR! code ELIFECYCLE
npm ERR! [email protected] start: `node examples/index.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] start script 'node examples/index.js'.
npm ERR! This is most likely a problem with the hunt package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node examples/index.js
npm ERR! You can get their info via:
npm ERR!     npm owner ls hunt
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/projects/vodolaz095/hunt/npm-debug.log

And it works ok with redis of 1.0.0.

vodolaz095 avatar Oct 17 '15 16:10 vodolaz095

Same issue here with redis v2.2.5, socket.io v1.3.7 and socket.io-redis v0.1.4.

// Server
redis.createClient(redisOptions.port, redisOptions.host, {
      detect_buffers: true,
      return_buffers: true,
      auth_pass: redisOptions.password
    });
// Client
socket.emit('echo', {content: 'A'});

And I'm getting this error:

/app/node_modules/socket.io-redis/node_modules/msgpack-js/msgpack.js:200
  if (decoder.offset !== buffer.length) throw new Error((buffer.length - decoder.offset) + " trailing bytes");
                                        ^

Error: 86 trailing bytes
    at Object.decode (/app/node_modules/socket.io-redis/node_modules/msgpack-js/msgpack.js:200:47)
    at Redis.onmessage (/app/node_modules/socket.io-redis/index.js:94:24)
    at emitThree (events.js:97:13)
    at RedisClient.emit (events.js:175:7)
    at RedisClient.return_reply (/app/node_modules/redis/index.js:620:22)
    at /app/node_modules/redis/index.js:309:18
    at doNTCallback0 (node.js:417:9)
    at process._tickCallback (node.js:346:13)

Hope it helps

Caligone avatar Oct 21 '15 07:10 Caligone

I have this issue with the following versions

node: v4.2.1 socket.io: 1.37 socket.io-redis: 0.14 redis: 1.0 (also tried 2.0 and 2.3)

I am emitting an event with the socket.io-php-emitter with msgpack and redis

events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: -842817745 trailing bytes
    at Object.decode (/var/www/website1/lib/vendor/node/node_modules/socket.io-redis/node_modules/msgpack-js/msgpack.js:200:47)
    at Redis.onmessage (/var/www/website1/lib/vendor/node/node_modules/socket.io-redis/index.js:94:24)
    at emitThree (events.js:97:13)
    at RedisClient.emit (events.js:175:7)
    at RedisClient.return_reply (/var/www/website1/lib/vendor/node/node_modules/socket.io-redis/node_modules/redis/index.js:672:22)
    at HiredisReplyParser.<anonymous> (/var/www/website1/lib/vendor/node/node_modules/socket.io-redis/node_modules/redis/index.js:309:14)
    at emitOne (events.js:77:13)
    at HiredisReplyParser.emit (events.js:169:7)
    at HiredisReplyParser.execute (/var/www/website1/lib/vendor/node/node_modules/socket.io-redis/node_modules/redis/lib/parser/hiredis.js:43:18)
    at RedisClient.on_data (/var/www/website1/lib/vendor/node/node_modules/socket.io-redis/node_modules/redis/index.js:534:27)

austinh avatar Nov 12 '15 18:11 austinh

Update: Switching dependenices from msgpack-js to msgpack-js-v5 fixed this problem for me. It looks like php's msgpack module has been updated to the spec, but the version used by msgpack has not.

austinh avatar Nov 13 '15 02:11 austinh

Does this bug have anything to do with redis version after 3.0.6? I have tried the suggestions in this post and downgraded all modules but the problem isn't fixed

laukaichung avatar Jan 08 '16 14:01 laukaichung

After several experiments, my service works fine with

    "socket.io": "1.3.7",
    "socket.io-redis": "0.1.4",

Just for reference

StevenRoan avatar Jan 12 '16 08:01 StevenRoan