简体   繁体   中英

How can I send an event to my browser JavaScript from the server-side of my app?

I have a situation where a user will be going through a series of form pages that will take them to a point where there will be a prolonged wait. The wait could be as long as 5 minutes. Once the waiting is over I want to move the user to the next page.

I'm wondering what the best way to listen for the signal from the app that it's time to move on. I considering using a WebSocket, but it seems like overkill for this. I considered opening an Ajax call using keep alive in the header response. I also considering using Ajax to poll every so often.

I'd love advice on what the cleanest approach is.

First off, you should not have that prolonged wait. Background this process on the server. Make the user do something else while that process happens. You can have them navigate away, tell them something's processing, we'll get back to you.

While you do that, each page in your site will have a listener that awaits a signal from the server. WebSockets, as you said is overkill. You can use Server-Sent Events instead. It's a "one-way socket" from server to browser.

Note that it isn't supported in IE (I wouldn't wonder) but there are polyfills .

I have also heard friends use Firebase for this as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM