A swift client for interacting with NATS servers


A swift client for interacting with a nats server.

Build Status License Swift4


Only uses SPM to install. Add the dependencies like below.

// swift-tools-version:4.0

import PackageDescription

let package = Package(
    name: "YourApp",
    products: [
        .library(name: "YourApp", targets: ["YourApp"])
    dependencies: [
        .package(url: "https://github.com/raykrow/swifty-nats.git", from: "1.0.0")
    targets: [
            name: "YourApp",
            dependencies: ["SwiftyClient"]

Basic Usage

import SwiftyNats

let client = NatsClient("http://nats.server:4222")

client.on(.connect) { _ in
    print("Client connected")

try? client.connect()

client.subscribe("foo.bar") { message in
    print("payload: \(message.payload)")
    print("size: \(message.byteCount)")
    print("reply subject: \(message.replySubject.subject)")

client.publish("this event happened", to: "foo.bar")


  • Save connect_urls property from INFO response and use it later when attempting to reconnect
  • In publish method make the payload optional. Its ok to send 0 count and no payload PUB swift.test 0\r\n\r\n (just pass "" as payload)
  • Support subscribing to a queue group SUB swift.test G1 11\r\n
  • Configurable max queue size, default 100
  • Configurable message encoding, default UTF-8
  • When trying to reconnect to the server, wait 5 seconds between retries, configurable
  • Add a reconnecting event


  • Swift Tools 4.0.0
View More Packages from this Author


Last updated: Thu Mar 16 2023 11:40:17 GMT-0500 (GMT-05:00)