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

Mosquitto 2.0.4 WebSockets connect failed, but 1.6.2 is works. #1974

Closed
028820 opened this issue Dec 23, 2020 · 9 comments
Closed

Mosquitto 2.0.4 WebSockets connect failed, but 1.6.2 is works. #1974

028820 opened this issue Dec 23, 2020 · 9 comments
Milestone

Comments

@028820
Copy link

028820 commented Dec 23, 2020

I working on 1.6.2, when I upgrade to 2.0.4, I got a problem, WebSockets connect failed.
I'm missing some configuration at 2.0.4 ?

exmaple configuration on the below

mosquitto.conf

listener 1883
protocol mqtt
listener 9001
protocol websockets
log_type all

Mosquitto 1.6.2 WebSockets is works

# run mosquitto 1.6.2 image
docker run -d -it --name mosquitto -p 1883:1883 -p 9001:9001 eclipse-mosquitto:1.6.2

docker logs mosquitto -f

1608707499: mosquitto version 1.6.2 starting
1608707499: Config loaded from /mosquitto/config/mosquitto.conf.
1608707499: Opening ipv4 listen socket on port 1883.
1608707499: Opening ipv6 listen socket on port 1883.
1608707499: Opening websockets listen socket on port 9001.


1608707587: New client connected from ::ffff:172.17.0.1 as test (p2, c1, k60).
1608707587: No will message specified.
1608707587: Sending CONNACK to test (0, 0)
1608707600: Received PUBLISH from test (d0, q0, r0, m0, 'test', ... (20 bytes))
1608707660: Received PINGREQ from test
1608707660: Sending PINGRESP to test
1608707720: Received PINGREQ from test
1608707720: Sending PINGRESP to test

Mosquitto 2.0.4 WebSockets connect failed

# run mosquitto 2.0.4
docker run -d -it --name mosquitto -p 1883:1883 -p 9001:9001 eclipse-mosquitto:2.0.4


docker logs 469d75750156 -f
1608707835: mosquitto version 2.0.4 starting
1608707835: Config loaded from /mosquitto/config/mosquitto.conf.
1608707835: Opening ipv4 listen socket on port 1883.
1608707835: Opening ipv6 listen socket on port 1883.
1608707835: Opening websockets listen socket on port 9001.
1608707835: mosquitto version 2.0.4 running

1608707913: Client <unknown> closed its connection.

MQTT GUI Client

mqttx.app

@ralight
Copy link
Contributor

ralight commented Dec 27, 2020

You should note that version 2.0 onwards will default to allow_anonymous false, so your client is not authorised to connect. Please see https://mosquitto.org/documentation/migrating-to-2-0/ for details of differences in behaviour between 1.x and 2.0.

@028820
Copy link
Author

028820 commented Dec 28, 2020

You should note that version 2.0 onwards will default to allow_anonymous false, so your client is not authorised to connect. Please see https://mosquitto.org/documentation/migrating-to-2-0/ for details of differences in behaviour between 1.x and 2.0.

Ehh, The mosquitto.conf just for simplify, It's my mistake。

Actually I had both tried allow_anonymous equals true or false.
it's seems like a bug or docker image problem, may be image build problem.

In the below, I post complete mosquitto.conf configuration and docker command.

mosquitto.conf


allow_anonymous false

acl_file /mosquitto/config/acl.conf
password_file /mosquitto/config/passwd.conf

max_keepalive 90



# If a client sends a packet which is larger than this value, it will be disconnected
max_packet_size 5120
# Dropped too large PUBLISH 
message_size_limit 5120

max_queued_bytes 50
# max queue size
max_queued_messages 1000
allow_zero_length_clientid false
connection_messages true

log_type error
log_type warning
log_type notice
log_type subscribe
log_type unsubscribe


# websocket
listener 9001
protocol websockets
websockets_log_level all


# websocket ssl
listener 9003
protocol websockets
cafile /mosquitto/config/ssl/chain.pem
keyfile /mosquitto/config/ssl/dev.fiture.com.key
certfile /mosquitto/config/ssl/dev.fiture.com.pem

