node-red icon indicating copy to clipboard operation
node-red copied to clipboard

mqtt nodes handling EVRYTHNG topics

Open tyoder opened this issue 11 years ago • 15 comments

Hi node-red folks,

EVRYTHNG is creating THNG Push - see here: https://dashboard.evrythng.com/developers/apidoc-beta#pubsub

Using Mosquitto, I can successfully publish and subscribe to Topics which correspond to the properties of my Thngs at EVRYTHNG - very cool...

However, there seems to be some problem with the Node-Red mqtt nodes and how I am configuring the Topics, because I haven't been able to make it work. Part of the problem might be the structure of the url for each Topic and its pure length. For example, here is what I need to use to publish to an EVRYTHNG Topic using mosquitto_pub:

mosquitto_pub -h pubsub.evrythng.com -t thngs/523ed9f9e4b065f4854d06fe/properties/state?access_token=MyApiKeyWhichIs90CharactersLong -m '[{"value": "saturday"}]'

Thank you in advance for any hints you can give me. btw, node-red + Thng Push = omg....YES!!!!

tyoder avatar Feb 01 '15 12:02 tyoder

You have not actually stated what problem you are seeing here. Please explain clearly what you are seeing and what you expect to happen.

hardillb avatar Feb 01 '15 12:02 hardillb

Sure...as I mentioned, using mosquitto_pub and mosquitto_sub, I can update a Thng property by publishing a msg and then successfully receive the msg.

Maybe the easiest way for you to diagnose would be for me to export the flow and share it with you. I will do that and take out my API key.

In the meantime, the behavior I am seeing is that the mqtt nodes seem to connect successfully and then disconnect. Here are some logs from last night:

31 Jan 20:15:51 - [red] Stopping flows 31 Jan 20:15:51 - [red] Starting flows 31 Jan 20:15:51 - [mqtt] [1b759507.e48a6b] disconnected from broker tcp://pubsub.evrythng.com:1883 31 Jan 20:15:52 - [mqtt] [c1c162fa.3e3ea] connected to broker tcp://pubsub.evrythng.com:1883 31 Jan 20:18:54 - [red] Stopping flows 31 Jan 20:18:54 - [red] Starting flows 31 Jan 20:18:54 - [mqtt] [c1c162fa.3e3ea] disconnected from broker tcp://pubsub.evrythng.com:1883 31 Jan 20:18:54 - [mqtt] [7ece6c68.813194] connected to broker tcp://pubsub.evrythng.com:1883 31 Jan 20:18:55 - [mqtt] [7ece6c68.813194] connection lost to broker tcp://pubsub.evrythng.com:1883 31 Jan 20:18:59 - [mqtt] [7ece6c68.813194] connected to broker tcp://pubsub.evrythng.com:1883 31 Jan 20:18:59 - [mqtt] [7ece6c68.813194] connection lost to broker tcp://pubsub.evrythng.com:1883 31 Jan 20:19:03 - [mqtt] [7ece6c68.813194] connected to broker tcp://pubsub.evrythng.com:1883 31 Jan 20:19:03 - [mqtt] [7ece6c68.813194] connection lost to broker tcp://pubsub.evrythng.com:1883 31 Jan 20:19:10 - [mqtt] [7ece6c68.813194] connected to broker tcp://pubsub.evrythng.com:1883 31 Jan 20:23:38 - [red] Stopping flows 31 Jan 20:23:38 - [red] Starting flows 31 Jan 20:23:38 - [mqtt] [3d3abc61.c2c544] connected to broker tcp://localhost:1883 31 Jan 20:23:38 - [mqtt] [7ece6c68.813194] disconnected from broker tcp://pubsub.evrythng.com:1883 31 Jan 20:23:38 - [mqtt] [3251e72a.cdae18] connected to broker tcp://pubsub.evrythng.com:1883

tyoder avatar Feb 01 '15 13:02 tyoder

hardillb, My name is Terry btw...

Here is the flow for a simple locahost pub that works:

