Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/orlandos-nl/SSEKit

Support for Server-Sent Events on AsyncSequences
https://github.com/orlandos-nl/SSEKit

Last synced: 23 days ago
JSON representation

Support for Server-Sent Events on AsyncSequences

Awesome Lists containing this project

README

        

## SSEKit

Support for Server-Sent Events in Swift through AsyncSequence.

### Usage with Hummingbird 2

Hummingbird 2's ResponseBody is already conforming to `AsyncSequence`, so you can use the APIs easily.
There's [Example Code](https://github.com/orlandos-nl/SSEKit/tree/main/Example) in this repository for Hummingbird 2.

```swift
import SSEKit
router.get("events") { req, context in
// Get any `AsyncSequence`. We'll use AsyncStream to make it easy
let (events, continuation) = AsyncStream.makeStream()

// TODO: Emit events into `continuation` using your logic
// This logic emits the current time as ISO8601
let now = ISO8601DateFormatter().string(from: Date())
continuation.yield(ServerSentEvent(data: SSEValue(string: now)))

// Closing the stream is important, as omitting this will let the body hang indefinitely
continuation.finish()

let body = ResponseBody(asyncSequence: events.mapToByteBuffer(allocator: context.allocator))
return Response(status: .ok, headers: [.contentType: "text/event-stream"], body: body)
}
```