<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Combine",
  "identifier" : "/documentation/Combine/Publisher/multicast(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Combine"
    ],
    "preciseIdentifier" : "s:7Combine9PublisherPAAE9multicastyAA10PublishersO9MulticastCy_xqd__Gqd__ycAA7SubjectRd__7FailureQyd__AKRtz6OutputQyd__ANRtzlF"
  },
  "title" : "multicast(_:)"
}
-->

# multicast(_:)

Applies a closure to create a subject that delivers elements to subscribers.

```
func multicast<S>(_ createSubject: @escaping () -> S) -> Publishers.Multicast<Self, S> where S : Subject, Self.Failure == S.Failure, Self.Output == S.Output
```

## Parameters

`createSubject`

A closure to create a new [`Subject`](/documentation/Combine/Subject) each time a subscriber attaches to the multicast publisher.

## Discussion

Use a multicast publisher when you have multiple downstream subscribers, but you want upstream publishers to only process one [`receive(_:)`](/documentation/Combine/Subscriber/receive(_:)) call per event. This is useful when upstream publishers are doing expensive work you don’t want to duplicate, like performing network requests.

In contrast with [`multicast(subject:)`](/documentation/Combine/Publisher/multicast(subject:)), this method produces a publisher that creates a separate [`Subject`](/documentation/Combine/Subject) for each subscriber.

The following example uses a sequence publisher as a counter to publish three random numbers, generated by a [`map(_:)`](/documentation/Combine/Publisher/map(_:)-99evh) operator.
It uses a [`multicast(_:)`](/documentation/Combine/Publisher/multicast(_:)) operator whose closure creates a [`PassthroughSubject`](/documentation/Combine/PassthroughSubject) to share the same random number to each of two subscribers. Because the multicast publisher is a [`ConnectablePublisher`](/documentation/Combine/ConnectablePublisher), publishing only begins after a call to [`connect()`](/documentation/Combine/ConnectablePublisher/connect()).

```
let pub = ["First", "Second", "Third"].publisher
    .map( { return ($0, Int.random(in: 0...100)) } )
    .print("Random")
    .multicast { PassthroughSubject<(String, Int), Never>() }

cancellable1 = pub
   .sink { print ("Stream 1 received: \($0)")}
cancellable2 = pub
   .sink { print ("Stream 2 received: \($0)")}
pub.connect()

// Prints:
// Random: receive value: (("First", 9))
// Stream 2 received: ("First", 9)
// Stream 1 received: ("First", 9)
// Random: receive value: (("Second", 46))
// Stream 2 received: ("Second", 46)
// Stream 1 received: ("Second", 46)
// Random: receive value: (("Third", 26))
// Stream 2 received: ("Third", 26)
// Stream 1 received: ("Third", 26)
```

In this example, the output shows that the [`print(_:to:)`](/documentation/Combine/Publisher/print(_:to:)) operator receives each random value only one time, and then sends the value to both subscribers.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)