moscajs / mosca

MQTT broker as a module
mosca.io
3.2k stars 508 forks source link

using ioredis with option dropBufferSupport will throw error when packet received #592

Closed TangMonk closed 7 years ago

TangMonk commented 7 years ago

code below:

const pubsubsettings = {
  type: 'redis',
  db: 12,
  port: 6379,
  return_buffers: true,
  host: "localhost",
  redis: require('ioredis'),
  dropBufferSupport: true
};

const moscaSettings = {
  port: 1883,
  backend: pubsubsettings,
  persistence: {
    factory: mosca.persistence.Redis,
    host: 'localhost',
    port: 6379,
    db: 12
  },
};

const server = new mosca.Server(moscaSettings);

when started mosca server, it works normally, but when received login packet it will throw an error and exit:

/Users/tangmonk/Documents/mygit/nodejs/hotel_didi_car/node_modules/.0.1.26@msgpack-lite/lib/read-core.js:24
    if (!func) throw new Error("Invalid type: " + (type ? ("0x" + type.toString(16)) : type));
               ^

Error: Invalid type: 0x�
    at Codec.decode (/Users/tangmonk/Documents/mygit/nodejs/hotel_didi_car/node_modules/.0.1.26@msgpack-lite/lib/read-core.js:24:22)
    at DecodeBuffer.fetch (/Users/tangmonk/Documents/mygit/nodejs/hotel_didi_car/node_modules/.0.1.26@msgpack-lite/lib/decode-buffer.js:26:21)
    at DecodeBuffer.read (/Users/tangmonk/Documents/mygit/nodejs/hotel_didi_car/node_modules/.0.1.26@msgpack-lite/lib/flex-buffer.js:166:28)
    at Object.decode (/Users/tangmonk/Documents/mygit/nodejs/hotel_didi_car/node_modules/.0.1.26@msgpack-lite/lib/decode.js:10:18)
    at Immediate.<anonymous> (/Users/tangmonk/Documents/mygit/nodejs/hotel_didi_car/node_modules/.3.2.0@ascoltatori/lib/redis_ascoltatore.js:120:27)
    at runCallback (timers.js:651:20)
    at tryOnImmediate (timers.js:624:5)
    at processImmediate [as _immediateCallback] (timers.js:596:5)

If I disable dropBufferSupport, it works normally, but will throw WARN message:

[WARN] ioredis is using hiredis parser, however "dropBufferSupport" is disabled. 
It's highly recommended to enable this option. 
Refer to https://github.com/luin/ioredis/wiki/Improve-Performance for more details.
mcollina commented 7 years ago

Mosca needs buffer support, but it currently do not depend on hiredis (old versions did). Check your package.json and remove that.