0

As the titles indicates my client is closing after receiving 0 data from broker. My broker uses a Self Signed Certificate. Next I will indicate my code and logs.

func selfSignedSSLSetting() { if mqttVesion == "3.1.1" { let clientID = "CocoaMQTT-\(animal!)-" mqtt = CocoaMQTT(clientID: clientID, host: defaultHost, port: 1883) mqtt!.username = "xxx" mqtt!.password = "xxx" mqtt!.keepAlive = 60 mqtt!.delegate = self mqtt!.allowUntrustCACertificate = true mqtt!.logLevel = .debug }else if mqttVesion == "5.0" { let clientID = "CocoaMQTT5-\(animal!)-" mqtt5 = CocoaMQTT5(clientID: clientID, host: defaultHost, port: 1883) let connectProperties = MqttConnectProperties() connectProperties.topicAliasMaximum = 0 connectProperties.sessionExpiryInterval = 0 connectProperties.receiveMaximum = 100 connectProperties.maximumPacketSize = 500 mqtt5!.connectProperties = connectProperties mqtt5!.logLevel = .debug mqtt5!.username = "xxx" mqtt5!.password = "xxx" mqtt5!.keepAlive = 60 mqtt5!.delegate = self mqtt5!.allowUntrustCACertificate = true } } 

Logs:

[TRACE] [didStateChangeTo]: new state: connecting CocoaMQTT(info): Connected to xx.xx.xx.xxx : 1883 CocoaMQTT(info): Enable backgrounding socket successfully CocoaMQTT(debug): SEND: CONNECT(id: CocoaMQTT-Sheep-, username: xxx, password: xxx, keepAlive : 60, cleansess: true) CocoaMQTT(debug): =========================MQTT 3.1.1========================= CocoaMQTT(debug): packetFixedHeaderType 16 CocoaMQTT(debug): remainingLen(len: len) [68] CocoaMQTT(debug): variableHeader something CocoaMQTT(debug): payload something CocoaMQTT(debug): ============================================================= CocoaMQTT(debug): socket wrote data 0 CocoaMQTT(debug): socket disconnected [TRACE] [didDisconnect]: Error Domain=NSPOSIXErrorDomain Code=54 "Connection reset by peer" UserInfo={NSLocalizedDescription=Connection reset by peer, NSLocalizedFailureReason=Error in read() function} [TRACE] [didStateChangeTo]: new state: disconnected 

I used this broker to connect a MQTT Client in Android using the PAHO lib and everything is working fine. Hope somebody could help indicating if is a problem in my code or a problem with the broker.

1
  • What do the MQTT broker logs show? Commented May 22, 2024 at 15:14

1 Answer 1

0

There could be a number of reasons, and it's unclear whether you're attempting to connect with MQTT v3.x or v5. If the server is unhappy with any of your connection settings it will simply disconnect you. With v3.x, there's no way for it to tell you why. It just drops the connection. With v5, it will usually send a disconnect message with a ReasonCode to indicate what it is unhappy about. So look for that.

But one thing I suspect is that you say that you're using a certificate, which implies a secure connection, but you seem to be attempting to connect to the broker on port 1883, which is usually the default port for unsecure TCP connections. Most brokers are configured for secure connections on a different port like 8883, or something else. Check the broker config.

Sign up to request clarification or add additional context in comments.

3 Comments

He posted the logs which says MQTT 3.1.1. But the secure connection might be a good point. Is there an equivalent for NSAllowsArbitraryLoads for MQTT connections?
Oh, yes, now I see that the v3.1.1 message is clearly about the outbound CONNECT packet. Can the client library log in more verbose detail? The connection happens in 3 steps: (1) The TCP connection is made, (2) The TLS session is negotiated, and (3) the connect packet is sent over the TLS connection. It would be good to know if the server drops during #2 (it rejects the TLS credentials) or #3 (it doesn't like something in the connect packet, perhaps username or password).
But I stand by my initial guess: 1883 is the IANA port number for unsecure (tcp) connections, while 8883 is the IANA port number for secure (tls) connections. Although plenty of companies have their brokers listening for secure connections on 443 or other ports.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.