A Swift NIO based STOMP v1.0, v1.1 and v1.2 client.
Heavily inspired by Adam Fowler's work on MQTT NIO and valkey-swift.
Simple (or Streaming) Text Oriented Message Protocol (STOMP) is a simple interoperable protocol designed for asynchronous message passing between clients via mediating servers. It defines a text based wire-format for messages passed between these clients and servers. STOMP has been in active use for several years and is supported by many message brokers and client libraries.
STOMPNIO is a Swift NIO based implementation of a STOMP client. It supports:
- STOMP versions 1.0, 1.1, and 1.2
- Unencrypted and encrypted (via TLS) connections
- WebSocket connections
- POSIX sockets
- Apple's Network framework via NIOTransportServices (required for iOS)
- Unix domain sockets
Create a client with server connection details:
import STOMPNIO
let stompClient = STOMPClient(.hostname("localhost"), logger: logger)The STOMPClient uses a connection pool, which requires a background process to manage it.
You can run the background process using async let. When you leave the scope of the function your async let variable is declared the client will be shutdown.
let stompClient = STOMPClient(.hostname("localhost"), logger: logger)
async let _ = stompClient.run()
// Use STOMP client
try await stompClient.send("Hello, World!", to: "/queue/a")
// Client continues running in backgroundAlternatively you could also use a TaskGroup.
try await withThrowingTaskGroup { group in
group.addTask {
await stompClient.run()
}
// All operations happen in the closure body
try await stompClient.send("Hello, World!", to: "/queue/a")
// When done, cancel the run() task
group.cancelAll()
}
// Client is shut down when task group exitsOr you can use STOMPClient with swift-service-lifecycle for long-running services.
let services: [Service] = [myApp, stompClient]
let serviceGroup = ServiceGroup(
services: services,
gracefulShutdownSignals: [.sigint, .sigterm],
logger: logger
)
try await serviceGroup.run()Once you have a STOMP client setup and running you can send STOMP frames directly from the STOMPClient.
try await stompClient.send("Hello, STOMP over NIO!", to: "/queue/a")Or you can subscribe to destinations using STOMPClient.subscribe. A single connection is used for all subscriptions.
try await stompClient.subscribe(to: "/queue/a") { subscription in
for try await frame in subscription {
print(String(buffer: frame.body))
}
}User guides and reference documentation for STOMP NIO can be found on the Swift Package Index.