Skip to main content

 

Polling

Polling Overview​

Polling gives you the ability to have a 'real-time' effect by causing a query to run at a specified interval. To enable polling for a query, pass a pollingInterval to the useQuery hook or action creator with an interval in milliseconds:

Polling continues while at least one subscriber for the query requests it, and stops when the last polling subscription is removed or disabled.

tip

Polling additionally has the ability to skip sending requests while the window is out of focus. To enable this behavior, pass skipPollingIfUnfocused: true to the useQuery hook or action creator.

Note: skipPollingIfUnfocused requires setupListeners to have been called.

src/Pokemon.tsx
import * as React from 'react'
import { useGetPokemonByNameQuery } from './services/pokemon'

export const Pokemon = ({ name }: { name: string }) => {
// Automatically refetch every 3s unless the window is out of focus
const { data, status, error, refetch } = useGetPokemonByNameQuery(name, {
pollingInterval: 3000,
skipPollingIfUnfocused: true,
})

return <div>{data}</div>
}

In an action creator without React Hooks:

const { data, status, error, refetch } = store.dispatch(
endpoints.getCountById.initiate(id, {
subscriptionOptions: { pollingInterval: 3000 },
}),
)

Computing the Interval from the Cache Entry​

pollingInterval can also be a function. It receives the current cache entry for the query (data, error, status, originalArgs, and so on) and returns the interval in milliseconds. This lets a query poll only while the server reports that work is still in progress:

src/Job.tsx
import * as React from 'react'
import { useGetJobQuery } from './services/jobs'

export const Job = ({ id }: { id: string }) => {
const { data } = useGetJobQuery(id, {
// Poll every second until the job finishes
pollingInterval: ({ data }) => (data?.status === 'running' ? 1000 : 0),
})

return <div>{data?.status}</div>
}

The function follows these rules:

  • It is called when the next poll is scheduled, after a request for that cache entry settles. It is not called while a request is pending, and it runs at most once per cache entry update.
  • A result of 0, a negative number, or anything that is not a number means "don't poll". The function is called again when the cache entry changes (for example after a manual refetch() or a refetch triggered by tag invalidation) or when a different function is passed, and polling resumes if it returns an interval.
  • If the function throws, that subscriber does not poll until the function is called again. The error is logged in development.
  • When several subscribers to the same cache entry ask for polling, the lowest interval wins, whether it came from a number or a function. A function subscriber's skipPollingIfUnfocused only applies while its last result was an interval.

An inline function in a hook can change on every render without updating the subscription while it is polling; the most recent function is used the next time a poll is scheduled. When its last result stopped polling, a re-render with a new function calls it again right away, so a function like () => (isActive ? 1000 : 0) starts polling as soon as isActive becomes true.

Polling Without React Hooks​

If you use polling without the convenience of React Hooks, you will need to manually call updateSubscriptionOptions on the promise ref to update the interval. This approach varies by framework but is possible everywhere. See the Svelte Example for one possibility, and the Usage Without React Hooks page for more details on working with subscriptions manually.

queryRef.updateSubscriptionOptions({ pollingInterval: 0 })