Upgrade the Alamofire from version 4 to 5
I am being told 'Value of type 'DataRequest' has no member 'stream' , is there any replacement for this api?
I am using request().stream {(data) in } method to receive chunk data from my server. the server pushing realtime log via stream chunked json data through get method. (For example: the Docker log api)
Alamofire version:5.0.0-beta.6
Xcode version:10.2.1
Swift version:5
Platform(s) running Alamofire: Mac
macOS version running Xcode:10.14.5
There is no equivalent to stream { } in Alamofire 5. It was removed from DataRequest due to the inherent differences between stream and response handling leading to a subpar user experience. However, we do think it's useful and it shouldn't be too difficult to support, but we'd like to know more about how people use the feature and what would be wanted out of it. So can you describe your use case? Are there any Alamofire features you would like to see for streaming HTTP data?
I am writing an gui program for a proxy client call Clash. It provide logs and realtime traffic through stream api. When using curl, it looks like below

The traffic data updated per second using stream chunked data.
These are codes I am using with Alamofire 4:
request(ConfigManager.apiUrl + "/traffic")
.stream {(data) in
if let jsonData = try? JSONSerialization.jsonObject(with: data) as? [String:Int] {
callback(jsonData["up"] ?? 0, jsonData["down"] ?? 0)
}
}.response {[weak self] res in
guard let err = res.error else {return}
// Error Handle
}
I get streaming chucked data via stream{} and handle error in response{}. In stream callback, I need to manually parse json data. I hope Alamofire could also provide streamJSON or even streamDecodable method for easier use.
@jshier Any ideas ?
This would involve creating a new Request subclass, likely something like DataStreamRequest, and adding the appropriate API, ensuring we don't store any of the data in memory. We don't have the bandwidth to do add this at the moment, but we'd be very interested in a PR building this functionality.
I am also using the stream function for stream chunked data, can you please add that function back in the new version.
I am also using request().stream {(data) to fetch the data from the sever as we have large amount of data flow at the time of initial sync. please keep the support for stream. We are receiving the chunk of data continuously as response stream, we are parsing it till the end of stream.
This is now tracked by #3072. Work is also proceeding on the feature/data-streaming branch, check it out and add your feedback to the tracked issue.
@yichengchen Thanks for the example. I've been able to confirm that the new DataStreamRequest can successfully decode from the Clash traffic log stream. Something like this should work:
struct ClashLog: Decodable {
let up: Int
let down: Int
}
AF.streamRequest("http://127.0.0.1:9090/traffic").responseStreamDecodable(of: ClashLog.self) { output in
}
It should be released as part of the next minor version of Alamofire.
DataStreamRequest shipped as part of Alamofire 5.1.
Most helpful comment
@yichengchen Thanks for the example. I've been able to confirm that the new
DataStreamRequestcan successfully decode from the Clash traffic log stream. Something like this should work:It should be released as part of the next minor version of Alamofire.