A maintained swift client for interacting with a nats server based on NIO2.
Join the #swift channel on nats.io Slack. We'll do our best to help quickly. You can also just drop by and say hello. We're looking forward to developing the community.
Add this packages as a dependency in your projects Package.swift
file and add the Name to your target like shown in this example:
// swift-tools-version:5.4
import PackageDescription
let package = Package(
name: "YourApp",
products: [
.executable(name: "YourApp", targets: ["YourApp"]),
],
dependencies: [
.package(name: "SwiftyNats", url: "https://github.com/aus-der-technik/swifty-nats.git", from: "2.2.0")
],
targets: [
.target(
name: "YourApp",
dependencies: ["SwiftyNats"]
),
]
)
Open the project inspector in XCode and select your project. It is importent to select the project and not a target!
Klick on the third tab Package Dependencies
and add the git url https://github.com/aus-der-technik/swifty-nats.git
by selecting the litte +
-sign at the end of the package list.
import SwiftyNats
// register a new client
let client = NatsClient("http://nats.server:4222")
// listen to an event
client.on(.connect) { _ in
print("Client connected")
}
// try to connect to the server
try? client.connect()
// subscribe to a channel with a inline message handler.
client.subscribe("foo.bar") { message in
print("payload: \(message.payload)")
print("size: \(message.byteCount)")
print("reply subject: \(message.replySubject.subject)")
}
// publish an event onto the message strem into a subject
client.publish("this event happened", to: "foo.bar")
The default loglevel is .error
. You can reset it to see more verbose messages. Possible
Values are .debug
, .info
, .error
or .critical
let client = NatsClient("http://nats.server:4222")
client.config.loglevel = .info
Reconnection is not part of this package, because if a server diconnects your application have to be sure that subscribtions are made up again correctly.
With SwiftyNats this is a very easy step:
// register a new client
let client = NatsClient(url)
// listen to the .disconnected event to try a reconnect
client.on(.disconnected) { [self] _ in
sleep(5);
try? client.reconnect()
doSubscribe()
}
// subscribe to the channels
doSubscribe()
// private function to subscribe to channels
private func doSubscribe(){
client.subscribe("foo.bar") { message in
print("payload: \(message.payload)")
}
}
The public class NatsEvent
contains all events you can subscribt to.
event | description |
---|---|
connected | The client is conected to the server. |
disconnected | The client disconnects and was connectd before. |
response | The client gets an response from the server (internal). |
error | The server sends an error that can't be handled. |
dropped | The clients droped a message. Mostly because of queue length to short. |
reconnecting | The client reconencts to the server, (Because of a called reconnect()). |
informed | The server sends his information data successfully to the client. |
Since 2.0.2 it is possible to get the informations from the conencted server
let client = NatsClient("http://nats.server:4222")
print("\(client.serverInformation.serverName) has Version: \(client.serverInformation.version))");
Contribution is always welcome. Just send me a pull request.
- The client configuration is now publicly available
- The handling of the connection status has been rewritten
- Rewrite of the connection setup
- The connection termination was rewritten
- All classes have been cleaned up (refactoring)
- A new license was added (BSD-0)
- The reconnection code was removed
- Subscription queue is an optional property of NatsSubject
- rewrite the ChannelHandler: remove a bug that could lead into dropped messages.
- uses NIO2
- works with Vapor, now!
- supports iOS anf tvOS
- Get information from the connected Server (Version, name, id, ...)
- Test with GitHub Actions
- Update Dockerfile to build and test on Swift 5.4 (Linux)
- Test with a real NATS-Server on macos
- Cleanup unused definitions
- Update Informations, do project care
- Tested with NATS 2.3.4
- Introduced logging
- Updated depricated functions
See: Contribution ;)
- Propper function description is needed