AsyncSequence from URLSessionWebSocketTask?

Hey there,

I’m currently planing with WebSockets and was a bit surprised that I don’t found any method to get an Async(Throwing)Sequence with the incoming Messges.
Anyways I think that is a common expectation, so are the any reasons that it’s not there yet?
Like heavy battery usage or so?

And are there a strong sequence if I want to contribute this kind of method to swift, like create a proposal, let it be reviewed and create a pull request on github?

Best regards

AsyncThrowingStream(unfolding: task.receive)?

1 Like

It‘s a solid solution, but unintuitive in my opinion. I want a consolidated solution for this issue. Elsewhere I would use an extension.

I would also take care about it, but I have currently only my experience about it and don’t want to invest time into a pull requests or something if there are no people with the same experience.

And then is the question where to start? Is there a guide or anything?

We generally recommend that you use the WebSocket support in Network framework [1], not URLSession, and this year Network framework got a whole bunch of Swift concurrency enhancements. See WWDC 2025 Session 250 Use structured concurrency with Network framework.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

[1] See TN3151 Choosing the right networking API.

4 Likes

I think you meant to link to this TechNote Quinn:

D’oh! Technote numbers are great, but TN3151 and TN3161 are way too close on the keyboard.

I fixed my original post, just to avoid further confusion.

Thanks for the headsup.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

Out of curiosity. It’s easy to suggest people use NWConnection (NetworkConnection has an unrealistic deployment restriction) but does Apple actually provide a readily available WebSocket connection? I just had a quick look at the APIs, and it seemed like one would have to implement the actual standard oneself?