njh / ruby-mqtt

Pure Ruby gem that implements the MQTT protocol, a lightweight protocol for publish/subscribe messaging.
http://www.rubydoc.info/gems/mqtt
MIT License
541 stars 135 forks source link

Problems with non-ASCII strings #102

Closed YtvwlD closed 6 years ago

YtvwlD commented 7 years ago

When I run

client.get do |topic,message|
    puts message.encoding

this, I get ASCII-8BIT. And subsequent string uses may fail with non-ASCII strings.

My code works when I do message = message.force_encoding('utf-8'). But this is not optimal.

njh commented 7 years ago

Hi Niklas,

The payload of a MQTT message is binary (it can't be assumed to be ASCII or UTF-8). Unfortunately Ruby does not have an 'array of bytes' class like other languages do - for example node.js has Buffer. So at the moment, just create a string without specifying a type for it.

I did have a plan to add payload type conversion (see #60) - so maybe passing in :utf8 as the type could be a solution. Or maybe it is better to assume the buffer is utf8 in the first place is a better default.

nick.

YtvwlD commented 7 years ago

Oh, okay. That sounds reasonable. I just assumed that all strings were UTF-8 because of this comment.

njh commented 7 years ago

Ah, yeah. But the payload isn't a string 😨