shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
552 stars 179 forks source link

Is it possible to connect to mosquitto running on my Raspberry Pi *locally* through the web app? #290

Closed Tejas-MD closed 3 years ago

Tejas-MD commented 3 years ago

I could connect to it via the hostname using the android build but could not with the web version.

Help please?

shamblett commented 3 years ago

If your running in the browser you are using websockets now, presumably you have read all the info in this regard, e.g. the URL must start with ws:// or wss://, the port mosquitto uses for this is 8080/8081, you have set your websocket headers appropriately. Also IIRC if you are using a local install of mosquitto it may or may not have been built with websocket support, some downloadable versions don't support this.

If you are still getting problems I'll need a client log to get any further.

Tejas-MD commented 3 years ago

Hi, sorry for the delayed response. I did try Opening a listener websockets port on raspi at port 1884. Though MQTT BOX (MQTT test client on my pc) connected to the raspi, the flutter web app did not.

I will try opening port 8080 instead of 1884 on my raspi for websockets and also post the client log soon.

shamblett commented 3 years ago

Don't forget mosquitto as the examples show runs a test server on test.mosquitto.org, see here for details. This server supports web sockets, you can always point your flutter app to this and see if it connects, if it does then you can try the Raspberry Pi.

Tejas-MD commented 3 years ago

Thanks for the reply. wss://test.mosquito.org works on port 8081. (Since my web app is hosted on Firebase, I had to use wss)

But the client does not connect to the Raspberry pi even in the debug build.

On Wed, 23 Jun 2021, 10:47 Steve Hamblett, @.***> wrote:

Don't forget mosquitto as the examples show runs a test server on test.mosquitto.org, see here https://test.mosquitto.org/ for details. This server supports web sockets, you can always point your flutter app to this and see if it connects, if it does then you can try the Raspberry Pi.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/shamblett/mqtt_client/issues/290#issuecomment-866529651, or unsubscribe https://github.com/notifications/unsubscribe-auth/APXTWWV6R4TQVMXRFP7GKFTTUFVANANCNFSM467CTESA .

shamblett commented 3 years ago

So are you saying you have a problem with the client or not?

Tejas-MD commented 3 years ago

I do have a problem with the client. It connects well with test.mosquitto.org but not mosquito hosted on my local raspberry pi.

Tejas-MD commented 3 years ago

Here's my client log: the hostname on my raspi is : axintra

1-2021-06-23 17:18:50.171 -- MqttConnectionHandlerBase::connect - server ws://axintra, port 8080 1-2021-06-23 17:18:50.173 -- SynchronousMqttBrowserConnectionHandler::internalConnect entered 1-2021-06-23 17:18:50.175 -- SynchronousMqttBrowserConnectionHandler::internalConnect - initiating connection try 0, auto reconnect in progress false 1-2021-06-23 17:18:50.177 -- SynchronousMqttBrowserConnectionHandler::internalConnect - calling connect 1-2021-06-23 17:18:50.180 -- MqttBrowserWsConnection::connect - entered 1-2021-06-23 17:18:50.182 -- MqttBrowserWsConnection::connect - WS URL is ws://axintra:8080 1-2021-06-23 17:18:50.192 -- MqttBrowserWsConnection::connect - connection is waiting 1-2021-06-23 17:18:50.858 -- MqttBrowserWsConnection::connect - websocket has erred 1-2021-06-23 17:18:50.859 -- SynchronousMqttBrowserConnectionHandler::internalConnect - connection complete 1-2021-06-23 17:18:50.862 -- SynchronousMqttBrowserConnectionHandler::internalConnect sending connect message 1-2021-06-23 17:18:50.865 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0 Connect Variable Header: ProtocolName=MQIsdp, ProtocolVersion=3, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=true, WillFlag=false, WillQos=MqttQos.atMostOnce, WillRetain=false, PasswordFlag=false, UserNameFlag=false, KeepAlive=60 MqttConnectPayload - client identifier is : zLKDz5B 1-2021-06-23 17:18:50.879 -- SynchronousMqttBrowserConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none 1-2021-06-23 17:18:55.885 -- SynchronousMqttBrowserConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none

