PeterF's avatar

Livewire import counter

Hi friends. I have an existing page flow in my TALL stack app that prompts for a file of transactions to be uploaded and then processes them with the Maatwebsite library for reading excel and csv files. In that package, I use the OnEachRow trait to call my own convoluted transaction processing logic that must run for each record read.

On of the things that implemented method onRow does, is to keep track via class level variables, of the number of transactions that have loaded successfully, and the number that have failed. Currently, when the file finishes processing, which could be a minute, it then displays those two numbers and some data about the failures, to the end user.

As that is clearly a substandard user experience, I want to make the upload a bit more dynamic, and it seem like the way is to have the number of records processed be incrementing on screen in real time, and also the number of errors incrementing in real time.

The problem I have is how to make that work. It seems like Livewire should be able to help me, but the question then, is where do I call into the Maatwebsite library to start the import? Fundamentally there is a "go" button which starts the import currently, so that could easily just call a livewire view into existence which tried to render the number of records currently processed/passed/failed... but where in the livewire component lifecycle do I call the serverside file read? Should I use wire:poll somewhere? Should I really just be firing off a job into a queue to process the file? I dont think I can send an event from a job to a livewire component, so the whole thing starts to unravel..... so then I think about a DB table or REDIS cache to hold shared state between livewire and the server doing the import.... it all starts to get terribly complicated for what I thought should be an easy thing to do.....

What have I missed? Surely I am not the first person to want to update the counter in real time for a file upload?

Thanks in advance.

0 likes
1 reply
PeterF's avatar
PeterF
OP
Best Answer
Level 6

There is nothing quite like publicly outing yourself on the internet to spur you on to find the answer. At least that's normally how it works for me.

So the answer to this question, was of course me not knowing what I did not know. And eventually I found what I didn't know and now I can learn it.

So do architect such a thing, you need to use the laravel broadcasting system, I am going with the open source websockets replacement for Pusher for the moment, and then in my Livewire component, I wire it up to Laravel Echo (which is the piece of the puzzle I didn't know existed).

Please or to participate in this conversation.