Skip to content
leeway edited this page Nov 19, 2021 · 1 revision

CocoaMQTT5

MQTT Client

public class CocoaMQTT5: NSObject, CocoaMQTT5Client 

Inheritance

CocoaMQTT5Client, CocoaMQTTDeliverProtocol, CocoaMQTTReaderDelegate, CocoaMQTTSocketDelegate, NSObject

Initializers

init(clientID:host:port:socket:)

Initial client object

public init(clientID: String, host: String = "localhost", port: UInt16 = 1883, socket: CocoaMQTTSocketProtocol = CocoaMQTTSocket()) 

Parameters

  • clientID: Client Identifier
  • host: The MQTT broker host domain or IP address. Default is "localhost"
  • port: The MQTT service port of host. Default is 1883

Properties

delegate

public weak var delegate: CocoaMQTT5Delegate?

host

public var host = "localhost"

port

public var port: UInt16 = 1883

clientID

public var clientID: String

username

public var username: String?

password

public var password: String?

cleanSession

Clean Session flag. Default is true

public var cleanSession = true

willMessage

Setup a Last Will Message to client before connecting to broker

public var willMessage: CocoaMQTT5Message?

backgroundOnSocket

Enable backgounding socket if running on iOS platform. Default is true

public var backgroundOnSocket: Bool 

delegateQueue

Delegate Executed queue. Default is DispatchQueue.main

public var delegateQueue = DispatchQueue.main

The delegate/closure callback function will be committed asynchronously to it

connState

public var connState = CocoaMQTTConnState.disconnected 

deliverTimeout

Re-deliver the un-acked messages

public var deliverTimeout: Double 

messageQueueSize

Message queue size. default 1000

public var messageQueueSize: UInt 

The new publishing messages of Qos1/Qos2 will be drop, if the queue is full

inflightWindowSize

In-flight window size. default 10

public var inflightWindowSize: UInt 

keepAlive

Keep alive time interval

public var keepAlive: UInt16 = 60

autoReconnect

Enable auto-reconnect mechanism

public var autoReconnect = false

autoReconnectTimeInterval

Reconnect time interval

public var autoReconnectTimeInterval: UInt16 = 1 

maxAutoReconnectTimeInterval

Maximum auto reconnect time interval

public var maxAutoReconnectTimeInterval: UInt16 = 128 

The timer starts from autoReconnectTimeInterval second and grows exponentially until this value After that, it uses this value for subsequent requests.

connectProperties

3.1.2.11 CONNECT Properties

public var connectProperties: MqttConnectProperties?

authProperties

3.15.2.2 AUTH Properties

public var authProperties: MqttAuthProperties?

logLevel

Console log level

public var logLevel: CocoaMQTTLoggerLevel 

enableSSL

Enable SSL connection

public var enableSSL: Bool 

sslSettings

public var sslSettings: [String: NSObject]? 

allowUntrustCACertificate

Allow self-signed ca certificate.

public var allowUntrustCACertificate: Bool 

Default is false

subscriptions

The subscribed topics in current communication

public var subscriptions: [String: CocoaMQTTQoS] = [:]

didConnectAck

public var didConnectAck: (CocoaMQTT5, CocoaMQTTCONNACKReasonCode, MqttDecodeConnAck) -> Void 

didPublishMessage

public var didPublishMessage: (CocoaMQTT5, CocoaMQTT5Message, UInt16) -> Void 

didPublishAck

public var didPublishAck: (CocoaMQTT5, UInt16, MqttDecodePubAck) -> Void 

didPublishRec

public var didPublishRec: (CocoaMQTT5, UInt16, MqttDecodePubRec) -> Void 

didReceiveMessage

public var didReceiveMessage: (CocoaMQTT5, CocoaMQTT5Message, UInt16, MqttDecodePublish) -> Void 

didSubscribeTopics

public var didSubscribeTopics: (CocoaMQTT5, NSDictionary, [String], MqttDecodeSubAck) -> Void 

didUnsubscribeTopics

public var didUnsubscribeTopics: (CocoaMQTT5, [String], MqttDecodeUnsubAck) -> Void 

didPing

public var didPing: (CocoaMQTT5) -> Void 

didReceivePong

public var didReceivePong: (CocoaMQTT5) -> Void 

didDisconnect

public var didDisconnect: (CocoaMQTT5, Error?) -> Void 

didDisconnectReasonCode

public var didDisconnectReasonCode: (CocoaMQTT5, CocoaMQTTDISCONNECTReasonCode) -> Void 

didAuthReasonCode

public var didAuthReasonCode: (CocoaMQTT5, CocoaMQTTAUTHReasonCode) -> Void 

didReceiveTrust

public var didReceiveTrust: (CocoaMQTT5, SecTrust, @escaping (Bool) -> Swift.Void) -> Void 

didCompletePublish

public var didCompletePublish: (CocoaMQTT5, UInt16, MqttDecodePubComp) -> Void 

didChangeState

public var didChangeState: (CocoaMQTT5, CocoaMQTTConnState) -> Void 

Methods

connect()

Connect to MQTT broker

public func connect() -> Bool 

Returns

  • Bool:​ It indicates whether successfully calling socket connect function. Not yet established correct MQTT session

connect(timeout:)

Connect to MQTT broker

public func connect(timeout: TimeInterval) -> Bool 

Parameters

  • timeout: Connect timeout

Returns

  • Bool:​ It indicates whether successfully calling socket connect function. Not yet established correct MQTT session

disconnect()

Send a DISCONNECT packet to the broker then close the connection

public func disconnect() 

disconnect(reasonCode:userProperties:)

public func disconnect(reasonCode : CocoaMQTTDISCONNECTReasonCode,userProperties : [String: String] ) 

ping()

Send a PING request to broker

public func ping() 

publish(_:withString:qos:DUP:retained:properties:)

Publish a message to broker

@discardableResult
    public func publish(_ topic: String, withString string: String, qos: CocoaMQTTQoS = .qos1, DUP: Bool = false
                        , retained: Bool = false, properties: MqttPublishProperties) -> Int 

Parameters

  • topic: Topic Name. It can not contain '#', '+' wildcards
  • string: Payload string
  • qos: Qos. Default is Qos1
  • retained: Retained flag. Mark this message is a retained message. default is false

Returns

  • 0 will be returned, if the message's qos is qos0
  • 1-65535 will be returned, if the messages's qos is qos1/qos2
  • -1 will be returned, if the messages queue is full

publish(_:DUP:retained:properties:)

Publish a message to broker

@discardableResult
    public func publish(_ message: CocoaMQTT5Message, DUP: Bool = false, retained: Bool = false, properties: MqttPublishProperties) -> Int 

Parameters

  • message: Message

subscribe(_:qos:)

Subscribe a <Topic Name>/<Topic Filter>

public func subscribe(_ topic: String, qos: CocoaMQTTQoS = .qos1) 

Parameters

  • topic: Topic Name or Topic Filter
  • qos: Qos. Default is qos1

subscribe(_:)

Subscribe a lists of topics

public func subscribe(_ topics: [MqttSubscription]) 

Parameters

  • topics: A list of tuples presented by (<Topic Names>/<Topic Filters>, Qos)

unsubscribe(_:)

Unsubscribe a Topic

public func unsubscribe(_ topic: String) 

Parameters

  • topic: A Topic Name or Topic Filter

unsubscribe(_:)

Unsubscribe a list of topics

public func unsubscribe(_ topics: [MqttSubscription]) 

Parameters

  • topics: A list of <Topic Names>/<Topic Filters>

auth(reasonCode:authProperties:)

Authentication exchange

public func auth(reasonCode : CocoaMQTTAUTHReasonCode,authProperties : MqttAuthProperties) 

socketConnected(_:)

public func socketConnected(_ socket: CocoaMQTTSocketProtocol) 

socket(_:didReceive:completionHandler:)

public func socket(_ socket: CocoaMQTTSocketProtocol,
                       didReceive trust: SecTrust,
                       completionHandler: @escaping (Bool) -> Swift.Void) 

socketDidSecure(_:)

public func socketDidSecure(_ sock: GCDAsyncSocket) 

socket(_:didWriteDataWithTag:)

public func socket(_ socket: CocoaMQTTSocketProtocol, didWriteDataWithTag tag: Int) 

socket(_:didRead:withTag:)

public func socket(_ socket: CocoaMQTTSocketProtocol, didRead data: Data, withTag tag: Int) 

socketDidDisconnect(_:withError:)

public func socketDidDisconnect(_ socket: CocoaMQTTSocketProtocol, withError err: Error?) 
Types
Protocols
Global Variables
Clone this wiki locally