[{"id":"7227ec1f.8dd814","type":"mqtt-broker","broker":"localhost","port":"1883","clientid":""},{"id":"3c201e69.c3dfe2","type":"mqtt out","name":"Test Pub","topic":"hello/world","qos":"0","retain":"false","broker":"7227ec1f.8dd814","x":352,"y":68,"z":"f41b72a.f0be49","wires":[]},{"id":"c8fb0a4a.3704f8","type":"inject","name":"Test properties","topic":"","payload":"'[{"value": "one"}]'","payloadType":"string","repeat":"","crontab":"","once":true,"x":133,"y":69,"z":"f41b72a.f0be49","wires":[["3c201e69.c3dfe2"]]}]

And here is the flow for attempting with Evrythng. Note that my API key has been removed. It is 90 characters long...

[{"id":"13f3de5b.ec0c22","type":"mqtt-broker","broker":"pubsub.evrythng.com","port":"1883","clientid":""},{"id":"8055ceb9.7faa3","type":"mqtt out","name":"Pub to Evrythng","topic":"thngs/523ed9f9e4b065f4854d06fe/properties/state?access_token=MyApiKeyWhichIs90CharsLong","qos":"0","retain":"false","broker":"13f3de5b.ec0c22","x":366,"y":94,"z":"4d21cc6f.b2de34","wires":[]},{"id":"48b64d33.b749b4","type":"inject","name":"","topic":"","payload":"'[{"value": "foo"}]'","payloadType":"string","repeat":"","crontab":"","once":true,"x":120,"y":93,"z":"4d21cc6f.b2de34","wires":[["8055ceb9.7faa3"]]}]

Before running, the connection looks good: image

As soon as I run the flow, it disconnects: image

Then, after a few seconds, it connects successfully again.

Logs: 1 Feb 08:34:57 - [mqtt] [4f3933c2.b0c6cc] connected to broker tcp://pubsub.evrythng.com:1883 1 Feb 08:36:51 - [mqtt] [4f3933c2.b0c6cc] connection lost to broker tcp://pubsub.evrythng.com:1883 1 Feb 08:37:07 - [mqtt] [4f3933c2.b0c6cc] connected to broker tcp://pubsub.evrythng.com:1883

Thanks, Terry

tyoder avatar Feb 01 '15 13:02 tyoder

Even using the same extra long topic on a local instance of mosquitto, this looks to all be working fine for me. Since I don't have a thng api key I can't test but my first assumption is that this is something specific with their broker implementation. The Mosquitto examples they have wouldn't show a problem if they are sending back a strange message ack or closing the connection after the publish. Please try running the following:

mosquitto_pub -d -h pubsub.evrythng.com -t thngs/523ed9f9e4b065f4854d06fe/properties/state?access_token=MyApiKeyWhichIs90CharactersLong -l

The -d turns on verbose mode, -l waits and reads from stdin then sends each line as a new message. paste in one of the message bodies e.g. [{"value": "saturday"}] and see what happens. This should show if the connection is getting dropped by their broker after the pub arrives and how they are reponding. (ctrl-d should stop mosquitto_pub afterwards)

hardillb avatar Feb 01 '15 14:02 hardillb

Hi Ben, First of all, I'm impressed that someone got back to me so quickly - THANKS!

I ran the experiment and got (what I think) are good results:

Publisher:

Minitab-CSs-iMac:~ terry$ mosquitto_pub -d -h pubsub.evrythng.com -t thngs/523ed9f9e4b065f4854d06fe/properties/state?access_token=ApiToken -l

Client mosqpub/6870-Minitab-CS sending CONNECT

Client mosqpub/6870-Minitab-CS received CONNACK

[{"value": "brot"}]

Client mosqpub/6870-Minitab-CS sending PUBLISH (d0, q0, r0, m1, 'thngs/523ed9f9e4b065f4854d06fe/properties/state?access_token=ApiToken', ... (19 bytes))

[{"value": "milk"}]

