---
url: https://docs.moqtail.dev/ts/api/client/classes/Subscription.md
---
# Class: Subscription

Owns the single consumer-facing stream for a SUBSCRIBE that may be switched to a
new track one or more times. A soft switch can keep producing from the old track
for an unknown time after SUBSCRIBE_OK, and a second switch may be issued before the
first target ever delivers anything, so only the first object actually delivered by
the newest tracked request is the guarantee that nothing older will send again.

That first delivery is the handover, not the end of the old track: a soft switch
leaves it delivering up to the group the new one starts at, so the two meet without
a hole. Those objects keep coming through here until the publisher says the
subscription is done, or until it has been quiet long enough to say so itself.

## Constructors

### Constructor

> **new Subscription**(`initial`): `Subscription`

#### Parameters

##### initial

[`SubscribeRequest`](SubscribeRequest.md)

#### Returns

`Subscription`

## Properties

### done

> `readonly` **done**: `Promise`<[`PublishDone`](../../model/classes/PublishDone.md) | `undefined`>

Resolves once every tracked request is over, with the newest one's PUBLISH_DONE, or
`undefined` if it ended without one. Never rejects.

***

### drainTimeoutMs

> **drainTimeoutMs**: `number` = `DEFAULT_SWITCH_DRAIN_TIMEOUT_MS`

How long a drained request may stay quiet before it is retired anyway, in
milliseconds. The publisher normally ends the drain with a PUBLISH_DONE; this is
what keeps a request that never gets one from being tracked forever.

***

### objects

> `readonly` **objects**: [`ObjectStream`](ObjectStream.md)

***

### onDrainDecision

> **onDrainDecision**: (`request`) => `boolean`

Set by the client to decide whether draining a request should continue.

#### Parameters

##### request

[`SubscribeRequest`](SubscribeRequest.md)

#### Returns

`boolean`

***

### onSuperseded?

> `optional` **onSuperseded?**: (`superseded`) => `void`

Set once by `MOQtailClient.switch`; invoked with everything older on cutover.

#### Parameters

##### superseded

[`SubscribeRequest`](SubscribeRequest.md)\[]

#### Returns

`void`

***

### stream

> `readonly` **stream**: `ReadableStream`<[`MoqtObject`](../../model/classes/MoqtObject.md)>

## Accessors

### newest

#### Get Signature

> **get** **newest**(): [`SubscribeRequest`](SubscribeRequest.md)

The request most recently switched to; must deliver data to win the handover.

##### Returns

[`SubscribeRequest`](SubscribeRequest.md)

***

### requests

#### Get Signature

> **get** **requests**(): readonly [`SubscribeRequest`](SubscribeRequest.md)\[]

Requests still tracked, oldest first, draining ones last.

##### Returns

readonly [`SubscribeRequest`](SubscribeRequest.md)\[]

## Methods

### addSwitchTarget()

> **addSwitchTarget**(`request`, `drainSuperseded?`): `void`

Track a newly issued switch target alongside whatever is still delivering.

`drainSuperseded` says what its handover does to the requests it takes over from:
a soft switch leaves them delivering to the end of what the publisher was told to
finish, a hard one retires them on the spot.

#### Parameters

##### request

[`SubscribeRequest`](SubscribeRequest.md)

##### drainSuperseded?

`boolean` = `false`

#### Returns

`void`

***

### clear()

> **clear**(): `void`

Stop tracking everything (used when the whole switch chain is torn down at once).

#### Returns

`void`

***

### deliver()

> **deliver**(`request`, `obj`, `previous?`): `boolean`

Delivers an object received for `request`. The first object delivered by the
newest tracked request retires everything older — only then is it guaranteed no
more data is coming from them.

Returns `true` if the object was delivered to the caller,
`false` if it was not consumed, and client should cancel the stream.

#### Parameters

##### request

[`SubscribeRequest`](SubscribeRequest.md)

##### obj

[`MoqtObject`](../../model/classes/MoqtObject.md)

##### previous?

[`Location`](../../model/classes/Location.md)

#### Returns

`boolean`

***

### drop()

> **drop**(`request`): `void`

Stop tracking `request` (e.g. it naturally completed without ever losing/winning a handover).

#### Parameters

##### request

[`SubscribeRequest`](SubscribeRequest.md)

#### Returns

`void`

***

### output()

> **output**(`request`, `obj`): `void`

Writes an object `request` released. While older requests still deliver, the newest
one's ordered objects wait, so the old track's tail comes first.

#### Parameters

##### request

[`SubscribeRequest`](SubscribeRequest.md)

##### obj

[`MoqtObject`](../../model/classes/MoqtObject.md)

#### Returns

`void`
