{"id":1592,"url":"https://github.com/Kitura/SwiftKafka","last_synced_at":"2025-08-02T04:31:51.972Z","repository":{"id":54254955,"uuid":"193670438","full_name":"Kitura/SwiftKafka","owner":"Kitura","description":"Swift SDK for Apache Kafka","archived":false,"fork":false,"pushed_at":"2021-04-08T07:45:22.000Z","size":227,"stargazers_count":61,"open_issues_count":7,"forks_count":19,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-07-01T09:43:54.969Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Kitura.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-25T08:49:09.000Z","updated_at":"2025-06-04T23:23:07.000Z","dependencies_parsed_at":"2022-08-13T10:10:28.517Z","dependency_job_id":null,"html_url":"https://github.com/Kitura/SwiftKafka","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/Kitura/SwiftKafka","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kitura%2FSwiftKafka","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kitura%2FSwiftKafka/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kitura%2FSwiftKafka/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kitura%2FSwiftKafka/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Kitura","download_url":"https://codeload.github.com/Kitura/SwiftKafka/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kitura%2FSwiftKafka/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268334613,"owners_count":24233793,"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","status":"online","status_checked_at":"2025-08-02T02:00:12.353Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-01-05T20:15:50.681Z","updated_at":"2025-08-02T04:31:51.588Z","avatar_url":"https://github.com/Kitura.png","language":"Swift","funding_links":[],"categories":["Messaging"],"sub_categories":["Video"],"readme":"\u003cp align=\"center\"\u003e\n\u003ca href=\"http://kitura.io/\"\u003e\n\u003cimg src=\"https://raw.githubusercontent.com/IBM-Swift/Kitura/master/Sources/Kitura/resources/kitura-bird.svg?sanitize=true\" height=\"100\" alt=\"Kitura\"\u003e\n\u003c/a\u003e\n\u003c/p\u003e\n\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://ibm-swift.github.io/SwiftKafka/index.html\"\u003e\n\u003cimg src=\"https://img.shields.io/badge/apidoc-SwiftKafka-1FBCE4.svg?style=flat\" alt=\"APIDoc\"\u003e\n\u003c/a\u003e\n\u003ca href=\"https://travis-ci.org/IBM-Swift/SwiftKafka\"\u003e\n\u003cimg src=\"https://travis-ci.org/IBM-Swift/SwiftKafka.svg?branch=master\" alt=\"Build Status - Master\"\u003e\n\u003c/a\u003e\n\u003cimg src=\"https://img.shields.io/badge/os-macOS-green.svg?style=flat\" alt=\"macOS\"\u003e\n\u003cimg src=\"https://img.shields.io/badge/os-linux-green.svg?style=flat\" alt=\"Linux\"\u003e\n\u003cimg src=\"https://img.shields.io/badge/license-Apache2-blue.svg?style=flat\" alt=\"Apache 2\"\u003e\n\u003ca href=\"http://swift-at-ibm-slack.mybluemix.net/\"\u003e\n\u003cimg src=\"http://swift-at-ibm-slack.mybluemix.net/badge.svg\" alt=\"Slack Status\"\u003e\n\u003c/a\u003e\n\u003c/p\u003e\n\n# SwiftKafka\n\nA swift implementation of [Kafka](https://kafka.apache.org/) for producing and consuming from event streams.\n\nThis works by wrapping the [librdkafka](https://github.com/edenhill/librdkafka) C library.\n\n\n## Swift version\n\nThe latest version of SwiftKafka requires **Swift 5.0** or later. You can download this version of the Swift binaries by following this [link](https://swift.org/download/).\n\n## Usage\n\n### Swift Package Manager\n\n#### Add dependencies\nAdd the `SwiftKafka` package to the dependencies within your application’s `Package.swift` file. Substitute `\"x.x.x\"` with the latest `SwiftKafka` [release](https://github.com/IBM-Swift/SwiftKafka/releases).\n```swift\n.package(url: \"https://github.com/IBM-Swift/SwiftKafka.git\", from: \"x.x.x\")\n```\nAdd `SwiftKafka` to your target's dependencies:\n```swift\n.target(name: \"example\", dependencies: [\"SwiftKafka\"]),\n```\n#### Import package\n```swift\nimport SwiftKafka\n```\n\n## Getting Started\n\nTo use SwiftKafka you will need to install the `librdkafka` package:\n\n### macOS\n```\nbrew install librdkafka\n```\n\n### Linux\nInstall librdkafka from the Confluent APT repositories - [see instructions here](https://docs.confluent.io/current/installation/installing_cp/deb-ubuntu.html#get-the-software) (following steps 1 and 2 to add the Confluent package signing key and apt repository), and then install librdkafka:\n```\nsudo apt install librdkafka\n```\n\n#### Running a Kafka instance locally\nTo experiment locally, you can set up your own Kafka server to produce/consume from.\n\nOn macOS you can follow this guide on [Kafka Installation using Homebrew](https://medium.com/@Ankitthakur/apache-kafka-installation-on-mac-using-homebrew-a367cdefd273) to run a local server.\n\nOn Linux, you can follow this guide for [a manual install on Ubuntu](https://docs.confluent.io/current/installation/installing_cp/deb-ubuntu.html).\n\n### KafkaConfig\n\nThe `KafkaConfig` class contains your configuration settings for a `KafkaConsumer`/`KafkaProducer`.  \n\nThe class is initialized with default values which can then be changed using the helper functions.\nFor example, to enable all logging you would set the debug variable:\n```swift\nlet config = KafkaConfig()\nconfig.debug = [.all]\n```\n\nAlternatively, you can access the configuration dictionary directly on the `KafkaConfig` object:\n\n```swift\nlet config = KafkaConfig()\nconfig[\"debug\"] = \"all\"\n```\nThe list of configuration keys and descriptions can be found in the librdkafka [CONFIGURATION.md](https://github.com/edenhill/librdkafka/blob/master/CONFIGURATION.md).\n\nWhen you pass this class to a producer/consumer, a copy is made so further changes to the instance will not affect existing configurations.\n\n### KafkaProducer:\n\nThe `KafkaProducer` class produces messages to a Kafka server.\n\nYou can initialize a `KafkaProducer` using a `KafkaConfig` instance or with the default configuration.\n\nThe producer sends a `KafkaProducerRecord` with the following fields:\n\n- topic: The topic where the record will be sent. If this topic doesn't exist the producer will try to create it.\n- value: The message body that will be sent with the record.\n- partition: The topic partition the record will be sent to. If this is not set the partition will be automatically assigned.\n- key: If the partition is not set, records with the same key will be sent to the same partition. Since order is guaranteed within a partition, these records will be read in order they were produced.\n\nThe `send()` function is asynchronous. The result is returned in a callback which contains a `KafkaConsumerRecord` on success or a `KafkaError` on failure.\n\nThe following example produces a message with the value \"Hello World\" to a \"test\" topic of a Kafka server running on localhost.\n\n```swift\ndo {\n    let producer = try KafkaProducer()\n    guard producer.connect(brokers: \"localhost:9092\") == 1 else {\n        throw KafkaError(rawValue: 8)\n    }\n    producer.send(producerRecord: KafkaProducerRecord(topic: \"test\", value: \"Hello world\", key: \"Key\")) { result in\n        switch result {\n        case .success(let message):\n            print(\"Message at offset \\(message.offset) successfully sent\")\n        case .failure(let error):\n            print(\"Error producing: \\(error)\")\n        }\n    }\n} catch {\n    print(\"Error creating producer: \\(error)\")\n}\n```\n\n### KafkaConsumer:\n\nThe `KafkaConsumer` class consumes messages from a Kafka server.\n\nYou can initialize a `KafkaConsumer` using a `KafkaConfig` instance or with the default configuration.\n\nYou can then subscribe to topics using `subscribe()`.\nThis will distribute the topic partitions evenly between consumers with the same group id.\nIf you do not set a group id, a random UUID will be used.\n\nAlternatively to can use `assign()` to manually set the partition and offset for the consumer.\n\nBoth `subscribe()` and `assign()` are asynchronous and will return immediately, however they may take up to sessionTimeoutMs (Default 10 seconds) * 2 before the consumer completely connects.\n\nTo consume messages from Kafka you call `poll(timeout:)`. This will poll Kafka, blocking for `timeout` seconds. When it completes, it returns an array of `KafkaConsumerRecord` with the following fields:\n\n- value: The message value if it can be UTF8 decoded to a String.\n- valueData: The message value as raw data.\n- key: The message key if it can be utf8 decoded to a String.\n- keyData: The message key as raw data.\n- offset: The message offset.\n- topic: The topic that the message was consumed from.\n- partition: The partition that the message was consumed from.\n\nWhen you have finished consuming, you can call `close()` to close the connection and unassigns the consumer.\nThe unassigned partitions will then be rebalanced between other consumers in the group.\nIf  `close()` is not called, the consumer will be closed when the class is deallocated.\n\nThe following example consumes and print all unread messages from the \"test\" topic of the Kafka server.\n\n```swift\ndo {\n    let config = KafkaConfig()\n    config.groupId = \"Kitura\"\n    config.autoOffsetReset = .beginning\n    let consumer = try KafkaConsumer(config: config)\n    guard consumer.connect(brokers: \"localhost:9092\") == 1 else {\n        throw KafkaError(rawValue: 8)\n    }\n    try consumer.subscribe(topics: [\"test\"])\n    while(true) {\n        let records = try consumer.poll()\n        print(records)\n    }\n} catch {\n    print(\"Error creating consumer: \\(error)\")\n}\n```\n\n## API Documentation\nFor more information visit our [API reference](https://ibm-swift.github.io/Swift-Kafka/index.html).\n\n## Community\n\nWe love to talk server-side Swift, and Kitura. Join our [Slack](http://swift-at-ibm-slack.mybluemix.net/) to meet the team!\n\n## License\nThis library is licensed under Apache 2.0. Full license text is available in [LICENSE](https://github.com/IBM-Swift/Swift-Kafka/blob/master/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKitura%2FSwiftKafka","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FKitura%2FSwiftKafka","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKitura%2FSwiftKafka/lists"}