Home  >  Article  >  Backend Development  >  javascript - How to get updated data from the database when using Server-Sent Events?

javascript - How to get updated data from the database when using Server-Sent Events?

WBOY
WBOYOriginal
2016-08-04 09:20:441108browse

I think of two ways:
1. Get the data in the most recent period, and return the data if there is any; but this time period is not easy to set. Chrome can use the last 3 seconds, but Firefox uses the last 5 seconds, so take 3 seconds If so, Firefox often cannot retrieve it; if it takes 5 seconds, Chrome will retrieve duplicate data.

2. The second idea is to mark whether it is read in the database, but because different browsers and different terminals get data out of sync, one of them will mark it as read after getting the data, and other browsers will not be able to get it. Data.

So, how should we deal with it?

Reply content:

I think of two ways:
1. Get the data in the most recent period, and return the data if there is any; but this time period is not easy to set. Chrome can use the last 3 seconds, but Firefox uses the last 5 seconds, so take 3 seconds If so, Firefox often cannot retrieve it; if it takes 5 seconds, Chrome will retrieve duplicate data.

2. The second idea is to mark whether it is read in the database, but because different browsers and different terminals get data out of sync, one of them will mark it as read after getting the data, and other browsers will not be able to get it. Data.

So, how should we deal with it?

The second idea is possible, but it needs to be optimized. You can store the update time of the data in the table memory and the update time of the latest data obtained from the server in the terminal. If the terminal time is smaller than the table time, it means there is new data to be pushed.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn