{"id":21834229,"url":"https://github.com/Recouse/EventSource","last_synced_at":"2025-07-20T16:31:23.497Z","repository":{"id":152529964,"uuid":"622575078","full_name":"Recouse/EventSource","owner":"Recouse","description":"Server-sent events in Swift","archived":false,"fork":false,"pushed_at":"2024-04-13T12:59:36.000Z","size":64,"stargazers_count":20,"open_issues_count":1,"forks_count":5,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-04-14T02:43:47.373Z","etag":null,"topics":["client","event-source","eventsource","server-sent-events","sse"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Recouse.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"Recouse"}},"created_at":"2023-04-02T14:17:57.000Z","updated_at":"2024-04-28T16:27:32.483Z","dependencies_parsed_at":"2024-01-30T13:42:24.449Z","dependency_job_id":"9805b517-b8de-402a-b8d1-793fa1160588","html_url":"https://github.com/Recouse/EventSource","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Recouse%2FEventSource","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Recouse%2FEventSource/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Recouse%2FEventSource/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Recouse%2FEventSource/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Recouse","download_url":"https://codeload.github.com/Recouse/EventSource/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226814497,"owners_count":17686302,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["client","event-source","eventsource","server-sent-events","sse"],"created_at":"2024-11-27T20:02:18.797Z","updated_at":"2025-07-20T16:31:23.491Z","avatar_url":"https://github.com/Recouse.png","language":"Swift","funding_links":["https://github.com/sponsors/Recouse"],"categories":["Swift"],"sub_categories":[],"readme":"# EventSource\n\n[![CI](https://github.com/Recouse/EventSource/actions/workflows/ci.yml/badge.svg)](https://github.com/Recouse/EventSource/actions/workflows/ci.yml)\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FRecouse%2FEventSource%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/Recouse/EventSource)\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FRecouse%2FEventSource%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/Recouse/EventSource)\n\nEventSource is a Swift package that provides a simple implementation of a client for [Server-Sent Events](https://html.spec.whatwg.org/multipage/server-sent-events.html) (SSE). It allows you to easily receive real-time updates from a server over a persistent HTTP connection, using a simple and efficient interface.\n\nIt also leverages Swift concurrency features to provide a more expressive and intuitive way to handle asynchronous operations.\n\n\u003e [!Note]\n\u003e Please note that this package was originally developed to be used in conjunction with another package, and as such, it may not cover all specification details. Please be aware of this limitation when evaluating whether EventSource is suitable for your specific use case.\n\n## Features\n\n- [x] Simple Swift API for SSE\n- [x] Supports data-only mode\n- [x] Data race safety with Swift 6\n\n## Installation\n\nThe module name of the package is `EventSource`. Choose one of the instructions below to install and add the following import statement to your source code.\n\n```swift\nimport EventSource\n```\n\n#### [Xcode Package Dependency](https://developer.apple.com/documentation/xcode/adding_package_dependencies_to_your_app)\n\nFrom Xcode menu: `File` \u003e `Swift Packages` \u003e `Add Package Dependency`\n\n```text\nhttps://github.com/Recouse/EventSource\n```\n\n#### [Swift Package Manager](https://www.swift.org/documentation/package-manager/)\n\nIn your `Package.swift` file, first add the following to the package `dependencies`:\n\n```swift\n.package(url: \"https://github.com/Recouse/EventSource.git\"),\n```\n\nAnd then, include \"EventSource\" as a dependency for your target:\n\n```swift\n.target(name: \"\u003ctarget\u003e\", dependencies: [\n    .product(name: \"EventSource\", package: \"EventSource\"),\n]),\n```\n\n## Usage\n\nUsing EventSource is easy. Simply create a new data task from an instance of EventSource with the URLRequest of the SSE endpoint you want to connect to, and await for events:\n```swift\nimport EventSource\n\nTask {\n    let eventSource = EventSource()\n    let dataTask = eventSource.dataTask(for: urlRequest)\n    \n    for await event in dataTask.events() {\n        switch event {\n        case .open:\n            print(\"Connection was opened.\")\n        case .error(let error):\n            print(\"Received an error:\", error.localizedDescription)\n        case .event(let event):\n            print(\"Received an event\", event.data ?? \"\")\n        case .closed:\n            print(\"Connection was closed.\")\n        }\n    }\n}\n```\n\nUse `dataTask.cancel()` to explicitly close the connection. However, in that case `.closed` event won't be emitted.\n\n### Data-only mode\n\nEventSource can be used in data-only mode, making it suitable for popular APIs like [OpenAI](https://platform.openai.com/docs/overview). Below is an example using OpenAI's [completions](https://platform.openai.com/docs/guides/text-generation) API:\n```swift\nTask {\n    var urlRequest = URLRequest(url: URL(string: \"https://api.openai.com/v1/chat/completions\")!)\n    urlRequest.allHTTPHeaderFields = [\n        \"Content-Type\": \"application/json\",\n        \"Authorization\": \"Bearer \\(accessToken)\"\n    ]\n    urlRequest.httpMethod = \"POST\"\n    urlRequest.httpBody = \"\"\"\n    {\n        \"model\": \"gpt-4o-mini\",\n        \"messages\": [\n            {\"role\": \"user\", \"content\": \"Why is the sky blue?\"}\n        ],\n        \"stream\": true\n    }\n    \"\"\".data(using: .utf8)!\n    \n    let eventSource = EventSource(mode: .dataOnly)\n    let dataTask = eventSource.dataTask(for: urlRequest)\n    \n    var response: String = \"\"\n    \n    for await event in dataTask.events() {\n        switch event {\n        case .event(let event):\n            if let data = eventDevent.data?.data(using: .utf8) {\n                let chunk = try? JSONDecoder().decode(ChatCompletionChunk.self, from: data)\n                let string = chunk?.choices.first?.delta.content ?? \"\"\n                response += string\n            }\n        default:\n            break\n        }\n    }\n    \n    print(response)\n}\n```\n\n## Compatibility\n\n* macOS 10.15+\n* iOS 13.0+\n* tvOS 13.0+\n* watchOS 6.0+\n* visionOS 1.0+\n\n## Dependencies\n\nNo dependencies.\n\n## Contributing\n\nContributions to are always welcomed! For more details see [CONTRIBUTING.md](CONTRIBUTING.md).\n\n## Credits\n\n* Mutex backport from [swift-sharing](https://github.com/pointfreeco/swift-sharing)\n\n## License\n\nEventSource is released under the MIT License. See [LICENSE](LICENSE) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRecouse%2FEventSource","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FRecouse%2FEventSource","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRecouse%2FEventSource/lists"}