robhogan / react-native-paho-mqtt

react-native-paho-mqtt
91 stars 32 forks source link

Cannot connect to broker #5

Closed MattBlack85 closed 7 years ago

MattBlack85 commented 7 years ago

Hi, apparently it seems I cannot connect to any broker from my android emulator, every time I try to connect I get this error:

07-20 11:58:24.197  7089  7121 I ReactNativeJS: [Error: AMQJS0016E Invalid MQTT message type 0.]
07-20 11:58:24.197  7089  7121 I ReactNativeJS: onConnectionLost:undefined

I don't really get what's wrong, I just tried the given example with both the eclipse broker and my own broker but the result is the same. Any help would be very very appreciated

robhogan commented 7 years ago

Hi Matt. That's odd - you're hitting https://github.com/rh389/react-native-paho-mqtt/blob/master/src/ClientImplementation.js#L635 which means the client has received data through the websocket but it's not a valid MQTT packet.

You might try adding client._client.traceFunction = (trace) => console.log(trace.message); immediately after creating your client instance but before connecting. If you post the output that ought to give us more to go on.

MattBlack85 commented 7 years ago

Hi @rh389 and thanks for the quick answer. I did as you suggested and here is the output:

07-20 12:36:27.920  4985  5067 I ReactNativeJS: "Client.connect"{"willMessage":null,"timeout":30000,"keepAliveInterval":60,"cleanSession":true,"mqttVersion":3}false
07-20 12:36:28.562  4985  5067 I ReactNativeJS: "socket.send"{"options":{"willMessage":null,"timeout":30000,"keepAliveInterval":60,"cleanSession":true,"mqttVersion":3}}
07-20 12:36:28.715  4985  5067 I ReactNativeJS: "socket.onmessage"{}
07-20 12:36:28.716  4985  5067 I ReactNativeJS: "Client._handleMessage"{"messageIdentifier":null,"type":0}
07-20 12:36:28.716  4985  5067 I ReactNativeJS: "Client._disconnected"16"AMQJS0016E Invalid MQTT message type 0."
07-20 12:36:28.718  4985  5067 I ReactNativeJS: [Error: AMQJS0016E Invalid MQTT message type 0.]
robhogan commented 7 years ago

"socket.onmessage"{} indicates you're receiving an empty websocket frame (or event.data isn't populated for whatever reason). I've never seen that before from an MQTT broker. Two possibilities come to mind -

  1. These are websocket-level keepalive/ping frames that this lib should be ignoring. Or...
  2. You might not have the WebSocket library properly set up on your RN app, or maybe even a more general network problem.

You could try discarding the empty frames by modifying the source like this: https://github.com/rh389/react-native-paho-mqtt/commit/5edc6084812a2ff178446a81eab8ac60a5a94b5b

If that works for you I'll release it (can't do any harm). If it doesn't, then that suggests to me that there's something going wrong with the underlying websocket communication in your case.

MattBlack85 commented 7 years ago

I tried but it doesn't work, I also tried to add a new case 0 to the switch, it seems I get time outs

07-20 14:12:02.705  4860  4891 I ReactNativeJS: "Client._handleMessage"{"messageIdentifier":null,"type":0}
07-20 14:12:32.342  4860  4891 I ReactNativeJS: "Client._disconnected"1"AMQJSC0001E Connect timed out."
07-20 14:12:32.344  4860  4891 I ReactNativeJS: [Error: AMQJSC0001E Connect timed out.]
07-20 14:12:32.344  4860  4891 I ReactNativeJS: onConnectionLost:undefined

Will have a look at network/ws library

MattBlack85 commented 7 years ago

Also, I tried to use the node library mqtt.js and I can connect, publish and receive messages correctly to and from the same broker

robhogan commented 7 years ago

It makes sense that it'd timeout if the packet we're discarding is supposed to be the CONNACK. It looks like for whatever reason it's coming through without its payload.

I think the most likely explanation is an issue with your RN WebSocket setup. You could try stripping back a bit (just to test it out) by using the WebSocket API directly with a public server.

MattBlack85 commented 7 years ago

The raw WS API on the test server seems to work very well, I connect, send and get back the message

robhogan commented 7 years ago

I'm at a loss then I'm afraid - if you have a sample app you could point to I'll try it out.

MattBlack85 commented 7 years ago

let me build a new project with a single view that uses the mqtt client, will post here the link when I'll be done :)

MattBlack85 commented 7 years ago

@rh389 https://github.com/MattBlack85/MqttClient

robhogan commented 7 years ago

Well, I can reproduce what you're seeing but it's slightly baffling.

The data coming back (from iot.eclipse.org) is actually an ArrayBuffer with three bytes, all zero, which doesn't make much sense to me. I'm pretty sure that should be the CONNACK but the payload is getting mangled along the way (possibly only when using the mqtt websocket protocol). Still got a few more things to try...

MattBlack85 commented 7 years ago

This is probably what my own mosquitto broker is returning as well

robhogan commented 7 years ago

Ha - I bet it's this, fixed in RN yesterday: https://github.com/facebook/react-native/pull/15046

Which means it was broken by https://github.com/facebook/react-native/commit/93a1d592d61b229d3dafe57d1590d286312cc0bf, so it should work in RN <=0.45, but is broken in RN 0.46 and 0.47.

Unfortunately there's nothing this library can do to compensate for that - it's very much reliant on binary websockets so I think you'll either have to either downgrade RN or wait for 0.48.

MattBlack85 commented 7 years ago

@rh389 thanks a lot for the time you spent digging into this, we have downgraded and it works now :+1:

robhogan commented 7 years ago

No worries - good luck with your project (and thanks for the bug report)

robhogan commented 7 years ago

FYI - the fix for this landed in 0.47-rc.5, so it will be in 0.47 final.

ArbazYousuf commented 6 years ago

same issue but i want to connect with iot-aws serverless that deploy on amazon when ever i connect it throw a connectionLost error

"Client.connect"{"willMessage":null,"timeout":30000,"keepAliveInterval":60,"cleanSession":true,"mqttVersion":4}false
"Client._disconnected"7"AMQJS0007E Socket error: Unknown socket error."
serverless.js:122 onConnectionLost:Error: AMQJS0007E Socket error: Unknown socket error.