# RxJava Tip for the Day - Share, Publish, Refcount and All That Jazz

DevFeed: [RxJava Tip for the Day - Share, Publish, Refcount and All That Jazz](<https://devfeed.tech/articles/rxjava-tip-for-the-day-share-publish-refcount-and-all-that-jazz-25324.md>)

Original publisher: [Read original article](<https://kau.sh/blog/rxjava-tip-for-the-day-share-publish-refcount-and-all-that-jazz/>)

Author: Kaushik Gopal

Published: 2015-01-21T07:00:00Z

Content type: tutorial

Language: en

Sources: [Kaushik Gopal's Site](<https://devfeed.tech/sources/kaushik-gopal-s-site.md>)

Topics: [RxJava](<https://devfeed.tech/topics/rxjava.md>), [implementation](<https://devfeed.tech/topics/implementation.md>)

Tags: [implementation](<https://devfeed.tech/tags/implementation.md>), [operator](<https://devfeed.tech/tags/operator.md>), [rxjava](<https://devfeed.tech/tags/rxjava.md>), [stream](<https://devfeed.tech/tags/stream.md>), [subscriber](<https://devfeed.tech/tags/subscriber.md>), [subscriptions](<https://devfeed.tech/tags/subscriptions.md>)

## AI overview

This tutorial explains RxJava's share() operator as a wrapper around publish().refcount(). It describes connected observables, multicasting, subscriber coordination, and reference counting during subscription disposal.

## Source excerpt

Ok, so in my previous post I innocuously introduced the .share() operator. Observable<Object> tapEventEmitter = _rxBus.toObserverable().share(); What is this share operator? ## The .share() operator is basically just a wrapper to the chained call .publish().refcount(). You'll find the chained combo .publish().refcount() used in quite a few Rx examples on the web. It allows you to "share" the emission of the stream. Considering how powerpacked and frequently used this combo is, RxJava basically introduced the friendlier more useful operator share(). This mechanism is sometimes referred to as "multicasting". Let's dig into some of the basics first: "ConnectedObservable" - This is a kind of observable which doesn't emit items even if subscribed to. It only starts emitting items after its .connect() method is called. It is for this reason that a connected obesrvable is also considered "cold" or "inactive" before the connect method is invoked. .publish()- This method allows us to change an ordinary observable into a "ConnectedObservable". Simply call this method on an ordinary observable and it becomes a connected one. We now know what 1/2 of the operator share does. Now why would you ever use a Connected Observable? The docs say: In this way you can wait for all intended Subscribers to subscribe to the Observable before the Observable begins emitting items. This essentially means that a regular usecase for publish would involve more than one subscriber. When you have more than one subscriber, it can get tricky to handle each of the subscriptions and dispose them off correctly. To make this process easier, Rx introduced this magical operator called refcount(): refcount() - This operator keeps track of how many subscribers are subscribed to the resulting Observable and refrains from disconnecting from the source ConnectedObservable until all such Observables are unsubscribed. It essentially maintains a reference counter in the background and accordingly takes the correct