Edit: I can confirm I have websockets enabled on my raspi because I can connect to it via websockets with another client.

shamblett commented 3 years ago

You seem to have an error on connection and your broker has not responded to the connect message -

1-2021-06-23 17:18:50.879 -- SynchronousMqttBrowserConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
1-2021-06-23 17:18:55.885 -- SynchronousMqttBrowserConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none

It seems after 5 minutes no response been received. from your broker. I as k again, why when you know the client works OK with the mosquitto test server do you think the client is at fault here, it looks more like its your broker/networks etc.

Tejas-MD commented 3 years ago

image This is a screenshot of MQTTBOX , A trusted MQTT testing client. It is right now connected to ws://axintra using websockets on the same network and PC where I am building the flutter web app.

Also, the android build on my emulator and physical android device connects to axintra easily.

This is why I think there might be something wrong with BrowserClient

shamblett commented 3 years ago

This is not a mosquitto broker, as I have said above have you checked what MQTT Headers it needs for operation over websockets? You are getting this on connect -

-2021-06-23 17:18:50.858 -- MqttBrowserWsConnection::connect - websocket has erred
Tejas-MD commented 3 years ago

This IS a mosquito broker. I have personally installed and have it running on the raspberry pi

On Thu, Jun 24, 2021, 1:36 PM Steve Hamblett @.***> wrote:

This is not a mosquitto broker, as I have said above have you checked what MQTT Headers it needs for operation over websockets? You are getting this on connect -

-2021-06-23 17:18:50.858 -- MqttBrowserWsConnection::connect - websocket has erred

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/shamblett/mqtt_client/issues/290#issuecomment-867431151, or unsubscribe https://github.com/notifications/unsubscribe-auth/APXTWWVSM7XJSRMXERWSKZ3TULRQBANCNFSM467CTESA .

shamblett commented 3 years ago

OK so then what's the difference between the one you have installed and the one on test.mosquitto.org where the client works? There must be some difference somewhere. What do your broker logs say on the connection attempt?

Tejas-MD commented 3 years ago

Sorry for the delay, I tried a loot of things to fix it, none worked :-(

Initial Broker log: image

On Connecting with example sketch of mqtt_client: image

shamblett commented 3 years ago

OK, what does the broker log print when you connect using MQTT Box? i.e. what does a good connection look like. What does the '' mean, I'm guessing its the client identifier which it wouldn't know at this point.

Also, change your hostname from 'ws://axintra' to 'ws://test.mosquitto.org' without doing anything else, does that work?

Tejas-MD commented 3 years ago

If I just change it to ws://test.mosquito.org without changing anything, it connects and runs the example well.

I'm not sure why it says < unknown > though.

shamblett commented 3 years ago

So, why do you think this is error in the client? It could be networking or anything. Remember the Dart runtime handles the network layer, it controls the socket layer, not the client package , just because some other utility works doesn't necessarily mean Dart will.

Tejas-MD commented 3 years ago

I definitely don't accuse the package. I am not entirely sure if it's the package's fault but because another client on the same device can connect to the server, why can't this?

Is there a way to get the further internal logs as to why there is a socket error?

shamblett commented 3 years ago

Not from Dart that I know, you could use traditional network troubleshooting such as wireshark etc. to see what's going out on the wire when you try and connect.

AMuellerAtAHS commented 3 years ago

I had similar problems until I found this answer on stackoverflow. You need to set a websocketProtocol, if you want to use MqttBrowserClient with flutter web on raspberry. May this could help anyone else.

Tejas-MD commented 3 years ago

Wow! This is the exact same problem I have. Will definitely try your solution out and let you know.

THANKS A TON @AMuellerAtAHS , I really was trying super hard to find a solution.