Based on cookies, for sites with a common superdomain
I want to share how this was done for one project. The difficulty of using other well-known methods was that the project was not tied to a single domain name, but was localized on a grid of subdomains. That is, the project sites were located on the third level domains. This circumstance caused some inconvenience due to the Same Origin Police.
Developers who have not yet encountered similar problems will probably be interesting. Imagine a web service (for example, a social network) that should inform the user online about various “new messages”, “new friends” and the like. Web service, tracking online index, any, Dow Jones, or God forbid, the most MICEX.
From the pages of such projects, constantly, with a certain periodicity, requests are sent to the server to obtain relevant information. Moreover, some services establish a permanent connection with the client in order to keep this client informed of their dark server business in real time.
The sore spot of such systems begins, oddly enough - with browsers. From browsers that support opening pages "in a new tab." And it is even more painful when in the project, for various kinds of information, there is a separate type of pages. (but this is a completely different story - the history of design).
In fact, it’s really inconvenient for a user to constantly click on links from the current page in order to see “well, what grade did I get there”.
And the user first opens one tab, for navigation, then the second tab, for tracking ratings, then the third - for tracking new messages. The number of simultaneously open tabs is directly dependent on the size of the user's desire to “control everything”.
How many servers do you need to quickly process a request from each client from each such open page? Especially, if the requests are asynchronous and with some periodicity.
Both growing projects and dinosaurs swallowing the web are faced with the need to solve such problems.
To solve this problem, apply at least two options:
- Increase server power, thereby reducing the processing time of the client's request. Few can afford it.
- Or “reason” requests from the client himself, which from any glance seems to be more optimal and more expedient.
Local Storage (and its modifications), which is gaining popularity, did not fit for the simple reason that it was also imposed on the rule of domain restriction (same origin policy). In addition, it was necessary to monitor the status of active (open) tabs, which requires an additional share of resources. And, everything else, it works, so far, not in all browsers. For the same reasons, Flash also did not have much hope, as well as other tasty features. A good algorithm, by the way, was described here .
In general, after many days of beating his head against the wall and gnawing the edges of office desks, I remembered an ancient and native browser-based mechanism - Cookies.
Now, after a lot of experiments, I think that there is a place where fantasies can roam to optimize this mechanism.
The method was developed for the messenger, so a cookie was also installed for the messenger itself, which indicated in which tab the messenger was opened. When the interval triggered, the tab checked, including which tab the messenger was open in and whether it was open at all. And she closed it in her house, in case she wasn’t the “mistress” of the messenger.
Synchronization of the state of the messenger was carried out using Local Storage. In case of its absence, there was nothing to do, how to make a request to the server to get the last state.
I'll start with the shortcomings:
Advantages:
For more detailed acquaintance with the described method, you can download an archive copy .
PS: Archived draft version.
PPS: UPD 10/23/2014 version 2.0 with blackjack ...
Source: https://habr.com/ru/post/156855/
All Articles