Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/orlandos-nl/SSEKit
Support for Server-Sent Events on AsyncSequences
https://github.com/orlandos-nl/SSEKit
Last synced: 2 months ago
JSON representation
Support for Server-Sent Events on AsyncSequences
- Host: GitHub
- URL: https://github.com/orlandos-nl/SSEKit
- Owner: orlandos-nl
- Created: 2024-06-18T21:40:17.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-06-30T16:27:23.000Z (5 months ago)
- Last Synced: 2024-07-15T01:40:11.197Z (4 months ago)
- Language: Swift
- Size: 11.7 KB
- Stars: 18
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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)
}
```