Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Binary payloads not supported? #109

hudgins opened this Issue Jun 12, 2013 · 7 comments


None yet
6 participants

hudgins commented Jun 12, 2013

I might be missing something, but it seems binary payloads are not supported because of this line, where we are converting the buffer to a utf8 string:


Leaving it as a buffer solves my problem, eg:

packet.payload = buf.slice(pos, len);

What is the thinking here? A simple test like so reproduces the problem:

var fs = require('fs');
var mqtt = require('mqtt');
var client = mqtt.createClient();


client.on('message', function(topic, message) {
  console.log('received message on topic', topic);
  fs.writeFile('./file.bin', message);

fs.readFile(process.argv[2], function(err, buffer) {
  client.publish('buffer', buffer);

agree, the user should do the encoding/decoding

I also run into this problem, maybe provide an option for the user to get binary payloads.


adamvr commented Jun 13, 2013

Yeah, this was just a cut corner to get things working quickly. Perhaps a setEncoding method on Connection?

@adamvr adamvr referenced this issue Jun 13, 2013


Set encoding #111

hudgins commented Jun 13, 2013

I see, makes sense. Turns out I habitually send stringified JSON when not sending binary, and apparently JSON.parse() can handle a Buffer containing a string.

This was referenced Aug 8, 2013

@mcollina mcollina closed this in #118 Aug 21, 2013


wolfeidau commented Sep 2, 2013

So is there an example or test using an encoding other than utf8? I am trying to send binary messages.

Basically want to try out msgpack.



wolfeidau commented Sep 2, 2013


I found it.

    client = mqtt.createClient({encoding: 'binary'});

If you like I will update the readme and add a test.


mcollina commented Sep 2, 2013

Please do so! Thanks a million!

Il giorno lunedì 2 settembre 2013, Mark Wolfe ha scritto:


I found it.

client = mqtt.createClient({encoding: 'binary'});

If you like I will update the readme and add a test.

Reply to this email directly or view it on GitHubhttps://github.com/adamvr/MQTT.js/issues/109#issuecomment-23654639

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment