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

CocoaMQTT

MQTT Client

public class CocoaMQTT: NSObject, CocoaMQTTClient 

Inheritance

CocoaMQTTClient, 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: CocoaMQTTDelegate?

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: CocoaMQTTMessage?

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.

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: (CocoaMQTT, CocoaMQTTConnAck) -> Void 

didPublishMessage

public var didPublishMessage: (CocoaMQTT, CocoaMQTTMessage, UInt16) -> Void 

didPublishAck

public var didPublishAck: (CocoaMQTT, UInt16) -> Void 

didReceiveMessage

public var didReceiveMessage: (CocoaMQTT, CocoaMQTTMessage, UInt16) -> Void 

didSubscribeTopics

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

didUnsubscribeTopics

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

didPing

public var didPing: (CocoaMQTT) -> Void 

didReceivePong

public var didReceivePong: (CocoaMQTT) -> Void 

didDisconnect

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

didReceiveTrust

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

didCompletePublish

public var didCompletePublish: (CocoaMQTT, UInt16) -> Void 

didChangeState

public var didChangeState: (CocoaMQTT, 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() 

ping()

Send a PING request to broker

public func ping() 

publish(_:withString:qos:retained:)

Publish a message to broker

@discardableResult
    public func publish(_ topic: String, withString string: String, qos: CocoaMQTTQoS = .qos1, retained: Bool = false) -> 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(_:)

Publish a message to broker

@discardableResult
    public func publish(_ message: CocoaMQTTMessage) -> 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: [(String, CocoaMQTTQoS)]) 

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: [String]) 

Parameters

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

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