Client mosqpub/6870-Minitab-CS sending PUBLISH (d0, q0, r0, m2, 'thngs/523ed9f9e4b065f4854d06fe/properties/state?access_token=ApiToken', ... (19 bytes))

^C

Subscriber:

Minitab-CSs-iMac:~ terry$ mosquitto_sub -d -h pubsub.evrythng.com -t thngs/523ed9f9e4b065f4854d06fe/properties?access_token=MyApiKey

Client mosqsub/6863-Minitab-CS sending CONNECT

Client mosqsub/6863-Minitab-CS received CONNACK

Client mosqsub/6863-Minitab-CS sending SUBSCRIBE (Mid: 1, Topic: thngs/523ed9f9e4b065f4854d06fe/properties?access_token=MyApiKey, QoS: 0)

Client mosqsub/6863-Minitab-CS received SUBACK

Subscribed (mid: 1): 0

Client mosqsub/6863-Minitab-CS received PUBLISH (d0, q0, r0, m0, 'thngs/523ed9f9e4b065f4854d06fe/properties', ... (60 bytes))

[{"timestamp":1422801582745,"key":"state","value":"speise"}]

Client mosqsub/6863-Minitab-CS sending PINGREQ

Client mosqsub/6863-Minitab-CS received PINGRESP

Client mosqsub/6863-Minitab-CS sending CONNECT

Client mosqsub/6863-Minitab-CS received CONNACK

Client mosqsub/6863-Minitab-CS sending SUBSCRIBE (Mid: 2, Topic: thngs/523ed9f9e4b065f4854d06fe/properties?access_token=MyApiKey, QoS: 0)

Client mosqsub/6863-Minitab-CS received SUBACK

Subscribed (mid: 2): 0

Client mosqsub/6863-Minitab-CS received PUBLISH (d0, q0, r0, m0, 'thngs/523ed9f9e4b065f4854d06fe/properties', ... (58 bytes))

[{"timestamp":1422801696491,"key":"state","value":"brot"}]

Client mosqsub/6863-Minitab-CS received PUBLISH (d0, q0, r0, m0, 'thngs/523ed9f9e4b065f4854d06fe/properties', ... (58 bytes))

[{"timestamp":1422801731325,"key":"state","value":"milk"}]

Client mosqsub/6863-Minitab-CS sending PINGREQ

Client mosqsub/6863-Minitab-CS received PINGRESP

Client mosqsub/6863-Minitab-CS sending CONNECT

Client mosqsub/6863-Minitab-CS received CONNACK

Client mosqsub/6863-Minitab-CS sending SUBSCRIBE (Mid: 3, Topic: thngs/523ed9f9e4b065f4854d06fe/properties?access_token=MyApiKey, QoS: 0)

Client mosqsub/6863-Minitab-CS received SUBACK

Subscribed (mid: 3): 0

Client mosqsub/6863-Minitab-CS sending CONNECT

Client mosqsub/6863-Minitab-CS received CONNACK

Client mosqsub/6863-Minitab-CS sending SUBSCRIBE (Mid: 4, Topic: thngs/523ed9f9e4b065f4854d06fe/properties?access_token=MyApiKey, QoS: 0)

Client mosqsub/6863-Minitab-CS received SUBACK

Subscribed (mid: 4): 0

Client mosqsub/6863-Minitab-CS sending CONNECT

Client mosqsub/6863-Minitab-CS received CONNACK

Client mosqsub/6863-Minitab-CS sending SUBSCRIBE (Mid: 5, Topic: thngs/523ed9f9e4b065f4854d06fe/properties?access_token=MyApiKey, QoS: 0)

Client mosqsub/6863-Minitab-CS received SUBACK

Subscribed (mid: 5): 0

^C

On Sun, Feb 1, 2015 at 9:25 AM, Ben Hardill [email protected] wrote:

Even using the same extra long topic on a local instance of mosquitto, this looks to all be working fine for me. Since I don't have a thng api key I can't test but my first assumption is that this is something specific with their broker implementation. The Mosquitto examples they have wouldn't show a problem if they are sending back a strange message ack or closing the connection after the publish. Please try running the following:

