Polling
Poll Helper
Polling your server for new information on the current page is common, so Inertia provides a helper to reduce boilerplate. It automatically stops polling when the page is unmounted. For data your server already broadcasts, live props refresh a prop the moment its event fires instead of on an interval.
The only required argument is the polling interval in milliseconds.
1import { usePoll } from '@inertiajs/vue3'23usePoll(2000)1import { usePoll } from '@inertiajs/vue3'23usePoll(2000)
1import { usePoll } from '@inertiajs/react'23usePoll(2000)1import { usePoll } from '@inertiajs/react'23usePoll(2000)
1import { usePoll } from '@inertiajs/svelte'23usePoll(2000)1import { usePoll } from '@inertiajs/svelte'23usePoll(2000)
If you need to pass additional request options to the poll helper, you can pass any of the router.reload options as the second parameter.
1import { usePoll } from '@inertiajs/vue3'23usePoll(2000, {4 onStart() {5 console.log('Polling request started')6 },7 onFinish() {8 console.log('Polling request finished')9 },10})1import { usePoll } from '@inertiajs/vue3'23usePoll(2000, {4 onStart() {5 console.log('Polling request started')6 },7 onFinish() {8 console.log('Polling request finished')9 },10})
1import { usePoll } from '@inertiajs/react'23usePoll(2000, {4 onStart() {5 console.log('Polling request started')6 },7 onFinish() {8 console.log('Polling request finished')9 },10})1import { usePoll } from '@inertiajs/react'23usePoll(2000, {4 onStart() {5 console.log('Polling request started')6 },7 onFinish() {8 console.log('Polling request finished')9 },10})
1import { usePoll } from '@inertiajs/svelte'23usePoll(2000, {4 onStart() {5 console.log('Polling request started')6 },7 onFinish() {8 console.log('Polling request finished')9 },10})1import { usePoll } from '@inertiajs/svelte'23usePoll(2000, {4 onStart() {5 console.log('Polling request started')6 },7 onFinish() {8 console.log('Polling request finished')9 },10})
You may also pass a function that returns the request options. The function is evaluated on every tick, allowing the poll to reflect the latest component state.
1<script setup>2import { usePoll } from '@inertiajs/vue3'34const props = defineProps({ counter: Number })56usePoll(2000, () => ({7 data: { counter_seen: props.counter },8 only: ['last_received'],9}))10</script>1<script setup>2import { usePoll } from '@inertiajs/vue3'34const props = defineProps({ counter: Number })56usePoll(2000, () => ({7 data: { counter_seen: props.counter },8 only: ['last_received'],9}))10</script>
1import { usePoll } from '@inertiajs/react'23export default ({ counter }) => {4 usePoll(2000, () => ({5 data: { counter_seen: counter },6 only: ['last_received'],7 }))8}1import { usePoll } from '@inertiajs/react'23export default ({ counter }) => {4 usePoll(2000, () => ({5 data: { counter_seen: counter },6 only: ['last_received'],7 }))8}
1import { usePoll } from '@inertiajs/svelte'23let { counter } = $props()45usePoll(2000, () => ({6 data: { counter_seen: counter },7 only: ['last_received'],8}))1import { usePoll } from '@inertiajs/svelte'23let { counter } = $props()45usePoll(2000, () => ({6 data: { counter_seen: counter },7 only: ['last_received'],8}))
If you'd like more control over the polling behavior, the poll helper provides stop and start methods that allow you to manually start and stop polling. You can pass the autoStart: false option to the poll helper to prevent it from automatically starting polling when the component is mounted.
1<script setup>2import { usePoll } from '@inertiajs/vue3'34const { start, stop } = usePoll(2000, {}, {5 autoStart: false,6})7</script>89<template>10 <button @click="start">Start polling</button>11 <button @click="stop">Stop polling</button>12</template>1<script setup>2import { usePoll } from '@inertiajs/vue3'34const { start, stop } = usePoll(2000, {}, {5 autoStart: false,6})7</script>89<template>10 <button @click="start">Start polling</button>11 <button @click="stop">Stop polling</button>12</template>
1import { usePoll } from '@inertiajs/react'23export default () => {4 const { start, stop } = usePoll(2000, {}, {5 autoStart: false,6 })78 return (9 <div>10 <button onClick={start}>Start polling</button>11 <button onClick={stop}>Stop polling</button>12 </div>13 )14}1import { usePoll } from '@inertiajs/react'23export default () => {4 const { start, stop } = usePoll(2000, {}, {5 autoStart: false,6 })78 return (9 <div>10 <button onClick={start}>Start polling</button>11 <button onClick={stop}>Stop polling</button>12 </div>13 )14}
1import { usePoll } from '@inertiajs/svelte'23const { start, stop } = usePoll(2000, {}, {4 autoStart: false,5})1import { usePoll } from '@inertiajs/svelte'23const { start, stop } = usePoll(2000, {}, {4 autoStart: false,5})
v3.7.0+
The poll helper also returns a reactive polling value that reflects whether the poll is running, making it ideal for rendering a pause or resume button. The value doesn't indicate whether a request is in flight.
1<script setup>2import { usePoll } from '@inertiajs/vue3'34const { start, stop, polling } = usePoll(2000)5</script>67<template>8 <button v-if="polling" @click="stop">Pause updates</button>9 <button v-else @click="start">Resume updates</button>10</template>1<script setup>2import { usePoll } from '@inertiajs/vue3'34const { start, stop, polling } = usePoll(2000)5</script>67<template>8 <button v-if="polling" @click="stop">Pause updates</button>9 <button v-else @click="start">Resume updates</button>10</template>
1import { usePoll } from '@inertiajs/react'23export default () => {4 const { start, stop, polling } = usePoll(2000)56 return polling7 ? <button onClick={stop}>Pause updates</button>8 : <button onClick={start}>Resume updates</button>9}1import { usePoll } from '@inertiajs/react'23export default () => {4 const { start, stop, polling } = usePoll(2000)56 return polling7 ? <button onClick={stop}>Pause updates</button>8 : <button onClick={start}>Resume updates</button>9}
1<script>2import { usePoll } from '@inertiajs/svelte'34const poll = usePoll(2000)5</script>67{#if poll.polling}8 <button onclick={poll.stop}>Pause updates</button>9{:else}10 <button onclick={poll.start}>Resume updates</button>11{/if}1<script>2import { usePoll } from '@inertiajs/svelte'34const poll = usePoll(2000)5</script>67{#if poll.polling}8 <button onclick={poll.stop}>Pause updates</button>9{:else}10 <button onclick={poll.start}>Resume updates</button>11{/if}
Concurrency Mode
By default, the poll helper fires a new request on every tick, even when the previous request is still in flight. You may control this behavior with the mode option, which accepts one of three values: overlap, cancel, or rest.
The default overlap mode allows requests to run in parallel. The cancel mode aborts any in-flight request when the next tick fires. The rest mode treats the interval as the time between the end of the previous request and the start of the next one, so requests never overlap.
1import { usePoll } from '@inertiajs/vue3'23usePoll(2000, {}, {4 mode: 'rest',5})1import { usePoll } from '@inertiajs/vue3'23usePoll(2000, {}, {4 mode: 'rest',5})
1import { usePoll } from '@inertiajs/react'23usePoll(2000, {}, {4 mode: 'rest',5})1import { usePoll } from '@inertiajs/react'23usePoll(2000, {}, {4 mode: 'rest',5})
1import { usePoll } from '@inertiajs/svelte'23usePoll(2000, {}, {4 mode: 'rest',5})1import { usePoll } from '@inertiajs/svelte'23usePoll(2000, {}, {4 mode: 'rest',5})
Background Tabs
v3.9.0+
By default, the poll helper throttles requests by 90% when the browser tab is in the background. You may control this behavior with the background option, which accepts one of three values: throttle, pause, or continue.
The pause mode stops polling entirely while the tab is hidden. Once the tab becomes visible again, polling resumes, firing a request immediately if the interval has already elapsed since the last one. The continue mode keeps polling at the regular interval, even while the tab is hidden.
1import { usePoll } from '@inertiajs/vue3'23usePoll(2000, {}, {4 background: 'pause',5})1import { usePoll } from '@inertiajs/vue3'23usePoll(2000, {}, {4 background: 'pause',5})
1import { usePoll } from '@inertiajs/react'23usePoll(2000, {}, {4 background: 'pause',5})1import { usePoll } from '@inertiajs/react'23usePoll(2000, {}, {4 background: 'pause',5})
1import { usePoll } from '@inertiajs/svelte'23usePoll(2000, {}, {4 background: 'pause',5})1import { usePoll } from '@inertiajs/svelte'23usePoll(2000, {}, {4 background: 'pause',5})
The keepAlive option is deprecated in favor of background: 'continue'.