> For the complete documentation index, see [llms.txt](https://soufatn.gitbook.io/rxjs-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://soufatn.gitbook.io/rxjs-book/summary/subjects/replay_subject.md).

# Rx.ReplaySubject

Represents an object that is both an observable sequence as well as an observer. Each notification is broadcasted to all subscribed and future observers, subject to buffer trimming policies.

This class inherits both from the `Rx.Observable` and `Rx.Observer` classes.

## Usage #\#

The follow example shows the basic usage of an `Rx.ReplaySubject` class. Note that this only holds the past two items in the cache.

```javascript
var subject = new Rx.ReplaySubject(2 /* buffer size */);

subject.onNext('a');
subject.onNext('b');
subject.onNext('c');

var subscription = subject.subscribe(
    function (x) {
        console.log('Next: ' + x.toString());
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

// => Next: b
// => Next: c

subject.onNext('d');
// => Next: d
```

### Location

* rx.binding.js

## `ReplaySubject Constructor` #\#

* [`constructor`](/rxjs-book/summary/subjects/replay_subject.md#rx)

## `ReplaySubject Instance Methods` #\#

* [`dispose`](/rxjs-book/summary/subjects/replay_subject.md#rxreplaysubjectprototypedispose)
* [`hasObservers`](/rxjs-book/summary/subjects/replay_subject.md#rxreplaysubjectprototypehasobservers)

## Inherited Classes #\#

* [`Rx.Observable`](https://github.com/Reactive-Extensions/RxJS/blob/master/doc/core/observable.md)
* [`Rx.Observer`](https://github.com/Reactive-Extensions/RxJS/blob/master/doc/core/observer.md)

## *ReplaySubject Constructor* #\#

### `Rx.ReplaySubject([bufferSize], [windowSize], [scheduler])` <a href="#rxreplaysubjectbuffersize-windowsize-scheduler" id="rxreplaysubjectbuffersize-windowsize-scheduler"></a>

[#](/rxjs-book/summary/subjects/replay_subject.md#rxreplaysubjectintialvalue) [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/subjects/replaysubject.js#L53-L64)

Initializes a new instance of the `Rx.ReplaySubject` class with the specified buffer size, window and scheduler.

#### Arguments

1. `[bufferSize = Number.MAX_VALUE]` *(Number)*: Maximum element count of the replay buffer.
2. `[windowSize = NUMBER.MAX_VALUE]` *(Number)*: Maximum time length of the replay buffer.
3. `[scheduler = Rx.Scheduler.currentThread]` *(Scheduler)*: Scheduler the observers are invoked on.

#### Example

```javascript
var subject = new Rx.ReplaySubject(
    2 /* buffer size */,
    null /* unlimited time buffer */,
    Rx.Scheduler.timeout);

subject.onNext('a');
subject.onNext('b');
subject.onNext('c');

var subscription = subject.subscribe(
    function (x) {
        console.log('Next: ' + x.toString());
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

// => Next: b
// => Next: c

subject.onNext('d');
// => Next: d
```

### Location

* rx.binding.js

## *ReplaySubject Instance Methods* #\#

### `Rx.ReplaySubject.prototype.dispose()` <a href="#rxreplaysubjectprototypedispose" id="rxreplaysubjectprototypedispose"></a>

[#](/rxjs-book/summary/subjects/replay_subject.md#rxreplaysubjectprototypedispose) [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/subjects/ReplaySubject.js#L147-L150)

Unsubscribe all observers and release resources.

#### Example

```javascript
var subject = new Rx.ReplaySubject();

var subscription = subject.subscribe(
    function (x) {
        console.log('Next: ' + x.toString());
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

subject.onNext(42);
// => Next: 42

subject.onCompleted();
// => Completed

subject.dispose();

try {
    subject.onNext(56);
} catch (e) {
    console.log(e.message);
}

// => Object has been disposed
```

### Location

* rx.binding.js

### `Rx.ReplaySubject.prototype.hasObservers()` <a href="#rxreplaysubjectprototypehasobservers" id="rxreplaysubjectprototypehasobservers"></a>

[#](/rxjs-book/summary/subjects/replay_subject.md#rxreplaysubjectprototypehasobservers) [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/subjects/ReplaySubject.js#L71-L73)

Indicates whether the subject has observers subscribed to it.

#### Returns

*(Boolean)*: Returns `true` if the Subject has observers, else `false`.

#### Example

```javascript
var subject = new Rx.ReplaySubject();

console.log(subject.hasObservers());

// => false

var subscription = subject.subscribe(
    function (x) {
        console.log('Next: ' + x.toString());
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

console.log(subject.hasObservers());

// => true
```

### Location

* rx.binding.js
