Open History to see the last commit
1---
2id: OnlineManager
3title: OnlineManager
4---
5β
6Defined in: [packages/query-core/src/onlineManager.ts:15](https://github.com/TanStack/query/blob/main/packages/query-core/src/onlineManager.ts#L15)
7β
8The `OnlineManager` manages the online state within TanStack Query. It can
9be used to change the default event listeners or to manually change the
10online state.
11β
12By default, the `onlineManager` assumes an active network connection, and
13listens to the `online` and `offline` events on the `window` object to
14detect changes.
15β
16## Extends
17β
18- `Subscribable`\<`Listener`\>
19β
20## Methods
21β
22### hasListeners()
23β
24```ts
25hasListeners(): boolean;
26```
27β
28Defined in: [packages/query-core/src/subscribable.ts:43](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L43)
29β
30Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.
31β
32#### Returns
33β
34`boolean`
35β
36`true` if at least one listener is registered.
37β
38#### Inherited from
39β
40```ts
41Subscribable.hasListeners
42```
43β
44***
45β
46### isOnline()
47β
48```ts
49isOnline(): boolean;
50```
51β
52Defined in: [packages/query-core/src/onlineManager.ts:111](https://github.com/TanStack/query/blob/main/packages/query-core/src/onlineManager.ts#L111)
53β
54`isOnline` can be used to get the current online state.
55β
56#### Returns
57β
58`boolean`
59β
60`true` if online.
61β
62***
63β
64### setEventListener()
65β
66```ts
67setEventListener(setup: SetupFn): void;
68```
69β
70Defined in: [packages/query-core/src/onlineManager.ts:76](https://github.com/TanStack/query/blob/main/packages/query-core/src/onlineManager.ts#L76)
71β
72`setEventListener` can be used to set a custom event listener that will
73be used to determine the online state. The provided `setup` function
74receives a `setOnline` callback that should be called with a `boolean`
75whenever the online state changes.
76β
77#### Parameters
78β
79##### setup
80β
81`SetupFn`
82β
83Receives the `setOnline` callback, registers the event listener, and may return
84a cleanup function that is called when the listener is replaced or no longer needed.
85β
86#### Returns
87β
88`void`
89β
90#### Example
91β
92```ts
93import NetInfo from '@react-native-community/netinfo'
94import { onlineManager } from '@tanstack/query-core'
95β
96onlineManager.setEventListener((setOnline) => {
97 return NetInfo.addEventListener((state) => {
98 setOnline(!!state.isConnected)
99 })
100})
101```
102β
103***
104β
105### setOnline()
106β
107```ts
108setOnline(online: boolean): void;
109```
110β
111Defined in: [packages/query-core/src/onlineManager.ts:96](https://github.com/TanStack/query/blob/main/packages/query-core/src/onlineManager.ts#L96)
112β
113`setOnline` can be used to manually set the online state.
114β
115#### Parameters
116β
117##### online
118β
119`boolean`
120β
121The online state.
122β
123#### Returns
124β
125`void`
126β
127#### Example
128β
129```ts
130import { onlineManager } from '@tanstack/query-core'
131β
132// Set to online
133onlineManager.setOnline(true)
134β
135// Set to offline
136onlineManager.setOnline(false)
137```
138β
139***
140β
141### subscribe()
142β
143```ts
144subscribe(listener: Listener): () => void;
145```
146β
147Defined in: [packages/query-core/src/subscribable.ts:28](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L28)
148β
149Registers a listener to be called on every update this object notifies about. Returns a function
150that removes the listener again β call it to stop listening. The base class never drops a listener
151on its own, though some subclasses clear all of theirs in `destroy()`.
152β
153#### Parameters
154β
155##### listener
156β
157`Listener`
158β
159Called on each update, with whatever the subclass passes to its subscribers.
160β
161#### Returns
162β
163A function that removes the listener.
164β
165() => `void`
166β
167#### Example
168β
169```ts
170const unsubscribe = subscribable.subscribe(() => {
171 // react to the update
172})
173β
174unsubscribe()
175```
176β
177#### Inherited from
178β
179```ts
180Subscribable.subscribe
181```