Does anyone know a way to detect only 1 single instance of a web application is running in Chrome? I'm currently using a flag using localStorage but when stuff like browser crashing, that flag is still there so the next time I try to access the app it will give me an error because it thinks another instance is already running.
-
If you don't mind "overwriting" old instance with new instance, you can create a new "app id" on app initial, overwrite it into localStorage, and check this flag from time to time. If it doesn't fit the current app id, quit. – Passerby Apr 03 '13 at 05:00
-
Did you try Session Cookies? – sagibb Apr 07 '13 at 08:57
2 Answers
A few thoughts:
Overwrite old instance with new instance.
Upon app initial, create a new "app id", overwrite it into localStorage (regardless of whether it exists or not);
During app life time, check this flag now and then. If it's changed to another value, that means this instance should be "overwritten", so quit.
Use timestamp as flag.
During app life time, update the current timestamp into the flag in a fixed interval (e.g. 1s, 10s, etc.);
Upon app initial, compare current timestamp with this flag:
If it's "short enough", you can assume that the app is currently running, and refuse to continue;
If it's "long enough", that means the last instance exited unexpectedly, and you can "safely" run this new instance, and maybe provide some helpful info.

- 9,715
- 2
- 33
- 50
-
Both are great suggestion. I guess what I really wanted is a way to prevent a user from logging in to application twice at the same time. This needs to happen at the login so it should lock down from logging in. – user322731 Apr 06 '13 at 19:37
-
@user322731 I added a little explanation on the second way. If your app uses cloud, maybe you can also handle that on server side. – Passerby Apr 07 '13 at 08:31
As per my comment about Session Cookies, as far as I know Chrome maintains the same session for all tabs (per process), so it might be an ideal solution for you, especially since you want/need to clear the session once Chrome is shut down.
Here are some other related Stack Overflow threads that have the opposite issue:
How Come Closing A Tab Doesn't Close A Session Cookie?
How to differ sessions in browser-tabs?
You can still keep the persistent values (cross session values) in regular cookies/local storage.
-
tutipute. This is exactly what I ended up with. Was over engineering stuff trying to use localStorage but setting session cookie is all that was needed. – user322731 Apr 07 '13 at 16:10
-