Any way to halt polling?

I’m successfully using Livewire to monitor the status of some jobs which process input and post to a third party API. The livewire component reports the real time progression of a number of these task that were all submitted via CSV file(s).

Because the tasks don’t actually take that long, I would like sub-second polling.

Sooner or later, all the ‘jobs’ are complete and I would like to halt polling. This could be done from either side, but probably easiest signalled from Laravel. Once the work s complete, I don’t want Livewire to keep polling the server.

2 Likes

I’d recommend using Echo instead of polling: https://laravel-livewire.com/docs/events/#echo-events

Great question. I have no idea but would love to know the answer.

Would you be able to modify the blade template that the livewire component returns to remove the polling directive once Laravel has decided that the job is done and polling should stop?

So if polling is no longer needed a parameter would be passed to the view which would be checked using blade if logic and it would ommit the code that handles the polling “wire:poll”?

That should work in theory but I have not tried it.

1 Like

Hey @csti, I don’t think that would work in reality. There is no internal mechanism to stop polling once an element no longer has the wire:poll attribute.

However, it wouldn’t actually be that hard to add the functionality to the framework.

I’m thinking it would be a one or two line add to src/js/component/polling.js inside the interval handler to do a check on the element to see if it still has a wire:poll attribute.

I’m open to a PR for it.

1 Like

I thought I would try just changing the template on the fly (with a new poll interval) but this does not work, and actually starts throwing an error

Uncaught TypeError: Cannot read property 'match' of null
    at _default.parseOutMethodAndParams (livewire.js?id=8706e53…:3368)
    at _default.get (livewire.js?id=8706e53…:3410)
    at livewire.js?id=8706e53…:3099

Hi. I have converted from pusher to livewire for this functionality to cut down on third party dependencies.

The solution works well, and I could live with it as it is, but I’m worried the users might leave the page in view long after it has finished.

1 Like

I ran into the same problem and solved it in a merge request which has now been merged by @calebporzio

1 Like

It would also be nice to slow down polling when document.hidden is true :slight_smile:

Hi. If I understand you, Livewire already does this

Livewire reduces polling when the browser tab is in the background so that it doesn’t bog down the server with ajax requests unnecessarily.