Best practice to solve Livewire UI Not updating until after API call - laravel

I am using Laravel Livewire, I currently have a form that captures a user's address information, then it needs to submit that to a 3rd party API (it's for billing information).
The issue that I am facing is that the UI doesnt update / livewire doesnt call render until after the entire method has run - which looks on the frontend like the UI is frozen / system isnt doing anything as the API is slow and takes a few seconds (15 - 20) to give a response.
I want to be able to trigger a browser event to display an alert saying the request is processing / show an alert banner on the page that it is processing before making the API request, process the API request, then display the results.
Currently I have identified the following ways to make this happen:
make the updates on the frontend, fire off an event with the data needed. Laravel event listener handles the API call, and emits a result event that the livewire component listens for, then the livewire component updates the page with the results.
make the updates on the frontend, dispatch a job to run in a queue worker, emit an event that the livewire component listens for, update the page with the results.
emit an event from the livewire component to itself, the livewire component then listens for that event and processes the API request, then updates the frontend with the results.
emit an event from the livewire component to itself to update the UI with processing info, while it still runs the API process in the original method call, and then updates the UI once the API gets a response
My thoughts are, what would be best practice here? All the above solutions require a different amount of work to get running, and solution 1 and 2 looks like complete overkill for such a simple process.
Does this come down to preference? Or is there actually a best practice way to do this in livewire?
And additionally, is there another way / simpler way to do this that I havnt mentioned above?
Your feedback is appreciated.

I would probably create a job and then return a response to the user. Then instigate Livewire polling that can check for the job being complete. If the API takes 15-20 seconds then polling once per second should be fine.
You might want to store the state in the database so that the job and the livewire component can communicate with each other. If you don't need to keep the data, using the cache to communicate between job and component would be a good alternative.

Related

Apollo GraphQL "writeQuery" causing re-render to the app which is leading to an unnecessary network request

I'm working on a chat app, developed using Apollo GraphQL and React and I have the following issue:
I have a component called "Conversation" which is fetching an array of messages between two users (the logged-in user and other user) using the "useQuery" hook of Apollo which is invoked every time the component mounts.
When a new message is sent by the logged-in user, I'm updating the cached array of messages using the "writeQuery" function provided by Apollo.
By doing that, the "Conversation" component is getting re-rendered, which is leading to an unnecessary network request because the "useQuery" hook is triggered again.
I would love to hear if there is a way of preventing "writeQuery" from triggering a re-render to the relevant component.
Thanks a lot in advance!

Symfony3.4 - Send a Server-Sent Event when something happens

I'm developing a project with Symfony 3.4.
I would like to send a message to all the clients currently viewing a page when something happens. I evaluated both Server-Sent Events and Websockets, and I decided to go with the former, because the communication is unidirectional (only server to client).
For this purpose, I'm using this library: https://packagist.org/packages/tonyhhyip/sse
It seems to work, but I need to specifically send a message when something happens in the whole system. I tried with the Symfony event system (by creating a custom event), but events seem to be dispatched and captured only within the same session (i.e., the same logged user). In other words, if an action performed by a user triggers an event, it is not captured by other users and therefore a message is not sent to the browser via SSE.
Any suggestion?
Thank you

How to asynchronously push data to reactJs from Spring api web service to update a progress Bar?

I have an application which allow an user to send a lot of SMS to his contacts (like thousands).
Obviously that tasks can take a lot of time to complete.
So the idea is to display a progress bar on the client side, to indicate the user how many messages have been sent so far.
The back end of my app is a restful spring webservice.
The front end is done with ReactJS and Redux.
The question is:
Is it technically possible from the back end to periodically push data to the client, to update the progress bar, with the amount of messages already sent.
First question regarding the back end architecture:
I've seen that using JAX-RS 2 with spring, I can make asynchronous call in the back end, to execute other tasks(like querying the DB to see the messages already sent) while the other process is sending all messages. Am i looking in the right direction here ?
Second question regarding front end :
So far I use thunk functions for my requests(post/get) to the server, which returns a json response, and it works well. But in this case, the back end would periodically push data to the client side, until the main task is completed, so I don't understand how would that work out exactly ?
I guess I'm not gonna be able to achieve that using the same request ? Should I look at other technologies to achieve that ?
Please let me know, if the description of my problem is not clear.
Cheers
There are two options: you can keep track of the task on the backend and have an API endpoint to check the status and poll it every x seconds.
The other option would be to use sockets, the frontend client would listen for an event and update display onEvent. The backend would be responsible for emitting events.

Automatically detect changes in a database using Ajax

I want to make an online DB based chat application. My problem is to detect changes in the DB, i.e. whenever a user sends a message to another user it should display to him. One approach is Ajax calls with setinterval but I want an instant solution. I do not want to keep the server busy with such Ajax calls.

How to sync my backbone.js app with the changes on server?

I have an app in backbone.js. The user can add items to the app which are added at the backend server and the collection is refreshed and the user sees the added data. How can I sync the app with any changes in the backend. Suppose if multiple users add the data at the same time each one should see the changes.
There are options like ajax polling where I can refresh the model after a certain time period but I hate to use it.
Can you suggest any event driven method where any change in the backend is reflected immediately in the frontend of my app.
You can add a kind of refresh event on your app, which will refresh every few minutes. Something like evernote desktop application does. In that event you would add collection sync event.
You can also use something like
collection.sync(method, collection, [options])
http://backbonejs.org/#Sync
This doesn't answer your question though, and I'm not sure how you would push changes from server to client.

Resources