mosquitto_pub -d -h pubsub.evrythng.com -t thngs/523ed9f9e4b065f4854d06fe/properties/state?access_token=MyApiKeyWhichIs90CharactersLong -l

The -d turns on verbose mode, -l waits and reads from stdin then sends each line as a new message. paste in one of the message bodies e.g. [{"value": "saturday"}] and see what happens. This should show if the connection is getting dropped by their broker after the pub arrives and how they are reponding. (ctrl-d should stop mosquitto_pub afterwards)

— Reply to this email directly or view it on GitHub https://github.com/node-red/node-red/issues/553#issuecomment-72366691.

About me http://about.me/tyoder

tyoder avatar Feb 01 '15 14:02 tyoder

Ben, If you send me your own email address, I will send you my API key. I will change it later. Besides, I'm just playing around - there is no risk. Terry

On Sun, Feb 1, 2015 at 9:51 AM, Terry Yoder [email protected] wrote:

Hi Ben, First of all, I'm impressed that someone got back to me so quickly - THANKS!

I ran the experiment and got (what I think) are good results:

Publisher:

Minitab-CSs-iMac:~ terry$ mosquitto_pub -d -h pubsub.evrythng.com -t thngs/523ed9f9e4b065f4854d06fe/properties/state?access_token=ApiToken -l

Client mosqpub/6870-Minitab-CS sending CONNECT

Client mosqpub/6870-Minitab-CS received CONNACK

[{"value": "brot"}]

Client mosqpub/6870-Minitab-CS sending PUBLISH (d0, q0, r0, m1, 'thngs/523ed9f9e4b065f4854d06fe/properties/state?access_token=ApiToken', ... (19 bytes))

[{"value": "milk"}]

Client mosqpub/6870-Minitab-CS sending PUBLISH (d0, q0, r0, m2, 'thngs/523ed9f9e4b065f4854d06fe/properties/state?access_token=ApiToken', ... (19 bytes))

^C

Subscriber:

Minitab-CSs-iMac:~ terry$ mosquitto_sub -d -h pubsub.evrythng.com -t thngs/523ed9f9e4b065f4854d06fe/properties?access_token=MyApiKey

Client mosqsub/6863-Minitab-CS sending CONNECT

Client mosqsub/6863-Minitab-CS received CONNACK

Client mosqsub/6863-Minitab-CS sending SUBSCRIBE (Mid: 1, Topic: thngs/523ed9f9e4b065f4854d06fe/properties?access_token=MyApiKey, QoS: 0)

Client mosqsub/6863-Minitab-CS received SUBACK

Subscribed (mid: 1): 0

Client mosqsub/6863-Minitab-CS received PUBLISH (d0, q0, r0, m0, 'thngs/523ed9f9e4b065f4854d06fe/properties', ... (60 bytes))

[{"timestamp":1422801582745,"key":"state","value":"speise"}]

Client mosqsub/6863-Minitab-CS sending PINGREQ

Client mosqsub/6863-Minitab-CS received PINGRESP

Client mosqsub/6863-Minitab-CS sending CONNECT

Client mosqsub/6863-Minitab-CS received CONNACK

Client mosqsub/6863-Minitab-CS sending SUBSCRIBE (Mid: 2, Topic: thngs/523ed9f9e4b065f4854d06fe/properties?access_token=MyApiKey, QoS: 0)

Client mosqsub/6863-Minitab-CS received SUBACK

Subscribed (mid: 2): 0

Client mosqsub/6863-Minitab-CS received PUBLISH (d0, q0, r0, m0, 'thngs/523ed9f9e4b065f4854d06fe/properties', ... (58 bytes))

[{"timestamp":1422801696491,"key":"state","value":"brot"}]

Client mosqsub/6863-Minitab-CS received PUBLISH (d0, q0, r0, m0, 'thngs/523ed9f9e4b065f4854d06fe/properties', ... (58 bytes))

[{"timestamp":1422801731325,"key":"state","value":"milk"}]

