---
title: jollyjinx/mqtt-nio
framework: Swift Package Catalog
role: article
path: packages/jollyjinx/mqtt-nio
---

# jollyjinx/mqtt-nio

[<img src="https://github.com/swift-server-community/mqtt-nio/workflows/CI/badge.svg" />](https://github.com/swift-server-community/mqtt-nio/workflows/CI/badge.svg)

## 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.

```swift try await MQTTConnection.withConnection(     address: .hostname("mqtt.eclipse.org"),     identifier: "My Client" ) { 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.

```swift 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`.

```swift 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](https://swiftpackageindex.com/swift-server-community/mqtt-nio/documentation/mqttnio). There is also a sample demonstrating the use of MQTTNIO in an iOS app found [here](https://github.com/adam-fowler/EmCuTeeTee).

## Package Metadata

Repository: jollyjinx/mqtt-nio

Default branch: main

README: README.md
