Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary payloads not supported? #109

Closed
hudgins opened this issue Jun 12, 2013 · 7 comments · Fixed by #118
Closed

Binary payloads not supported? #109

hudgins opened this issue Jun 12, 2013 · 7 comments · Fixed by #118

Comments

@hudgins
Copy link

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:

https://github.com/adamvr/MQTT.js/blob/master/lib/parse.js#L122

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.subscribe('buffer');

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);
});
@carlhoerberg
Copy link

agree, the user should do the encoding/decoding

@tanpinsiang
Copy link

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

@adamvr
Copy link
Member

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 mentioned this issue Jun 13, 2013
@hudgins
Copy link
Author

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.

@wolfeidau
Copy link
Contributor

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.

Cheers

@wolfeidau
Copy link
Contributor

OK

I found it.

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

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

@mcollina
Copy link
Member

mcollina commented Sep 2, 2013

Please do so! Thanks a million!

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

OK

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//issues/109#issuecomment-23654639
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants