SSEKit

1.1.0

Support for Server-Sent Events on AsyncSequences
hummingbird-project/sse-kit

What's New

1.1.0

2024-09-14T09:05:13Z

What's Changed

New Contributors

Full Changelog: 1.0.1...1.1.0

SSEKit

Support for Server-Sent Events in Swift through AsyncSequence.

Usage with Hummingbird 2

Hummingbird 2's ResponseBody is already conforming to AsyncSequence<ByteBuffer>, so you can use the APIs easily. There's Example Code in this repository for Hummingbird 2.

import SSEKit
router.get("events") { req, context in
  // Get any `AsyncSequence<ServerSentEvent>`. We'll use AsyncStream to make it easy
  let (events, continuation) = AsyncStream<ServerSentEvent>.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)
}

Description

  • Swift Tools 5.10.0
View More Packages from this Author

Dependencies

Last updated: Sun Apr 12 2026 02:53:51 GMT-0900 (Hawaii-Aleutian Daylight Time)