简体   繁体   中英

JS listen for a change in db / cookies / localhost files and immediately react

I want to logout a user edited his cookies so their new values are not match to his user info collected in database, like login token.

As same as this, I want to call http post function to server to fetch a new data as soon as new data was inserted.

I have tried setTimeout but after 20 minutes (approximately) the app crashed as it timed out or increased the resources requirements from a given device. I have to mention that I need to modify the changes in DOM as quick as possible, think like of a chat, we do not want to let the user wait for the fresh new message.

How Facebook for example, handles such a thing without getting timed out? Please try to supply a solution based on JS (pure).

Thanks.

I cannot provide a pure JS solution to you, but I believe you are addressing this in a wrong way.

Facebook uses react for their front-end, so they can use component lifecycle to manage updates without reloading the whole page. I would recommend you using push services or maybe websockets to communicate with your backend in a "realtime" fashion. With web sockets you can inform your frontend about when a change has been made and work from there. If you are not familiar with them https://socket.io would be a nice place to start.

This should set you in the right direction.

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