Swiftpack.co - teufelaudio/CombineLongPolling as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by teufelaudio.
teufelaudio/CombineLongPolling v0.1.6
Combine HTTP Long-Polling wrappers
⭐️ 14
🕓 2 years ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/teufelaudio/CombineLongPolling.git", from: "v0.1.6")

Combine HTTP Long-Polling wrappers

This library implements basic HTTP Long-Polling functionality using Combine.

Long-polling Publisher

Represents a Combine publisher that starts a data task that, once finished, will be restarted over and over again. The data task can terminate due to error or timeout, but this won't be propagated as error to the LongPollingPublisher, because that would interrupt the main publisher. Instead, this publisher will emit events of type Result<(Data, URLResponse), Error> for every time a DataTaskPublisher emits a successful output or an error. Then, the LongPollingPublisher restarts the DataTaskPublisher. LongPollingPublisher will Never fail.

Usage:

let config = URLSessionConfiguration.default
// For long-polling you probably want big numbers in here
// but for testing how it works, start with short times
config.timeoutIntervalForRequest = 10
config.timeoutIntervalForResource = 10

// Create and keep your data task (`AnyCancellable`)
let task = URLSession(configuration: config)
    .longPollingPublisher(for: URL(string: "http://localhost:3000/subscribe")!)
    .map { result in
        // You may want to convert URLResponse into HTTPURLResponse first
        // and validate your statusCode first, and also decode the JSON
        // from the data variable, but for simplicity here we only check
        // how the main data task ended, with success or failure. It will
        // be restarted after that, so LongPollingPublisher will keep
        // emitting values forever, and never fails.
        switch result {
        case let .success(data, _):
            return String(data: data, encoding: .utf8)!
        case let .failure(error):
            return String("catching error: \(error)")
        }
    }
    .sink { string in Swift.print(string) }

Installation:

.package(url: "https://github.com/teufelaudio/CombineLongPolling.git", .branch("master"))

GitHub

link
Stars: 14
Last commit: 2 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

Ensure that calling cancel() deallocates the subscription
2 years ago

What's Changed

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics