Skip to content

Latest commit

 

History

History
66 lines (53 loc) · 3.02 KB

File metadata and controls

66 lines (53 loc) · 3.02 KB

MQTT NIO

sswg:sandbox|94x20 Swift 6.2.3 Codecov

A Swift NIO based MQTT v3.1.1 and v5.0 client.

MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol that was developed by IBM and first released in 1999. It uses the pub/sub pattern and translates messages between devices, servers, and applications. It is commonly used in Internet of things (IoT) technologies.

MQTTNIO is a Swift NIO based implementation of a MQTT client. It supports

  • MQTT versions 3.1.1 and 5.0.
  • Unencrypted and encrypted (via TLS) connections
  • WebSocket connections
  • Posix sockets
  • Apple's Network framework via NIOTransportServices (required for iOS).
  • Unix domain sockets

Overview

Create a connection to the MQTT broker with MQTTConnection.withConnection and use it inside the closure. When the closure returns the connection will be closed. CONNECT and DISCONNECT packets are sent automatically, respectively before and after the closure is executed.

try await MQTTConnection.withConnection(
    address: .hostname("mqtt.eclipse.org"),
    identifier: "My Client",
    logger: Logger(...)
) { connection in
    // You are now connected to the MQTT broker
    // The connection will be active only inside this closure
}

Subscribe to a topic with MQTTConnection.subscribe, providing a closure that receives an AsyncSequence of incoming PUBLISH messages sent from the broker to that topic. When the closure finishes executing, the corresponding UNSUBSCRIBE message is automatically sent to the broker, and the subscription is cleaned up.

let subscribeInfo = MQTTSubscribeInfo(topicFilter: "my-topics", qos: .atLeastOnce)
try await connection.subscribe(to: [subscribeInfo]) { subscription in
    for try await message in subscription {
        var buffer = message.payload
        let string = buffer.readString(length: buffer.readableBytes)
        // No need to filter messages, as only messages for "my-topics" are received here
        print(string)
    }
}

Publish to a topic with MQTTConnection.publish.

try await connection.publish(
    to: "my-topics",
    payload: ByteBuffer(string: "This is the Test payload"),
    qos: .atLeastOnce
)

Documentation

User guides and reference documentation for MQTT NIO can be found on the Swift Package Index. There is also a sample demonstrating the use of MQTTNIO in an iOS app found here.