Top Description Constructors Methods
io.reactivex.rxjava3.flowables

public abstract Class ConnectableFlowable<T>

extends Flowable<T>
Class Inheritance
Known Direct Subclasses
io.reactivex.rxjava3.internal.operators.flowable.FlowablePublish, io.reactivex.rxjava3.internal.operators.flowable.FlowableReplay
Type Parameters
<T>
the type of items emitted by the ConnectableFlowable
Imports
java.util.Objects, java.util.concurrent.TimeUnit, org.reactivestreams.*, io.reactivex.rxjava3.annotations.*, io.reactivex.rxjava3.core.*, io.reactivex.rxjava3.disposables.Disposable, io.reactivex.rxjava3.functions.Consumer, io.reactivex.rxjava3.internal.functions.*, io.reactivex.rxjava3.internal.operators.flowable.*, io.reactivex.rxjava3.internal.util.ConnectConsumer, io.reactivex.rxjava3.plugins.RxJavaPlugins, io.reactivex.rxjava3.schedulers.Schedulers

A ConnectableFlowable resembles an ordinary Flowable, except that it does not begin emitting items when it is subscribed to, but only when its connect method is called. In this way you can wait for all intended Subscribers to Flowable#subscribe to the Flowable before the Flowable begins emitting items.

When the upstream terminates, the ConnectableFlowable remains in this terminated state and, depending on the actual underlying implementation, relays cached events to late Subscribers. In order to reuse and restart this ConnectableFlowable, the reset() method has to be called. When called, this ConnectableFlowable will appear as fresh, unconnected source to new Subscribers. Disposing the connection will reset the ConnectableFlowable to its fresh state and there is no need to call reset() in this case.

Note that although connect() and reset() are safe to call from multiple threads, it is recommended a dedicated thread or business logic manages the connection or resetting of a ConnectableFlowable so that there is no unwanted signal loss due to early connect() or reset() calls while Subscribers are still being subscribed to to this ConnectableFlowable to receive signals from the get go.

Since
2.0.0
See Also
RxJava Wiki: Connectable Observable Operators

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public @NonNull Flowable<T>

Returns:

a new Flowable instance that automatically connects to this ConnectableFlowable when the first Subscriber subscribes
autoConnect
()

Returns a Flowable that automatically connects (at most once) to this ConnectableFlowable when the first Subscriber subscribes.

public @NonNull Flowable<T>

Returns:

a new Flowable instance that automatically connects to this ConnectableFlowable when the specified number of Subscribers subscribe to it
autoConnect
(int
the number of subscribers to await before calling connect on the ConnectableFlowable. A non-positive value indicates an immediate connection.
numberOfSubscribers
)

Returns a Flowable that automatically connects (at most once) to this ConnectableFlowable when the specified number of Subscribers subscribe to it.

public @NonNull Flowable<T>

Returns:

a new Flowable instance that automatically connects to this ConnectableFlowable when the specified number of Subscribers subscribe to it and calls the specified callback with the Disposable associated with the established connection
autoConnect
(int
the number of subscribers to await before calling connect on the ConnectableFlowable. A non-positive value indicates an immediate connection.
numberOfSubscribers
,
@NonNull Consumer<? super Disposable>
the callback Consumer that will receive the Disposable representing the established connection
connection
)

Returns a Flowable that automatically connects (at most once) to this ConnectableFlowable when the specified number of Subscribers subscribe to it and calls the specified callback with the Disposable associated with the established connection.

public abstract void
connect(@NonNull Consumer<? super Disposable>
the action that receives the connection subscription before the subscription to source happens allowing the caller to synchronously disconnect a synchronous source
connection
)

Instructs the ConnectableFlowable to begin emitting the items from its underlying Flowable to its Subscribers.

public final @NonNull Disposable

Returns:

the subscription representing the connection
connect
()

Instructs the ConnectableFlowable to begin emitting the items from its underlying Flowable to its Subscribers.

public @NonNull Flowable<T>

Returns:

the new Flowable instance
refCount
()

Returns a Flowable that stays connected to this ConnectableFlowable as long as there is at least one subscription to this ConnectableFlowable.

public final @NonNull Flowable<T>

Returns:

the new Flowable instance
refCount
(int
the number of subscribers required to connect to the upstream
subscriberCount
)

Connects to the upstream ConnectableFlowable if the number of subscribed subscriber reaches the specified count and disconnect if all subscribers have unsubscribed.

public final @NonNull Flowable<T>

Returns:

the new Flowable instance
refCount
(long
the time to wait before disconnecting after all subscribers unsubscribed
timeout
,
@NonNull TimeUnit
the time unit of the timeout
unit
)

Connects to the upstream ConnectableFlowable if the number of subscribed subscriber reaches 1 and disconnect after the specified timeout if all subscribers have unsubscribed.

public final @NonNull Flowable<T>

Returns:

the new Flowable instance
refCount
(long
the time to wait before disconnecting after all subscribers unsubscribed
timeout
,
@NonNull TimeUnit
the time unit of the timeout
unit
,
@NonNull Scheduler
the target scheduler to wait on before disconnecting
scheduler
)

Connects to the upstream ConnectableFlowable if the number of subscribed subscriber reaches 1 and disconnect after the specified timeout if all subscribers have unsubscribed.

public final @NonNull Flowable<T>

Returns:

the new Flowable instance
refCount
(int
the number of subscribers required to connect to the upstream
subscriberCount
,
long
the time to wait before disconnecting after all subscribers unsubscribed
timeout
,
@NonNull TimeUnit
the time unit of the timeout
unit
)

Connects to the upstream ConnectableFlowable if the number of subscribed subscriber reaches the specified count and disconnect after the specified timeout if all subscribers have unsubscribed.

public final @NonNull Flowable<T>

Returns:

the new Flowable instance
refCount
(int
the number of subscribers required to connect to the upstream
subscriberCount
,
long
the time to wait before disconnecting after all subscribers unsubscribed
timeout
,
@NonNull TimeUnit
the time unit of the timeout
unit
,
@NonNull Scheduler
the target scheduler to wait on before disconnecting
scheduler
)

Connects to the upstream ConnectableFlowable if the number of subscribed subscriber reaches the specified count and disconnect after the specified timeout if all subscribers have unsubscribed.

public abstract void
reset()

Resets this ConnectableFlowable into its fresh state if it has terminated.

Inherited from io.reactivex.rxjava3.core.Flowable:
allambambArrayambWithanyblockingFirstblockingFirstblockingForEachblockingForEachblockingIterableblockingIterableblockingLastblockingLastblockingLatestblockingMostRecentblockingNextblockingSingleblockingSingleblockingStreamblockingStreamblockingSubscribeblockingSubscribeblockingSubscribeblockingSubscribeblockingSubscribeblockingSubscribeblockingSubscribeblockingSubscribebufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferSizecachecacheWithInitialCapacitycastcollectcollectcollectIntocombineLatestcombineLatestcombineLatestcombineLatestcombineLatestcombineLatestcombineLatestcombineLatestcombineLatestcombineLatestcombineLatestArraycombineLatestArraycombineLatestArrayDelayErrorcombineLatestArrayDelayErrorcombineLatestDelayErrorcombineLatestDelayErrorcomposeconcatconcatconcatconcatconcatconcatconcatArrayconcatArrayDelayErrorconcatArrayEagerconcatArrayEagerconcatArrayEagerDelayErrorconcatArrayEagerDelayErrorconcatDelayErrorconcatDelayErrorconcatDelayErrorconcatEagerconcatEagerconcatEagerconcatEagerconcatEagerDelayErrorconcatEagerDelayErrorconcatEagerDelayErrorconcatEagerDelayErrorconcatMapconcatMapconcatMapconcatMapCompletableconcatMapCompletableconcatMapCompletableDelayErrorconcatMapCompletableDelayErrorconcatMapCompletableDelayErrorconcatMapDelayErrorconcatMapDelayErrorconcatMapDelayErrorconcatMapEagerconcatMapEagerconcatMapEagerDelayErrorconcatMapEagerDelayErrorconcatMapIterableconcatMapIterableconcatMapMaybeconcatMapMaybeconcatMapMaybeDelayErrorconcatMapMaybeDelayErrorconcatMapMaybeDelayErrorconcatMapSingleconcatMapSingleconcatMapSingleDelayErrorconcatMapSingleDelayErrorconcatMapSingleDelayErrorconcatMapStreamconcatMapStreamconcatWithconcatWithconcatWithconcatWithcontainscountcreatedebouncedebouncedebouncedebouncedefaultIfEmptydeferdelaydelaydelaydelaydelaydelaydelaySubscriptiondelaySubscriptiondelaySubscriptiondematerializedistinctdistinctdistinctdistinctUntilChangeddistinctUntilChangeddistinctUntilChangeddoAfterNextdoAfterTerminatedoFinallydoOnCanceldoOnCompletedoOnEachdoOnEachdoOnErrordoOnLifecycledoOnNextdoOnRequestdoOnSubscribedoOnTerminateelementAtelementAtelementAtOrErroremptyerrorerrorfilterfirstfirstElementfirstOrErrorfirstOrErrorStagefirstStageflatMapflatMapflatMapflatMapflatMapflatMapflatMapflatMapflatMapflatMapflatMapflatMapflatMapCompletableflatMapCompletableflatMapIterableflatMapIterableflatMapIterableflatMapIterableflatMapMaybeflatMapMaybeflatMapSingleflatMapSingleflatMapStreamflatMapStreamforEachforEachWhileforEachWhileforEachWhilefromActionfromArrayfromCallablefromCompletablefromCompletionStagefromFuturefromFuturefromIterablefromMaybefromObservablefromOptionalfromPublisherfromRunnablefromSinglefromStreamfromSuppliergenerategenerategenerategenerategenerategroupBygroupBygroupBygroupBygroupBygroupBygroupJoinhideignoreElementsintervalintervalintervalintervalintervalRangeintervalRangeisEmptyjoinjustjustjustjustjustjustjustjustjustjustlastlastElementlastOrErrorlastOrErrorStagelastStageliftmapmapOptionalmaterializemergemergemergemergemergemergemergemergemergeArraymergeArraymergeArrayDelayErrormergeArrayDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeWithmergeWithmergeWithmergeWithneverobserveOnobserveOnobserveOnofTypeonBackpressureBufferonBackpressureBufferonBackpressureBufferonBackpressureBufferonBackpressureBufferonBackpressureBufferonBackpressureBufferonBackpressureBufferonBackpressureBufferonBackpressureBufferonBackpressureDroponBackpressureDroponBackpressureLatestonBackpressureLatestonBackpressureReduceonBackpressureReduceonErrorCompleteonErrorCompleteonErrorResumeNextonErrorResumeWithonErrorReturnonErrorReturnItemonTerminateDetachparallelparallelparallelpublishpublishpublishpublishrangerangeLongrebatchRequestsreducereducereduceWithrepeatrepeatrepeatUntilrepeatWhenreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayretryretryretryretryretryretryUntilretryWhensafeSubscribesamplesamplesamplesamplesamplesamplesamplescanscanscanWithsequenceEqualsequenceEqualsequenceEqualsequenceEqualserializesharesinglesingleElementsingleOrErrorsingleOrErrorStagesingleStageskipskipskipskipLastskipLastskipLastskipLastskipLastskipLastskipUntilskipWhilesortedsortedstartWithstartWithstartWithstartWithstartWithArraystartWithItemstartWithIterablesubscribesubscribesubscribesubscribesubscribesubscribesubscribesubscribeActualsubscribeOnsubscribeOnsubscribeWithswitchIfEmptyswitchMapswitchMapswitchMapCompletableswitchMapCompletableDelayErrorswitchMapDelayErrorswitchMapDelayErrorswitchMapMaybeswitchMapMaybeDelayErrorswitchMapSingleswitchMapSingleDelayErrorswitchOnNextswitchOnNextswitchOnNextDelayErrorswitchOnNextDelayErrortaketaketaketakeLasttakeLasttakeLasttakeLasttakeLasttakeLasttakeLasttakeLasttakeLasttakeUntiltakeUntiltakeWhiletesttesttestthrottleFirstthrottleFirstthrottleFirstthrottleLastthrottleLastthrottleLastthrottleLatestthrottleLatestthrottleLatestthrottleLatestthrottleLatestthrottleWithTimeoutthrottleWithTimeoutthrottleWithTimeouttimeIntervaltimeIntervaltimeIntervaltimeIntervaltimeouttimeouttimeouttimeouttimeouttimeouttimeouttimeouttimertimertimestamptimestamptimestamptimestamptotoFuturetoListtoListtoListtoMaptoMaptoMaptoMultimaptoMultimaptoMultimaptoMultimaptoObservabletoSortedListtoSortedListtoSortedListtoSortedListunsafeCreateunsubscribeOnusingusingwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwithLatestFromwithLatestFromwithLatestFromwithLatestFromwithLatestFromwithLatestFromzipzipzipzipzipzipzipzipzipzipzipzipzipArrayzipWithzipWithzipWithzipWith

Constructor Detail

ConnectableFlowableback to summary
public ConnectableFlowable()

Method Detail

autoConnectback to summary
public @NonNull Flowable<T> autoConnect()

Returns a Flowable that automatically connects (at most once) to this ConnectableFlowable when the first Subscriber subscribes.

The connection happens after the first subscription and happens at most once during the lifetime of the returned Flowable. If this ConnectableFlowable terminates, the connection is never renewed, no matter how Subscribers come and go. Use refCount() to renew a connection or dispose an active connection when all Subscribers have cancelled their Subscriptions.

This overload does not allow disconnecting the connection established via connect(Consumer). Use the autoConnect(int, Consumer) overload to gain access to the Disposable representing the only connection.

Backpressure:
The operator itself doesn't interfere with backpressure which is determined by the upstream ConnectableFlowable's behavior.
Scheduler:
autoConnect does not operate by default on a particular Scheduler.
Returns:@NonNull Flowable<T>

a new Flowable instance that automatically connects to this ConnectableFlowable when the first Subscriber subscribes

Annotations
@NonNull
@CheckReturnValue
@BackpressureSupport:PASS_THROUGH
@SchedulerSupport:none
See Also
refCount(), autoConnect(int, Consumer)
autoConnectback to summary
public @NonNull Flowable<T> autoConnect(int numberOfSubscribers)

Returns a Flowable that automatically connects (at most once) to this ConnectableFlowable when the specified number of Subscribers subscribe to it.

The connection happens after the given number of subscriptions and happens at most once during the lifetime of the returned Flowable. If this ConnectableFlowable terminates, the connection is never renewed, no matter how Subscribers come and go. Use refCount() to renew a connection or dispose an active connection when all Subscribers have cancelled their Subscriptions.

This overload does not allow disconnecting the connection established via connect(Consumer). Use the autoConnect(int, Consumer) overload to gain access to the Disposable representing the only connection.

Backpressure:
The operator itself doesn't interfere with backpressure which is determined by the upstream ConnectableFlowable's behavior.
Scheduler:
autoConnect does not operate by default on a particular Scheduler.
Parameters
numberOfSubscribers:int

the number of subscribers to await before calling connect on the ConnectableFlowable. A non-positive value indicates an immediate connection.

Returns:@NonNull Flowable<T>

a new Flowable instance that automatically connects to this ConnectableFlowable when the specified number of Subscribers subscribe to it

Annotations
@NonNull
@CheckReturnValue
@BackpressureSupport:PASS_THROUGH
@SchedulerSupport:none
autoConnectback to summary
public @NonNull Flowable<T> autoConnect(int numberOfSubscribers, @NonNull Consumer<? super Disposable> connection)

Returns a Flowable that automatically connects (at most once) to this ConnectableFlowable when the specified number of Subscribers subscribe to it and calls the specified callback with the Disposable associated with the established connection.

The connection happens after the given number of subscriptions and happens at most once during the lifetime of the returned Flowable. If this ConnectableFlowable terminates, the connection is never renewed, no matter how Subscribers come and go. Use refCount() to renew a connection or dispose an active connection when all Subscribers have cancelled their Subscriptions.

