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

MQTT 5.0 cannot subscribe: Cannot parse topic #24

Closed
miqmago opened this issue Mar 10, 2019 · 3 comments
Closed

MQTT 5.0 cannot subscribe: Cannot parse topic #24

miqmago opened this issue Mar 10, 2019 · 3 comments

Comments

@miqmago
Copy link

miqmago commented Mar 10, 2019

I'm trying to use MQTT 5.0 but I'm always getting the same error: Cannot parse topic

        client.connect({
            clientId: activeInterface.mac_address.replace(/:/ig, '').toUpperCase(),
            username: 'bla',
            password: 'bla',
            protocolId: 'MQTT',
            protocolVersion: 5,
        });

        client.on('connack', () => {
            console.log('client connected... sending subscribe');
            client.subscribe({
                messageId: 0,
                subscriptions: [{
                    topic: 'whatever',
                    qos: 0,
                    nl: false, // no Local MQTT 5.0 flag
                    rap: true, // Retain as Published MQTT 5.0 flag
                    rh: 1, // Retain Handling MQTT 5.0
                }],
            });
        });

I've add this line in parser.js:466 for debugging:

console.log(length, end, this._list.length, this.packet.length, this._list.toString('utf8'));

It always fails (whatever topic I place):

2167 2172 13 13 '\u0000\u0000\u0000\bwhatever\u0000'
client error Error: Cannot parse topic
    at Parser._parseSubscribe (/node_modules/mqtt-connection/node_modules/mqtt-packet/parser.js:304:48)
    at Parser._parsePayload (/node_modules/mqtt-connection/node_modules/mqtt-packet/parser.js:99:14)
    at Parser.parse (/node_modules/mqtt-connection/node_modules/mqtt-packet/parser.js:39:41)
    at DestroyableTransform.process [as _transform] (/node_modules/mqtt-connection/lib/parseStream.js:14:17)
    at DestroyableTransform.Transform._read (/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:184:10)
    at DestroyableTransform.Transform._write (/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:172:83)
    at doWrite (/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:428:64)
    at writeOrBuffer (/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:417:5)
    at DestroyableTransform.Writable.write (/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:334:11)
    at Socket.ondata (_stream_readable.js:639:20)

If I switch to MQTT lower version it works perfectly...

@miqmago
Copy link
Author

miqmago commented Mar 12, 2019

If I place a console.log at _parseNum parser.js:499 it seems that _pos is one byte misfit:

  console.log(this._list._bufs);
  console.log(this._list._bufs.map(b => b.toString()), this._pos, result);
[ <Buffer 00 00 00 08 77 68 61 74 65 76 65 72 00> ]
[ '\u0000\u0000\u0000\bwhatever\u0000' ] 3 2167

00 08 = 8 (= 'whatever'.length)
08 77 = 2167

@miqmago
Copy link
Author

miqmago commented Mar 12, 2019

Seems a problem of options:
When the packet is receiving connack, it calls setOptions with connectPacket. I don't see a way to return protocol version from server in connack, or the connack from client is not getting original options or assigning original options to packet. That way, protocolVersion is being lost.

I've ended by overriding options manually, doing this:

    mqttConnection.on('connack', this.conAck.bind(this));

    conAck(resp) {
        this.mqttConnection.setOptions({
            protocolVersion: 5,
        });
        this.mqttConnection.subscribe({
            messageId: 1,
            subscriptions: [{
                topic: 'whatever',
                qos: 0,
                nl: false, // no Local MQTT 5.0 flag
                rap: true, // Retain as Published MQTT 5.0 flag
                rh: 1, // Retain Handling MQTT 5.0
            }],
        });
    }

@miqmago
Copy link
Author

miqmago commented Mar 18, 2019

Problem reported at mqttjs/mqtt-packet#50

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

No branches or pull requests

1 participant