Open History to see the last commit
1---
2id: Subscription
3title: Subscription
4---
5
6# Interface: Subscription
7
8Defined in: [packages/db/src/types.ts:269](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L269)
9
10Public interface for a collection subscription
11Used by sync implementations to track subscription lifecycle
12
13## Extends
14
15- `EventEmitter`\<[`SubscriptionEvents`](../type-aliases/SubscriptionEvents.md)\>
16
17## Properties
18
19### lastError
20
21```ts
22readonly lastError: unknown;
23```
24
25Defined in: [packages/db/src/types.ts:273](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L273)
26
27Most recent subset-load failure observed by this subscription.
28
29***
30
31### status
32
33```ts
34readonly status: SubscriptionStatus;
35```
36
37Defined in: [packages/db/src/types.ts:271](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L271)
38
39Current status of the subscription
40
41## Methods
42
43### clearListeners()
44
45```ts
46protected clearListeners(): void;
47```
48
49Defined in: [packages/db/src/event-emitter.ts:115](https://github.com/TanStack/db/blob/main/packages/db/src/event-emitter.ts#L115)
50
51Clear all listeners
52
53#### Returns
54
55`void`
56
57#### Inherited from
58
59```ts
60EventEmitter.clearListeners
61```
62
63***
64
65### emitInner()
66
67```ts
68protected emitInner<T>(event, eventPayload): void;
69```
70
71Defined in: [packages/db/src/event-emitter.ts:96](https://github.com/TanStack/db/blob/main/packages/db/src/event-emitter.ts#L96)
72
73**`Internal`**
74
75Emit an event to all listeners
76
77#### Type Parameters
78
79##### T
80
81`T` *extends* keyof [`SubscriptionEvents`](../type-aliases/SubscriptionEvents.md)
82
83#### Parameters
84
85##### event
86
87`T`
88
89Event name to emit
90
91##### eventPayload
92
93[`SubscriptionEvents`](../type-aliases/SubscriptionEvents.md)\[`T`\]
94
95Event payload
96 For use by subclasses - subclasses should wrap this with a public emit if needed
97
98#### Returns
99
100`void`
101
102#### Inherited from
103
104```ts
105EventEmitter.emitInner
106```
107
108***
109
110### off()
111
112```ts
113off<T>(event, callback): void;
114```
115
116Defined in: [packages/db/src/event-emitter.ts:53](https://github.com/TanStack/db/blob/main/packages/db/src/event-emitter.ts#L53)
117
118Unsubscribe from an event
119
120#### Type Parameters
121
122##### T
123
124`T` *extends* keyof [`SubscriptionEvents`](../type-aliases/SubscriptionEvents.md)
125
126#### Parameters
127
128##### event
129
130`T`
131
132Event name to stop listening for
133
134##### callback
135
136(`event`) => `void`
137
138Function to remove
139
140#### Returns
141
142`void`
143
144#### Inherited from
145
146```ts
147EventEmitter.off
148```
149
150***
151
152### on()
153
154```ts
155on<T>(event, callback): () => void;
156```
157
158Defined in: [packages/db/src/event-emitter.ts:17](https://github.com/TanStack/db/blob/main/packages/db/src/event-emitter.ts#L17)
159
160Subscribe to an event
161
162#### Type Parameters
163
164##### T
165
166`T` *extends* keyof [`SubscriptionEvents`](../type-aliases/SubscriptionEvents.md)
167
168#### Parameters
169
170##### event
171
172`T`
173
174Event name to listen for
175
176##### callback
177
178(`event`) => `void`
179
180Function to call when event is emitted
181
182#### Returns
183
184Unsubscribe function
185
186```ts
187(): void;
188```
189
190##### Returns
191
192`void`
193
194#### Inherited from
195
196```ts
197EventEmitter.on
198```
199
200***
201
202### once()
203
204```ts
205once<T>(event, callback): () => void;
206```
207
208Defined in: [packages/db/src/event-emitter.ts:37](https://github.com/TanStack/db/blob/main/packages/db/src/event-emitter.ts#L37)
209
210Subscribe to an event once (automatically unsubscribes after first emission)
211
212#### Type Parameters
213
214##### T
215
216`T` *extends* keyof [`SubscriptionEvents`](../type-aliases/SubscriptionEvents.md)
217
218#### Parameters
219
220##### event
221
222`T`
223
224Event name to listen for
225
226##### callback
227
228(`event`) => `void`
229
230Function to call when event is emitted
231
232#### Returns
233
234Unsubscribe function
235
236```ts
237(): void;
238```
239
240##### Returns
241
242`void`
243
244#### Inherited from
245
246```ts
247EventEmitter.once
248```
249
250***
251
252### waitFor()
253
254```ts
255waitFor<T>(event, timeout?): Promise<SubscriptionEvents[T]>;
256```
257
258Defined in: [packages/db/src/event-emitter.ts:66](https://github.com/TanStack/db/blob/main/packages/db/src/event-emitter.ts#L66)
259
260Wait for an event to be emitted
261
262#### Type Parameters
263
264##### T
265
266`T` *extends* keyof [`SubscriptionEvents`](../type-aliases/SubscriptionEvents.md)
267
268#### Parameters
269
270##### event
271
272`T`
273
274Event name to wait for
275
276##### timeout?
277
278`number`
279
280Optional timeout in milliseconds
281
282#### Returns
283
284`Promise`\<[`SubscriptionEvents`](../type-aliases/SubscriptionEvents.md)\[`T`\]\>
285
286Promise that resolves with the event payload
287
288#### Inherited from
289
290```ts
291EventEmitter.waitFor
292```