Introduction
In such situations, you need to evaluate what the dangers are, how serious are those dangers, and what would be the minimal expectation that would be good-enough.
The problem-statement was that you want to save an array when the user logs out or when he or she navigates away. There are multiple topics we should cover and you can combine them according to your needs in order to get the best possible mix.
logout
This is the easiest part of the problem-space and I strongly suggest that you make this your first milestone. You will need to send your array to your server upon logout and then the server will save the array (or its changes) into your database.
So, you need to:
- be able to load the array from the database, pass it to the UI, and make it operational
- be able to modify the array each time when such a modification is needed
- be able to send the array to your server which will store it
Now, the load part should be pretty clear, it's likely already implemented. However, modification and sending needs a good plan already, because you do not want to implement the same thing twice.
You need to have a function that will run upon each modification and another one that will run upon each sending. And you will likely need to make the second one an AJAX request and trigger this AJAX request upon logout click and only log out when this save has been completed.
onbeforeunload
You said this is unreliable. So, let's see how is this unreliability manifesting: Reliability of onbeforeunload
Basically, under normal circumstances, it will run successfully, so if you embed the sending of your array into this, then it should be successfully sent. However, it's indeed possible that the browser or the OS would crash for some reason and you need to handle that as well.
sessionStorage
You can store the array in sessionStorage
and whenever the user reloads the page while being in sessionStorage
, then the array could be synced. But, of course, this comes with limitations, due to the fact that the user may use a different browser or maybe his session was destroyed. The latter problem may be handled by localStorage
as long as the data is not confidential, but localStorage
will not handle the former problem.
periodic saves
You can run setInterval
, which periodically would call your send function (as long as there was some change to save), and, if this runs every 5 minutes, then the maximum data loss is of 5 minutes, which only happens if the browser/OS/comp has crashed, so it would be a very rare situation with minimalized damage.
saving every change
You can also decide to send all changes when they happen to the server, which may create some extra burden on your server, but if it's manageable, then you have huge benefits.
Imagine the case when a user logs in with a second browser while still being logged in with the first one. It is highly desirable to make sure that the second browser will get whatever changes were made in the first browser, even if both sessions are still alive
WebSocket
You could create a WebSocket connection that maintains a duplex channel between the browser and the server, allowing you to sync browser and server instantly and that would allow you to create multiple sessions and each session would be maintained real-time, without having you, as a user to worry about data syncing.
Conclusion
onbeforeunload
is pretty much reliable, but in case of crashes, it will not work. There are work-arounds and maybe (probably) you want to save your data periodically, because in the case of crashes or other extreme scenarios, if there are no intermittent saves, the user may end up having to concede the loss of the work of multiple hours. So, save frequently, but pay attention to your server's limits.