Client mosqsub/6863-Minitab-CS sending PINGREQ

Client mosqsub/6863-Minitab-CS received PINGRESP

Client mosqsub/6863-Minitab-CS sending CONNECT

Client mosqsub/6863-Minitab-CS received CONNACK

Client mosqsub/6863-Minitab-CS sending SUBSCRIBE (Mid: 3, Topic: thngs/523ed9f9e4b065f4854d06fe/properties?access_token=MyApiKey, QoS: 0)

Client mosqsub/6863-Minitab-CS received SUBACK

Subscribed (mid: 3): 0

Client mosqsub/6863-Minitab-CS sending CONNECT

Client mosqsub/6863-Minitab-CS received CONNACK

Client mosqsub/6863-Minitab-CS sending SUBSCRIBE (Mid: 4, Topic: thngs/523ed9f9e4b065f4854d06fe/properties?access_token=MyApiKey, QoS: 0)

Client mosqsub/6863-Minitab-CS received SUBACK

Subscribed (mid: 4): 0

Client mosqsub/6863-Minitab-CS sending CONNECT

Client mosqsub/6863-Minitab-CS received CONNACK

Client mosqsub/6863-Minitab-CS sending SUBSCRIBE (Mid: 5, Topic: thngs/523ed9f9e4b065f4854d06fe/properties?access_token=MyApiKey, QoS: 0)

Client mosqsub/6863-Minitab-CS received SUBACK

Subscribed (mid: 5): 0

^C

On Sun, Feb 1, 2015 at 9:25 AM, Ben Hardill [email protected] wrote:

Even using the same extra long topic on a local instance of mosquitto, this looks to all be working fine for me. Since I don't have a thng api key I can't test but my first assumption is that this is something specific with their broker implementation. The Mosquitto examples they have wouldn't show a problem if they are sending back a strange message ack or closing the connection after the publish. Please try running the following:

mosquitto_pub -d -h pubsub.evrythng.com -t thngs/523ed9f9e4b065f4854d06fe/properties/state?access_token=MyApiKeyWhichIs90CharactersLong -l

The -d turns on verbose mode, -l waits and reads from stdin then sends each line as a new message. paste in one of the message bodies e.g. [{"value": "saturday"}] and see what happens. This should show if the connection is getting dropped by their broker after the pub arrives and how they are reponding. (ctrl-d should stop mosquitto_pub afterwards)

— Reply to this email directly or view it on GitHub https://github.com/node-red/node-red/issues/553#issuecomment-72366691.

About me http://about.me/tyoder

About me http://about.me/tyoder

tyoder avatar Feb 01 '15 15:02 tyoder

My username at gmail.com should find me

hardillb avatar Feb 01 '15 18:02 hardillb

OK, got the API key. Publishes seam to be working fine, without any disconnects.

Subscribes are looking a bit strange, they are working with mosquitto_sub but not from Node-RED.

I think this is because the subscribes replying without the api key on the topic. e.g.

thngs/523ed9f9e4b065f4854d06fe/properties

rather than

 thngs/523ed9f9e4b065f4854d06fe/properties?access_token=MyApiKey

I'll need to talk to somebody who understands the MQTT spec a bit better than me to see what should be happening here. But my gut feel is that the API key would be better used as a MQTT username rather than trying to use it as a "url" argument on the topic. I think the mosquitto_sub command is working because it's not checking the topics match when it gets a message. Node-RED multiplexes connections to the same broker from different nodes and sends on the messages to the correct nodes by matching the requested topics.

Also the mosquitto_sub client is being disconnected from time to time randomly as well, but the trace you sent also showed this:

[{"timestamp":1422801582745,"key":"state","value":"speise"}] 
Client mosqsub/6863-Minitab-CS sending PINGREQ 
Client mosqsub/6863-Minitab-CS received PINGRESP 
Client mosqsub/6863-Minitab-CS sending CONNECT 
Client mosqsub/6863-Minitab-CS received CONNACK 
Client mosqsub/6863-Minitab-CS sending SUBSCRIBE (Mid: 2, Topic: thngs/523ed9f9e4b065f4854d06fe/properties?access_token=MyApiKey, QoS: 0) 
Client mosqsub/6863-Minitab-CS received SUBACK

