# RelayTrackWriter

URL: https://www.caprid.com/docs/reference/typescript/caprid/relay/index/interfaces/relaytrackwriter/

Defined in: [relay-sdks/ts/src/publish.ts:138](https://github.com/caprids/relay-sdks/blob/main/ts/src/publish.ts#L138)

One registered track on a publisher connection, from [RelayPublisher.registerTrack](https://www.caprid.com/docs/reference/typescript/caprid/relay/index/interfaces/relaypublisher/#registertrack).
Registration sends nothing; `write()` and `openGroup()` send, after waiting for relay demand.
The behaviour behind this interface is the track's write slot (see CONTEXT.md and the
"Write phases and interruptions" table in spec/behavior.md).

## Properties

### closed

```ts
readonly closed: boolean;
```

Defined in: [relay-sdks/ts/src/publish.ts:146](https://github.com/caprids/relay-sdks/blob/main/ts/src/publish.ts#L146)

True once the application closed the track or the publisher connection ended.

***

### hasDemand

```ts
readonly hasDemand: boolean;
```

Defined in: [relay-sdks/ts/src/publish.ts:144](https://github.com/caprids/relay-sdks/blob/main/ts/src/publish.ts#L144)

Whether the relay currently has an upstream subscription for this track, so a write would
proceed at once. Demand is the relay's own subscription, shared by every downstream viewer
and ended when the relay chooses: it is neither a viewer count nor a liveness signal. Always false once the track is closed.

## Methods

### close()

```ts
close(): void;
```

Defined in: [relay-sdks/ts/src/publish.ts:185](https://github.com/caprids/relay-sdks/blob/main/ts/src/publish.ts#L185)

End the track and cancel an outstanding write. The publisher connection stays open.

#### Returns

`void`

***

### demandChanges()

```ts
demandChanges(): AsyncIterableIterator<boolean>;
```

Defined in: [relay-sdks/ts/src/publish.ts:153](https://github.com/caprids/relay-sdks/blob/main/ts/src/publish.ts#L153)

The current demand state first, then one value per relay subscription change: `true` when the
relay subscribes or replaces its subscription (so `true` can repeat), `false` when its
subscription ends. Ends when the track ends. This is how a catalog re-sends its document on
every relay subscription. Values are per relay subscription, never per downstream viewer.

#### Returns

`AsyncIterableIterator`\<`boolean`\>

***

### openGroup()

```ts
openGroup(options?): Promise<RelayGroupWriter>;
```

Defined in: [relay-sdks/ts/src/publish.ts:166](https://github.com/caprids/relay-sdks/blob/main/ts/src/publish.ts#L166)

Open a group and hold the track's single write slot until it closes or aborts. Waits for relay
demand, for room under the connection's settling-group limit and for the stream, within
writeTimeoutMs.

With `payload`, the group opens with it as object 0 and resolves once the transport takes it
(`objectCount` is 1). Its delivery timeout counts from this call, so every wait before the stream
opens counts (draft-22 §5.2), and a `0x02` or `0x06` property in `properties` sets the group's
delivery timeouts. If it expires the call fails with `DELIVERY_TIMEOUT` and the group is reset.
Without `payload` there is no object to time, so only writeTimeoutMs bounds the wait; `properties`
without `payload` is a TypeError.

#### Parameters

| Parameter | Type |
| ------ | ------ |
| `options?` | \{ `payload?`: `ArrayBufferView`\<`ArrayBufferLike`\>; `properties?`: readonly [`PublishProperty`](https://www.caprid.com/docs/reference/typescript/caprid/relay/index/interfaces/publishproperty/)[]; `signal?`: `AbortSignal`; \} |
| `options.payload?` | `ArrayBufferView`\<`ArrayBufferLike`\> |
| `options.properties?` | readonly [`PublishProperty`](https://www.caprid.com/docs/reference/typescript/caprid/relay/index/interfaces/publishproperty/)[] |
| `options.signal?` | `AbortSignal` |

#### Returns

`Promise`\<[`RelayGroupWriter`](https://www.caprid.com/docs/reference/typescript/caprid/relay/index/interfaces/relaygroupwriter/)\>

***

### write()

```ts
write(payload, options?): Promise<void>;
```

Defined in: [relay-sdks/ts/src/publish.ts:180](https://github.com/caprids/relay-sdks/blob/main/ts/src/publish.ts#L180)

Send one independent object/group. Waits for relay demand (see hasDemand). Resolves when the
transport accepts the complete group, not when a downstream viewer acknowledges it.
Only one write may be pending; await it before writing again. Any ArrayBufferView is
copied as its raw bytes; anything else is a TypeError that leaves the track usable.
Properties are encoded as supplied; the relay may transform them before delivery.
Failure resets this group and leaves the track usable while the connection is open.
Data may already have reached the relay; choose the next object explicitly (no automatic retry).

#### Parameters

| Parameter | Type |
| ------ | ------ |
| `payload` | `ArrayBufferView` |
| `options?` | \{ `properties?`: readonly [`PublishProperty`](https://www.caprid.com/docs/reference/typescript/caprid/relay/index/interfaces/publishproperty/)[]; `signal?`: `AbortSignal`; \} |
| `options.properties?` | readonly [`PublishProperty`](https://www.caprid.com/docs/reference/typescript/caprid/relay/index/interfaces/publishproperty/)[] |
| `options.signal?` | `AbortSignal` |

#### Returns

`Promise`\<`void`\>

***

### writeJSON()

```ts
writeJSON(value, options?): Promise<void>;
```

Defined in: [relay-sdks/ts/src/publish.ts:189](https://github.com/caprids/relay-sdks/blob/main/ts/src/publish.ts#L189)

Serialize with JSON.stringify and write one object. Serialization errors leave the writer usable.

#### Parameters

| Parameter | Type |
| ------ | ------ |
| `value` | `unknown` |
| `options?` | \{ `properties?`: readonly [`PublishProperty`](https://www.caprid.com/docs/reference/typescript/caprid/relay/index/interfaces/publishproperty/)[]; `signal?`: `AbortSignal`; \} |
| `options.properties?` | readonly [`PublishProperty`](https://www.caprid.com/docs/reference/typescript/caprid/relay/index/interfaces/publishproperty/)[] |
| `options.signal?` | `AbortSignal` |

#### Returns

`Promise`\<`void`\>

***

### writeText()

```ts
writeText(text, options?): Promise<void>;
```

Defined in: [relay-sdks/ts/src/publish.ts:187](https://github.com/caprids/relay-sdks/blob/main/ts/src/publish.ts#L187)

Encode UTF-8 text and write one object with the same options and semantics as write().

#### Parameters

| Parameter | Type |
| ------ | ------ |
| `text` | `string` |
| `options?` | \{ `properties?`: readonly [`PublishProperty`](https://www.caprid.com/docs/reference/typescript/caprid/relay/index/interfaces/publishproperty/)[]; `signal?`: `AbortSignal`; \} |
| `options.properties?` | readonly [`PublishProperty`](https://www.caprid.com/docs/reference/typescript/caprid/relay/index/interfaces/publishproperty/)[] |
| `options.signal?` | `AbortSignal` |

#### Returns

`Promise`\<`void`\>
