Maintaining Online User Count Socket.io NodeJS - javascript

I'm trying to keep track of all the users logged in to my socket.io / nodejs app.
When someone logs in I simply do:
sessionobj[result[0].username] = sessionId;
This also works fine if someone opens the site in multiple tabs, as they will still have the same username, and will only be added once to the sessionobj.
The problem occurs when someone closes one of the tabs. Ideally, I want the user to be "logged out" when he has no tabs open, however, I'm not sure how to accomplish this. For example, if the users has 2 tabs open and I add the line:
delete sessionobj[socket.handshake.session.username];
to the socket.on('disconnect' event, the user will be deleted when he closes one of the tabs, even if the other tab still remains open. Is there any way to keep track of this accurately?

Just store information about the current number of tabs somewhere, e.g. in the localstorage. Whenever a tab is opened you add one and when a tab is closed you substract one from said number.
Thus you know how many windows are open and can simply not call your disconnect event when there are more tabs of your app around.

Related

implement a modal window, which still lets the user navigate on the page

I am currently adding a live chat to my webapp. I got it already finished - now to the problem:
I want the live-chat to "stick" to the down right edge part of my webapplication. I want it to still persist, when there is a route-change for example - so the user can still navigate on the page and still do whatever he wants.
I googled already for modal windows, that "follow", but I cant find any results.
How can I accomplish that behaviour of my live chat? (modal window dialog?)
What do I need to search for?

Javascript bring a tab to the front

I am writing a Javascript which runs in the browser using Tampermonkey.
The logic in the script is as follows.
1. Do some checks.
2. If a certain condition is met, then trigger an alert box in the browser.
Currently the alert box is shown in the browser tab, but amidst multiple tabs, the user does not notice the alert box till they navigate to the tab.
I am trying to figure out one or both of the following.
1. Bring the tab to the front i.e. focus on that tab.
2. Do something more aggressive than the alert box to catch user's attention. I thought of things such as playing sounds etc but it requires on user's speakers being on etc.
Any ideas on how to make this work would be greatly appreciated.
Looks like this cannot be done and the easiest option would be to write some data to the browser's local storage and then open a new window, fetch the data and display it.

How to make different tabs with same user but different windows with different users using localStorage?

I'm developing chat application and I managed to set same user across different opened tabs but across different windows I want to allow different users.
client.js:
window.addEventListener('load', function () {
if (typeof(localStorage.localStor) === "undefined") {
console.log("is null");
localStorage.setItem('localStor', {});
//user displayed for registering
} else {
console.log("exists");
console.log(typeof(localStorage.localStor)); //prints 'string'
//display as already registered user
}
}, false);
So when user registers in a tab in one window ,after that, all the tabs opened in this window are for the same user ,but when opening new window the user registered in the first one will appear as registered also here.
I've read that by using session ,new tabs have a session one for each,so I don't think it is preferable.
How can I achieve that in JavaScript without JQuery? Thanks in advance.
As far as javascript is concerned, all tabs are separate windows. Your browser just displays tabs together for convenience. You can either use localStorage to keep a user persistent through all windows/tabs or sessionStorage to keep a user persistent only in one specific window/tab.

Callback for jQuery window.focus doesn't trigger after some time

I'm facing this weird issue in Chrome. This is the simple code I tried (in my web application):
jQuery(window).focus(function() {
console.log("Window is now active");
});
This works fine for some time.
SCENARIO
I opened 2 tabs with different links (Say gmail, stackoverflow, etc). Then I opened my web application's home page in 1 tab and opened some other page of same application in another tab (Total 4 tabs).
Now, when I switched between my application's tabs, it showed me the logger for some time. But, when I clicked other tab(say stackoverflow tab) and returned to my web app tab, then loggers between my web app tabs stopped.
No matter how many time I switch between my application's tabs, it never showed logger until refresh.
I'm aware that this may be a rare scenario, but I dearly need a solution from someone who might have faced this problem as well.
Any suggestion about any plugin to be used is also welcome
EDIT
Well now I got a hint of why this problem is happening, but haven't found any solution yet.
After following THIS example, I found that in chrome, you need to click inside the window after which focus event is fired, but not just on clicking the tab.

Closing a tab without closing the window

Hi I have set up a task in my task scheduler to login to my UTM by opening a firefox window at regular intervals. When the relevant URL is loaded a auto-login GM script works on it and logs me in to UTM.
I would like to close that tab after the login is done. If I use
window.close();
It works fine but if no other tab is opened at the time, it simply closes the window.
Without going into details of UTM I would like the following -
A GM hack which closes the tab only if other tabs are already open. If it is the last tab of the window, then it should just replace it with a blank tab (so that window is not closed).
Probably GM cannot obtain any info about the other tabs, but is there any hack ?
As far as I'm aware this isn't possible, if nothing else then for security reasons: you don't spam javascript from your video streaming tab collecting all your data and submitting forms on your behalf.
Here's a thread with a solution that worked for IE7 that you may be able to manipulate but as I said it's not likely.
If you decide to have ago, don't think about identifying the number of tabs, it won't happen, instead focus on determining whether or not there are multiple tabs.
The linked example uses if(clientY == 0) to determine whether or not the browser is closing. This would be a good starting point.
Again, for security reasons, you won't be able to find out information about open tabs.

Categories