Backpressure:
The operator itself doesn't interfere with backpressure which is determined by the upstream ConnectableFlowable's behavior.
Scheduler:
autoConnect does not operate by default on a particular Scheduler.
Parameters
numberOfSubscribers:int

the number of subscribers to await before calling connect on the ConnectableFlowable. A non-positive value indicates an immediate connection.

connection:@NonNull Consumer<? super Disposable>

the callback Consumer that will receive the Disposable representing the established connection

Returns:@NonNull Flowable<T>

a new Flowable instance that automatically connects to this ConnectableFlowable when the specified number of Subscribers subscribe to it and calls the specified callback with the Disposable associated with the established connection

Annotations
@NonNull
@CheckReturnValue
@BackpressureSupport:PASS_THROUGH
@SchedulerSupport:none
Exceptions
NullPointerException:
if connection is null
connectback to summary
public abstract void connect(@NonNull Consumer<? super Disposable> connection)

Instructs the ConnectableFlowable to begin emitting the items from its underlying Flowable to its Subscribers.

Scheduler:
The behavior is determined by the implementor of this abstract class.
Parameters
connection:@NonNull Consumer<? super Disposable>

the action that receives the connection subscription before the subscription to source happens allowing the caller to synchronously disconnect a synchronous source

Annotations
@SchedulerSupport:none
Exceptions
NullPointerException:
if connection is null
See Also
ReactiveX documentation: Connect
connectback to summary
public final @NonNull Disposable connect()

Instructs the ConnectableFlowable to begin emitting the items from its underlying Flowable to its Subscribers.

To disconnect from a synchronous source, use the connect(io.reactivex.rxjava3.functions.Consumer) method.

Scheduler:
The behavior is determined by the implementor of this abstract class.
Returns:@NonNull Disposable

the subscription representing the connection

Annotations
@NonNull
@SchedulerSupport:none
See Also
ReactiveX documentation: Connect
refCountback to summary
public @NonNull Flowable<T> refCount()

Returns a Flowable that stays connected to this ConnectableFlowable as long as there is at least one subscription to this ConnectableFlowable.

Backpressure:
The operator itself doesn't interfere with backpressure which is determined by the upstream ConnectableFlowable's backpressure behavior.
Scheduler:
This refCount overload does not operate on any particular Scheduler.
Returns:@NonNull Flowable<T>

the new Flowable instance

Annotations
@NonNull
@CheckReturnValue
@SchedulerSupport:none
@BackpressureSupport:PASS_THROUGH
See Also
ReactiveX documentation: RefCount, refCount(int), refCount(long, TimeUnit), refCount(int, long, TimeUnit)
refCountback to summary
public final @NonNull Flowable<T> refCount(int subscriberCount)

Connects to the upstream ConnectableFlowable if the number of subscribed subscriber reaches the specified count and disconnect if all subscribers have unsubscribed.

Backpressure:
The operator itself doesn't interfere with backpressure which is determined by the upstream ConnectableFlowable's backpressure behavior.
Scheduler:
This refCount overload does not operate on any particular Scheduler.

History: 2.1.14 - experimental

Parameters
subscriberCount:int

the number of subscribers required to connect to the upstream

Returns:@NonNull Flowable<T>

the new Flowable instance

Annotations
@CheckReturnValue
@SchedulerSupport:none
@BackpressureSupport:PASS_THROUGH
@NonNull
Exceptions
IllegalArgumentException:
if subscriberCount is non-positive
Since
2.2
refCountback to summary
public final @NonNull Flowable<T> refCount(long timeout, @NonNull TimeUnit unit)

Connects to the upstream ConnectableFlowable if the number of subscribed subscriber reaches 1 and disconnect after the specified timeout if all subscribers have unsubscribed.

Backpressure:
The operator itself doesn't interfere with backpressure which is determined by the upstream ConnectableFlowable's backpressure behavior.
Scheduler:
This refCount overload operates on the computation Scheduler.

History: 2.1.14 - experimental

Parameters
timeout:long

the time to wait before disconnecting after all subscribers unsubscribed

unit:@NonNull TimeUnit

