forked from livewire/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolling.blade.php
52 lines (39 loc) · 1.4 KB
/
polling.blade.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Livewire offers a directive called `wire:poll` that, when added to an element, will refresh the component every `2s`.
@component('components.tip')
Polling for changes over Ajax is a lightweight, simpler alternative to something like Laravel Echo, Pusher, or any WebSocket strategy.
@endcomponent
@component('components.code')
@verbatim
<div wire:poll>
Current time: {{ now() }}
</div>
@endverbatim
@endcomponent
You can customize the frequency by passing a directive modifier like `750ms`. For example:
@component('components.code')
@verbatim
<div wire:poll.750ms>
Current time: {{ now() }}
</div>
@endverbatim
@endcomponent
You can also specify a specific action to fire on the polling interval by passing a value to `wire:poll`:
@component('components.code')
@verbatim
<div wire:poll="foo">
Current time: {{ now() }}
</div>
@endverbatim
@endcomponent
Now, the `foo` method on the component will be called every 2 seconds.
## Polling in the background
Livewire reduces polling when the browser tab is in the background so that it doesn't bog down the server with ajax requests unnecessarily.
Only about 5% of the expected polling requests are kept.
If you'd like to keep polling at the normal rate even while the tab is in the background, you can use the `keep-alive` modifier:
@component('components.code')
@verbatim
<div wire:poll.keep-alive>
Current time: {{ now() }}
</div>
@endverbatim
@endcomponent