# Swift Async Algorithms Proposal: Broadcast (Previously Shared)

**URL:** <https://forums.swift.org/t/swift-async-algorithms-proposal-broadcast-previously-shared/61210>\
**Category:** Swift Async Algorithms\
**Created:** [November 3, 2022, 12:45pm UTC](https://forums.swift.org/t/swift-async-algorithms-proposal-broadcast-previously-shared/61210 "2022-11-03T12:45:36Z")\
**Posts on this page:** 14\
**Page:** 3

<div class="post-metadata">

**Author:** ![twittemb](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/twittemb/32/28643_2.png) [@twittemb](https://forums.swift.org/u/twittemb)\
**Post date:** [January 1, 2023, 2:45pm UTC](https://forums.swift.org/t/swift-async-algorithms-proposal-broadcast-previously-shared/61210/41 "2023-01-01T14:45:06Z")

</div>

Hi everyone and happy new year.

To push the thinking a but further on that matter, I've implemented a version of `broadcast` based on a buffering strategy.

> <https://github.com/apple/swift-async-algorithms/pull/242>
>
> This PR is an attempt to implement a broadcast operator following the temporary …conclusion of this discussion: https://forums.swift.org/t/swift-async-algorithms-proposal-broadcast-previously-shared/61210/36
> 
> This kind of operator can become tricky to implement once you consider the back pressure management and the cancellation policies.
> 
> To ease the understanding I've used 2 levels of state machines (known as orthogonal regions in my book):
> \- a state machine per iteration/client to handle buffering and cancellation
> \- a state machine for orchestrating the value distribution mechanism and restricting the access to the upstream sequence
> 
> I've used the pattern: one task for the upstream sequence iteration. This task is started on the first call to \`next\` by the first iteration and suspended between each element.
> 
> Pending questions (@FranzBusch, @phausler, @etcwilde) :
> 
> \- For now each client state machine is created on the first call to "next". It makes it a bit hard to test because we need some synchronisation between iterations to ensure we don't loose values and for now my tests \*\*are flaky because of that\*\*. Should we do that in the call to "makeAsyncIterator" instead ? (I remember a decision where nothing important should be done on \`makeAsyncIterator\` ... \`broadcast\` might be an exception to that rule)
> 
> \- For now I use an unbounded buffer per iteration, which can lead to uncontrolled memory footprint. Should we have an "on overflow" policy to drop elements? We mentioned an optimisation of the storage in the forum based on a single buffer and keeping track of indexes per client -\> it means some more computations to handle the sanitisation of these indexes, meaning poorer performances also.
> 
> \- For now there is no "on cancellation" policy. The task for the upstream sequence remains alive when there is no more clients, waiting for a new one. Should we allow to cancel the upstream task in that case? It will be easy to do as I keep track of the task in the state machine. 
> 
> \- For now there is a single locking mechanism for all the state machines (the main one and the ones per client). Should we consider a locking mechanism per client to increase the perfs (by parallelising computations) ?
> 
> I've not yet implemented a "replay" mechanism, but it will be pretty easy to do thanks to the separation of concerns between state machines. All we have to do is keep track of the history and initialise each new client state machine with this history in its internal buffer.

Let me know what you think.

---

<div class="post-metadata">

**Author:** ![FranzBusch](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/franzbusch/32/19447_2.png) [@FranzBusch](https://forums.swift.org/u/FranzBusch)\
**Post date:** [January 10, 2023, 11:11am UTC](https://forums.swift.org/t/swift-async-algorithms-proposal-broadcast-previously-shared/61210/42 "2023-01-10T11:11:23Z")

</div>

Coming back to this after the holidays now. I agree that there are multiple different behaviours; however, I believe we should start with the "consume at the speed of the fastest consumer including a buffer". This is to me the safest option. If we realise that further down the line we want the other one we can always create that and provide an option for it.

The option you laid out without back pressure is basically just a `buffer` attached to the upstream and a "fastest consumer" strategy. So I wouldn't spend any time on that.

W.r.t. to "Subjects" I would like to see how the ecosystem evolves once we get `broadcast`, because as you said, you can compose an `AsyncStream` and `broadcast` to get the desired behaviour. From my experience "Subjects" have sometimes lead to confusion so maybe a more explicit composition is good.

---

<div class="post-metadata">

**Author:** ![tcldr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tcldr/32/24405_2.png) [@tcldr](https://forums.swift.org/u/tcldr)\
**Post date:** [January 10, 2023, 12:27pm UTC](https://forums.swift.org/t/swift-async-algorithms-proposal-broadcast-previously-shared/61210/43 "2023-01-10T12:27:42Z")

</div>

> [@FranzBusch](#):
>
> W.r.t. to "Subjects" I would like to see how the ecosystem evolves once we get `broadcast`, because as you said, you can compose an `AsyncStream` and `broadcast` to get the desired behaviour. From my experience "Subjects" have sometimes lead to confusion so maybe a more explicit composition is good.

My point is that perhaps this is something we should consider holistically.

Subject's may appear complex on the surface, but perhaps that's because they solve multiple problems in a single-shot:

1. Performing the Source Sequence role
2. Multicasting unicast source sequences/pipelines (including conversion of custom unicast `AsyncSequence` types to be multi-cast)
3. [Side-stepping the multi-pass `AsyncSequence` issue](https://forums.swift.org/t/swift-async-algorithms-design-guidelines/61629/13)

My concern is that if we continue down the path of solving only no. 2, we will slowly move away from solving no. 1 and no. 3.

My opinion is that there seems to be an attachment to the API of `AsyncStream`, and particularly breaking apart the 'producer' and 'sequence' parts, [similar to the discussion about enhancing `AsyncStream` with a new initialiser](https://forums.swift.org/t/pitch-convenience-async-throwing-stream-makestream-methods/61030), in the name of simplicity. However, the alternative seems to be many more types with limited composability. So far, I'm not sure this is a progression of the benefits provided by a `Subject` like type, and to me it feels even _more_ complex.

For example, now programmers must remember that they always need to chain a `broadcast` sequence to their asynchronous sequence pipelines if they wish to vend to an unknown number of consumers. Worse, if the sequence is type erased (`any AsyncSequence`), they'll need to do this _defensively_ to avoid undefined behaviour.

Of course, a `Subject` equivalent would need to be carefully designed to leverage the unique properties of `AsyncSequence` (i.e. `autoconnect` doesn't make much sense in the context of a back pressure supporting pipeline), but on the whole, my guess is the surface _complexity_ of Subject will result in a _net reduction_ in complexity when viewed holistically and compared to the current direction.

Hope this is helpful – happy to discuss any of the points raised in further detail if useful.

---

<div class="post-metadata">

**Author:** ![filiplazov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/filiplazov/32/7886_2.png) [@filiplazov](https://forums.swift.org/u/filiplazov)\
**Post date:** [June 21, 2023, 5:17pm UTC](https://forums.swift.org/t/swift-async-algorithms-proposal-broadcast-previously-shared/61210/44 "2023-06-21T17:17:15Z")

</div>

Is there any traction on this?  
After wwdc I was hoping to see async algorithms bring more feature parity to what Combine offers but it doesn't seem like it is happening any time soon and your post was the only thing going in that direction. Are there any initiatives for this to be taken into consideration or to produce solutions which tackle the problems you are trying to solve here?

Thank you for your effort btw :)

---

<div class="post-metadata">

**Author:** ![tcldr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tcldr/32/24405_2.png) [@tcldr](https://forums.swift.org/u/tcldr)\
**Post date:** [June 21, 2023, 6:32pm UTC](https://forums.swift.org/t/swift-async-algorithms-proposal-broadcast-previously-shared/61210/45 "2023-06-21T18:32:44Z")

</div>

I agree, progress on asynchronous sequences does appear to have slowed down somewhat.

I don't have any particular insights into why, but my guess would be that we're dependent on decisions in other areas of the language before `AsyncSequence` can really get into a good shape. For example, a decision on [typed throws](https://forums.swift.org/t/precise-error-typing-in-swift/52045) will determine the shape of a protocol for production side of an `AsyncSequence` – an `AsyncSource` or whatever it ends up being called. It would also allow for primary associated types to be locked-in for `AsyncSequence`, or failing that whether a stop-gap `AnyAsyncSquence` type should have 1 generic parameter, or include a 2nd parameter for a typed error.

More widely, I'm not sure asynchronous sequences can ever be a _complete_ replacement for Combine as the focus for asynchronous sequences is decidedly on the _asynchronous_ part. Probably quite rightly, It's very hard to wrestle Swift concurrency into acting synchronously as has been [discussed quite extensively on the Observable proposal](https://forums.swift.org/t/se-0395-observability/64342/62).

Maybe, `Observable` will eventually fill much of this particular gap, but that remains to be seen.

---

<div class="post-metadata">

**Author:** ![Philippe\_Hausler](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/philippe_hausler/32/22569_2.png) [@Philippe\_Hausler](https://forums.swift.org/u/Philippe_Hausler)\
**Post date:** [June 21, 2023, 6:38pm UTC](https://forums.swift.org/t/swift-async-algorithms-proposal-broadcast-previously-shared/61210/46 "2023-06-21T18:38:10Z")

</div>

Observation has taken a lot of my time to get that over the line - broadcast is definitely one of the first post 1.0 things to tackle.

Things that need to be done:

- There are a few lurking sendability/concurrency warnings in strict mode that need to be addressed.
- The initializers for types should be audited; e.g. the extension accessors for things should be the primary access point and we should have a uniform construction story (iirc there are a few w/ public initializers on the types)
- The inlining needs to be audited for ABI boundaries

---

<div class="post-metadata">

**Author:** ![BastianKusserow](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bastiankusserow/32/30307_2.png) [@BastianKusserow](https://forums.swift.org/u/BastianKusserow)\
**Post date:** [June 22, 2023, 8:36am UTC](https://forums.swift.org/t/swift-async-algorithms-proposal-broadcast-previously-shared/61210/47 "2023-06-22T08:36:27Z")

</div>

Is there any chance to get a rough estimated timeline on when a 1.0 can be expected and when things like broadcast will have a decision which path to take?

---

<div class="post-metadata">

**Author:** ![Philippe\_Hausler](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/philippe_hausler/32/22569_2.png) [@Philippe\_Hausler](https://forums.swift.org/u/Philippe_Hausler)\
**Post date:** [June 22, 2023, 10:08pm UTC](https://forums.swift.org/t/swift-async-algorithms-proposal-broadcast-previously-shared/61210/48 "2023-06-22T22:08:44Z")

</div>

The faster we can get some of those issues I pointed out addressed the quicker we can start to work on those other parts.

I have a couple of PRs up for some of them -

> <https://github.com/apple/swift-async-algorithms/pull/271>
>
> This is a comprehensive audit of the initializers and inline ABI boundaries of a…ll API types. 
> 
> The primary entry point for all transformations is the extension to AsyncSequence unless the type is a public interface as an AsyncIteratorProtocol.

> <https://github.com/apple/swift-async-algorithms/pull/272>
>
> This brings the main API surface up to Sendable clean in complete mode. 
> 
> The …primary point of change is Async(Throwing)Channel. Which previously permitted a potential Sendability soundness hole. It now requires that AsyncChannel's Element type MUST be Sendable. The reasoning is that this type's primary use case is to send elements from one isolation to another. This means that the element must be Sendable to achieve this.

But truth be told there are bits that folks could help out getting those done faster. Reviews definitely are useful, also confirmation of testing of those changes. Additional audits of inlines, sendability and warnings will go a long way to shoring up the remaining bits.

---

<div class="post-metadata">

**Author:** ![Sajjon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sajjon/32/12044_2.png) [@Sajjon](https://forums.swift.org/u/Sajjon)\
**Post date:** [May 15, 2024, 6:10pm UTC](https://forums.swift.org/t/swift-async-algorithms-proposal-broadcast-previously-shared/61210/49 "2024-05-15T18:10:37Z")

</div>

With Async Algorithm 1.0 released in December last year, what is the status on getting Broadcast / Shared / Multicast implemented in Async Algorithm package? Anything being worked on? 🙏

---

<div class="post-metadata">

**Author:** ![hekuli](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hekuli/32/35084_2.png) [@hekuli](https://forums.swift.org/u/hekuli)\
**Post date:** [November 8, 2024, 3:39pm UTC](https://forums.swift.org/t/swift-async-algorithms-proposal-broadcast-previously-shared/61210/50 "2024-11-08T15:39:25Z")

</div>

I also have this need for my applications. Pending this feature request landing... what would be the best alternative APIs to use today for a multicast Pub/Sub like component?

---

<div class="post-metadata">

**Author:** ![nonameplum](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nonameplum/32/6450_2.png) [@nonameplum](https://forums.swift.org/u/nonameplum)\
**Post date:** [January 8, 2025, 11:09am UTC](https://forums.swift.org/t/swift-async-algorithms-proposal-broadcast-previously-shared/61210/51 "2025-01-08T11:09:43Z")

</div>

I'm bringing this back as probably many of us are waiting for the final implementation.

Based on already proposed solutions in chronological order:

- [[WIP] Broadcast algorithm by phausler · Pull Request #214 · apple/swift-async-algorithms · GitHub](https://github.com/apple/swift-async-algorithms/pull/214) - @Philippe_Hausler
- [Broadcast algorithm (Previously Shared) by tcldr · Pull Request #227 · apple/swift-async-algorithms · GitHub](https://github.com/apple/swift-async-algorithms/pull/227) - @tcldr
- [[Broadcast] implement the algorithm with a buffering strategy (v1.1) by twittemb · Pull Request #242 · apple/swift-async-algorithms · GitHub](https://github.com/apple/swift-async-algorithms/pull/242) - @twittemb

& potential vision from @FranzBusch [Broadcast algorithm (Previously Shared) by tcldr · Pull Request #227 · apple/swift-async-algorithms · GitHub](https://github.com/apple/swift-async-algorithms/pull/227#issuecomment-1660458507)

Can we consider the last one as the closest desired solution to the problem until the final solution is released?

---

<div class="post-metadata">

**Author:** ![tcldr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tcldr/32/24405_2.png) [@tcldr](https://forums.swift.org/u/tcldr)\
**Post date:** [January 8, 2025, 12:38pm UTC](https://forums.swift.org/t/swift-async-algorithms-proposal-broadcast-previously-shared/61210/52 "2025-01-08T12:38:15Z")

</div>

I’ll jump in here to say I haven’t been actively developing my own ideas on this further, but I still firmly believe that the ideal solution to this looks quite a bit different to those above.

The crux of it is that the range of problems that this algorithm should attempt to solve is broader than a single algorithm.

Firstly, as you begin designing a multicast algorithm such as the one above, it becomes clear that there are many variations on a 'broadcast' algorithm, and therefore it's important that we design a solution to accommodate future enhancements.

The following comment by @ktoso summarises some of these:

> [@\[question\]: (how to) re-introduce concurrent iteration runtime error for \`AsyncStream\`](https://forums.swift.org/t/question-how-to-re-introduce-concurrent-iteration-runtime-error-for-asyncstream/71831/8):
>
> How does that look like in practice? Explicit operators which define the various semantics, like:
> 
> - cancellation:
> - cancel when one cancels
> - cancel when all cancel
> - etc
> 
> - buffering / replay / live:
> - buffer and replay to all subscribers
> - broadcast from current element to all subscribers
> - round robin elements to active subscribers" behaviors
> 
> - when elements are emitted
> - when completion is signaled

Secondly, and perhaps more contentiously, I'm still unclear as to why this family of broadcast algorithms is expected to be chained (like `map` or `filter`) rather than being source algorithms (like `AsyncStream` or Subject's in the Rx/Combine world.)

The argument that I've heard to date is that programmers will find the 'chaining' algorithms simpler to understand as they struggle with the concept of Subjects in Rx/Combine.

However, I think that this is to miss some of the benefits of this model.

One of the big things that I think new programmers struggle with is that they are unable to iterate over source sequences (such as AsyncStream) multiple times – they're 'single shot'. Concerningly, the behaviour when you do is undefined.

If we were to instead adopt the source algorithm approach to multicasting, we would benefit from not only a range of multicasting options, but also a range of source algorithms that can safely be iterated multiple times. (And also designed to accommodate other issues such as back pressure.)

Of course, this approach would take some design effort, likely the introduction of a new `AsyncSource` type (roughly equivalent to a Subject) in the stdlib and then a new chained operator to multicast via said AsyncSource types. But I think it would go a long way to solving many of the shortcomings we have with AsyncSequence today.

---

<div class="post-metadata">

**Author:** ![jonericcook](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@jonericcook](https://forums.swift.org/u/jonericcook)\
**Post date:** [May 15, 2025, 9:05pm UTC](https://forums.swift.org/t/swift-async-algorithms-proposal-broadcast-previously-shared/61210/53 "2025-05-15T21:05:36Z")

</div>

@tcldr thanks for all your work on this and also for all your posts on this forum

I have been on the hunt for an Event Bus type of solution. What you guys are talking about looks crazy cool and complicated... it reminds me of [Broadway — Broadway v1.2.1](https://hexdocs.pm/broadway/Broadway.html)

I have found a handful of Event Bus examples using Combine but only one that uses AsyncStream.

Medium article: [EventBus: Failed Swift Structured Concurrency | by Kirill Chuyanov | Medium](https://medium.com/@archanger/eventbus-failed-swift-structured-concurrency-12a2f47a581e)  
GitHub Gist: [EventBus: Structured Concurrency · GitHub](https://gist.github.com/archanger/1f023c9beaa010152b326c64f1ecbce9)

If you can't read the article he concludes by saying:

> **Conclusion**  
> Apple did a great job of introducing compile-time checks within structured concurrency. It helps a lot to write as safe code as possible. However, be cautious when using **AsyncStreams** and always check for object deallocations. Most probably, by design, **AsyncStreams** are not supposed to be infinite, but sadly, most of the time, streams are infinite.  
> You can find the full listing [here](https://gist.github.com/archanger/1f023c9beaa010152b326c64f1ecbce9)  
> **Post Conclusion**  
> It is possible to break the cycle by moving from **AsyncStreams** to _closures_.

My question is why can't this work? Why can't I create a new AsyncStream per subscriber?

---

<div class="post-metadata">

**Author:** ![Philippe\_Hausler](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/philippe_hausler/32/22569_2.png) [@Philippe\_Hausler](https://forums.swift.org/u/Philippe_Hausler)\
**Post date:** [May 16, 2025, 5:25pm UTC](https://forums.swift.org/t/swift-async-algorithms-proposal-broadcast-previously-shared/61210/54 "2025-05-16T17:25:14Z")

</div>

> [@jonericcook](#):
>
> My question is why can't this work? Why can't I create a new AsyncStream per subscriber?

You could... and folks do as an approximation of "Broadcast" type things. The issue is that is kind of expensive - it keeps a duplication of the buffer for each "side" which could be shared and indices are pointed into the buffer, that full-buffer scenario is not always what folks want, sometimes they only want the "latest values", and lastly the ideal form would only have replication in the iterator and not at the sequence level.

I think the major issues are that we need to have a proposal for set of behaviors and an implementation that achieves the outlined behaviors.

[Previous page](https://forums.swift.org/t/swift-async-algorithms-proposal-broadcast-previously-shared/61210.md?page=2)