# mqtt
listener 1883
# mqtt ssl
listener 8883
protocol mqtt
cafile /mosquitto/config/ssl/chain.pem
keyfile /mosquitto/config/ssl/dev.xxxx.com.key
certfile /mosquitto/config/ssl/dev.xxxx.com.pem

Docker Command


# mosquitto 1.6.12, websocket is works
docker run -d -it --name mosquitto -p 1883:1883 -p 8883:8883 -p 9003:9003 -p 9001:9001 -v /Users/david/data/mosquitto:/mosquitto eclipse-mosquitto:1.6.12-openssl


# mosquitto 2.0.4,  websocket is connect failed
docker run -d -it --name mosquitto2 -p 1883:1883 -p 8883:8883 -p 9003:9003 -p 9001:9001 -v /Users/david/data/mosquitto:/mosquitto eclipse-mosquitto:2.0.4-openssl

The mosquitto 2.0.4 docker logs.

image

Do I need to provide more information?

@ralight
Copy link
Contributor

ralight commented Dec 28, 2020

I confess I only had a quick glance and didn't actually check it, what with it being holiday here. I'll have a proper look.

@028820
Copy link
Author

028820 commented Dec 28, 2020

I confess I only had a quick glance and didn't actually check it, what with it being holiday here. I'll have a proper look.

hha, in fact the version 1.6.12 works well for me, that performance is awesome, especial shared subscriptions feature of mqtt 5.

It's help me to easy build high available, scale-out IoT system. based-on mqtt brokers routing table and message routing (a little work), that's bottleneck is just only bandwidth.

I try upgrade to 2.0, just only try it's, not must.

Thank you for your to mqtt contributions, happy holiday to you :P

@ralight
Copy link
Contributor

ralight commented Dec 29, 2020

I've found the problem now, it's a typo in calculating the end of the remaining length part of the packet, for websocket clients only. The fun part is that most clients won't see that problem.

mqttx does see the problem because it is pretty badly behaved in how it sends packets - it sends a new websockets packet (and a new TCP packet) for each part of each MQTT packet. For example, a CONNECT packet with authentication has: command, remaining length, length of protocol string, protocol string, protocol level, connect flags, keepalive, length of client id, client id, length of username, username, length of password, password. Each one of those ends up being a new TCP packet. If you use the default mqttx client id and username and password as your credentials, this gives you a total on the wire cost of 1010 bytes to transmit a 48 byte MQTT command, when it could be as little as 122 bytes for the on the wire cost. Ouch!

Still, I'm grateful for this behaviour because this might not have got found for a while otherwise.

I'll get the fix into 2.0.5, most likely in early January.

@ralight ralight added this to the 2.0.5 milestone Dec 29, 2020
@ralight ralight closed this as completed Jan 12, 2021
ralight added a commit that referenced this issue Jan 12, 2021
Only affects those clients that send fragmented packets.

Closes #1974. Thanks to 贺亚东.
@ralight ralight modified the milestones: 2.0.5, 2.0.6 Jan 12, 2021
@ralight
Copy link
Contributor

ralight commented Jan 12, 2021

I'm afraid this got missed for 2.0.5, I thought I'd already fixed it. I've done it now for 2.0.6.

@yschroeder
Copy link

Hi @ralight, long time no see :-)

I just happened to update my mosquitto to a recent version (after that libwebsockets regression was finally sorted out with a new release on their side). I can confirm that 2.0.5 does not work when sending large messages via websockets (small messages are fine).

Current master (f1180dd) works for all message sizes (tried a few megabytes in size).

By the way: the problem also exist(ed) with ngx-mqtt (https://www.npmjs.com/package/ngx-mqtt) not only mqttx.

@ralight
Copy link
Contributor

ralight commented Jan 29, 2021

@yschroeder thanks for the feedback, it's good to hear 2.0.6 fixes that for you.

@therealsmat
Copy link

therealsmat commented Jul 3, 2021

I have the same issue as @028820 in 2.0.10.

Pub/Sub works perfectly via MQTT but does not work on WebSockets even with the same credentials.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants