I am displaying data from another application so when the new data added to that app i want my application get that data and update the UI with the latest data. I am using webhook to get the newly added data and then storing that on my application DB. But can’t show the updated data until reload.
Question posted in Javascript
A very good W3school tutorial can be found here.
A very good W3school tutorial can be found here.
2
Answers
For these kind of immediate changes, we usually do data pooling or in this case, web sockets. Consider using socket.io as an example but if you are in a serverless environment, like NextJS’s API routes, it’s not possible to use web sockets. You can use SaaS as Pusher to achieve what you are looking for.
Or, if you don’t mind, you can keep calling API after 1 minute:
Or more better query using TanStack Query:
These are just examples, configure as you are expecting.
You can run a polling logic inside a useEffect hook and update a state variable to save the state. Refer the example mentioned in the below post.
React hooks. Periodic run useEffect