The CONNECT & CONNACK signify the disconnect and reconnect

hardillb avatar Feb 01 '15 18:02 hardillb

Hi Ben, So you got publish to work correctly? I couldn't make that work from node-red...only with mosquitto_pub. On Feb 1, 2015 1:55 PM, "Ben Hardill" [email protected] wrote:

OK, got the API key. Publishes seam to be working fine, without any disconnects.

Subscribes are looking a bit strange, they are working with mosquitto_sub but not from Node-RED.

I think this is because the subscribes replying without the api key on the topic. e.g.

thngs/523ed9f9e4b065f4854d06fe/properties

rather than

thngs/523ed9f9e4b065f4854d06fe/properties?access_token=MyApiKey

I'll need to talk to somebody who understands the MQTT spec a bit better than me to see what should be happening here. But my gut feel is that the API key would be better used as a MQTT username rather than trying to use it as a "url" argument on the topic. I think the mosquitto_sub command is working because it's not checking the topics match when it gets a message. Node-RED multiplexes connections to the same broker from different nodes and sends on the messages to the correct nodes by matching the requested topics.

Also the mosquitto_sub client is being disconnected from time to time randomly as well, but the trace you sent also showed this:

[{"timestamp":1422801582745,"key":"state","value":"speise"}] Client mosqsub/6863-Minitab-CS sending PINGREQ Client mosqsub/6863-Minitab-CS received PINGRESP Client mosqsub/6863-Minitab-CS sending CONNECT Client mosqsub/6863-Minitab-CS received CONNACK Client mosqsub/6863-Minitab-CS sending SUBSCRIBE (Mid: 2, Topic: thngs/523ed9f9e4b065f4854d06fe/properties?access_token=MyApiKey, QoS: 0) Client mosqsub/6863-Minitab-CS received SUBACK

The CONNECT & CONNACK signify the disconnect and reconnect

— Reply to this email directly or view it on GitHub https://github.com/node-red/node-red/issues/553#issuecomment-72378113.

tyoder avatar Feb 01 '15 19:02 tyoder

Yep, Publishing working fine for me using the latest Node-RED code from the git head stream

hardillb avatar Feb 01 '15 20:02 hardillb

OK, I've checked with somebody with an intimate understanding of the MQTT spec.

I believe that what THING are doing here is wrong, topics are not like URIs and should not support the passing of arguments like this. The broker should not be treating the ? character as something special.

The username field in the MQTT connection setup would be a better place to put the API key

This appears to work with mosquitto_sub because it is not validating that what it gets back from the broker matches the subscription it requested, there is no guarantee it will work with other clients (as shown by Node-RED)

hardillb avatar Feb 02 '15 11:02 hardillb

Ben, thanks for looking into this. I have contacted support at EVRYTHNG and shared this thread with them.

Thanks again! Terry

tyoder avatar Feb 02 '15 11:02 tyoder

Hi Terry and Ben,

Just to keep you in the loop here: indeed there is a problem with the way we treat topics are URIs (which would have been really elegant to treat all M2M protocols equal but I understand it is not in the standard!) We could reproduce it and we'll be fixing this really soon and will get back to you with the solution. Thanks a lot for your help to you both.

domguinard avatar Feb 03 '15 17:02 domguinard

Hi again,

We did fix this issue with a new release of our engine last night. You can now connect using the standard username / password method of MQTT and our broker does send the exact same topic string as the one used for subscribing.

The updated documentation is available here: https://dashboard.evrythng.com/developers/apidoc-beta/pubsub

I tested it on node red and it did work now. Thanks again for flagging the issue!

domguinard avatar Feb 13 '15 16:02 domguinard

@domguinard thanks for confirming!

Will close this issue.

knolleary avatar Feb 13 '15 16:02 knolleary