Next.js Discord

Discord Forum

instant updates from route handler to frontend

Unanswered
Elite posted this in #help-forum
Open in Discord
So I have a webhook server that lets me know when a PayPal invoice has been paid.

And I have a dynamic route (/transaction/{id})

I want to immediately change my frontend when this invoice gets paid on the correlating id.

Rn I have a use state that checks for transaction status changes in mongodb.

So since the webhook runs on a route handler, I’m able to update the transactions state in mongodb, but Im not able to let the frontend use effect know that it’s been updating.

I don’t really want to poll as I want this flow to be as instant as possible, the user pays invoice in PayPal, frontend changes.

How could I do this?

16 Replies

Interested in seeing what others propose as a solution. So messaging so I don’t lose it.
I mean like actual instant would have to have a web socket connection or something continuously polling, at least that’s what my mind goes to.
correct. real time communication requires real time technologies, so server-sent events or websockets.
Right you can make some intelligent decisions to trigger refreshes but there isn’t some magic solution. You could have it fetch when the tab becomes active again maybe? If you would assume that they would pay it and the tab would lose focus.
That’s just a hand wavey way to make it feel real time I have done before.
@joulev correct. real time communication requires real time technologies, so server-sent events or websockets.
How could I do server sent events or web sockets?

Would really love it to be instant. But if it isn’t possible ig I could poll
if you self-host: google is your friend
@joulev if on Vercel/Netlify/similar serverless platforms: no you can't.
so then i cannot do instant updates if im on vercel?
@joulev if on Vercel/Netlify/similar serverless platforms: no you can't.
saw this:
https://github.com/vercel/next.js/discussions/48427#discussioncomment-5624604

but confused as to how I could implement it in my route.
the webhook route handler is a POST. and I want it to let my frontend know when its received a valid req.
Yeah so that’s just streaming, what you are looking to achieve can be done through that, I don’t know the challenges involved in regards to hosting and stuff I self host everything I work on so I can’t offer insight there.
@Elite so then i cannot do instant updates if im on vercel?
no you can't, and that's a platform limitation, not a framework limitation.
Alr I’ll just long poll
Will seem instant
Make sure to setup hard limits on spending. Again I don’t know much about vercel but it sounds like that could rapidly get out of control.
Alr