the time unit of the timeout

Returns:@NonNull Flowable<T>

the new Flowable instance

Annotations
@CheckReturnValue
@SchedulerSupport:io.reactivex:computation
@BackpressureSupport:PASS_THROUGH
@NonNull
Exceptions
NullPointerException:
if unit is null
Since
2.2
See Also
refCount(long, TimeUnit, Scheduler)
refCountback to summary
public final @NonNull Flowable<T> refCount(long timeout, @NonNull TimeUnit unit, @NonNull Scheduler scheduler)

Connects to the upstream ConnectableFlowable if the number of subscribed subscriber reaches 1 and disconnect after the specified timeout if all subscribers have unsubscribed.

Backpressure:
The operator itself doesn't interfere with backpressure which is determined by the upstream ConnectableFlowable's backpressure behavior.
Scheduler:
This refCount overload operates on the specified Scheduler.

History: 2.1.14 - experimental

Parameters
timeout:long

the time to wait before disconnecting after all subscribers unsubscribed

unit:@NonNull TimeUnit

the time unit of the timeout

scheduler:@NonNull Scheduler

the target scheduler to wait on before disconnecting

Returns:@NonNull Flowable<T>

the new Flowable instance

Annotations
@CheckReturnValue
@SchedulerSupport:custom
@BackpressureSupport:PASS_THROUGH
@NonNull
Exceptions
NullPointerException:
if unit or scheduler is null
Since
2.2
refCountback to summary
public final @NonNull Flowable<T> refCount(int subscriberCount, long timeout, @NonNull TimeUnit unit)

Connects to the upstream ConnectableFlowable if the number of subscribed subscriber reaches the specified count and disconnect after the specified timeout if all subscribers have unsubscribed.

Backpressure:
The operator itself doesn't interfere with backpressure which is determined by the upstream ConnectableFlowable's backpressure behavior.
Scheduler:
This refCount overload operates on the computation Scheduler.

History: 2.1.14 - experimental

Parameters
subscriberCount:int

the number of subscribers required to connect to the upstream

timeout:long

the time to wait before disconnecting after all subscribers unsubscribed

unit:@NonNull TimeUnit

the time unit of the timeout

Returns:@NonNull Flowable<T>

the new Flowable instance

Annotations
@CheckReturnValue
@SchedulerSupport:io.reactivex:computation
@BackpressureSupport:PASS_THROUGH
@NonNull
Exceptions
NullPointerException:
if unit is null
IllegalArgumentException:
if subscriberCount is non-positive
Since
2.2
See Also
refCount(int, long, TimeUnit, Scheduler)
refCountback to summary
public final @NonNull Flowable<T> refCount(int subscriberCount, long timeout, @NonNull TimeUnit unit, @NonNull Scheduler scheduler)

Connects to the upstream ConnectableFlowable if the number of subscribed subscriber reaches the specified count and disconnect after the specified timeout if all subscribers have unsubscribed.

Backpressure:
The operator itself doesn't interfere with backpressure which is determined by the upstream ConnectableFlowable's backpressure behavior.
Scheduler:
This refCount overload operates on the specified Scheduler.

History: 2.1.14 - experimental

Parameters
subscriberCount:int

the number of subscribers required to connect to the upstream

timeout:long

the time to wait before disconnecting after all subscribers unsubscribed

unit:@NonNull TimeUnit

the time unit of the timeout

scheduler:@NonNull Scheduler

the target scheduler to wait on before disconnecting

Returns:@NonNull Flowable<T>

the new Flowable instance

Annotations
@CheckReturnValue
@SchedulerSupport:custom
@BackpressureSupport:PASS_THROUGH
@NonNull
Exceptions
NullPointerException:
if unit or scheduler is null
IllegalArgumentException:
if subscriberCount is non-positive
Since
2.2
resetback to summary
public abstract void reset()

Resets this ConnectableFlowable into its fresh state if it has terminated.

Calling this method on a fresh or active ConnectableFlowable has no effect.

Scheduler:
The behavior is determined by the implementor of this abstract class.
Annotations
@SchedulerSupport:none
Since
3.0.0