Allow only one open browser window/tab per user/application

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
    <xp:eventHandler
        event="onClientLoad"
        submit="false">
        <xp:this.script><![CDATA[
            var secondsIamAliveInterval = 5;
            var tabId = sessionStorage.getItem("tabId");
            if (!tabId) {
                tabId = Math.random();
                sessionStorage.setItem("tabId", tabId);
            }
            function sendTabIdToServer() {
                XSP.partialRefreshGet("#{id:browserTabControl}", {
                    params: {'tabId': tabId}
                });
            }
            sendTabIdToServer();
            setInterval(function() {
                sendTabIdToServer();
            }, secondsIamAliveInterval * 1000);
        ]]></xp:this.script>
    </xp:eventHandler>
    <xp:panel id="browserTabControl">
        <xp:this.rendered><![CDATA[#{javascript:
            var secondsIgnoreOtherSession = 7;
            if (param.tabId) {
                var userName = session.getEffectiveUserName();
                var userData = applicationScope.get(userName);
                var now = new Date().getTime();
                if (userData) {
                    if (userData.tabId !== param.tabId) {
                        if (userData.time + secondsIgnoreOtherSession*1000 > now) {
                            context.redirectToPage("Error.xsp");
                        }
                    }
                } 
                applicationScope.put(userName, {tabId : param.tabId, time: now});
            }
            return true
        }]]></xp:this.rendered>
    </xp:panel>
</xp:view>





This snippet was developed for an answer to question "How to stop a user from opening a new browser session in XPages" on StackOverflow : http://stackoverflow.com/a/27427055/2065611

The snippet can be used as a Custom Control "UniqueBrowserTab" and be integrated into every XPages or e.g. into application layout's Custom Control with <xc:UniqueBrowserTab />.
It expects an additional XPage Error.xsp with an error message (like "you can't open a second browser window/tab for this application") which has not included the Custom Control "UniqueBrowserTab".

This will allow only one browser window/tab per database for a user across all browsers.
The active browser window/tab sends every 5 seconds an "I am alive". After 7 seconds inactivity an other window/tab is allowed to take over.
The snippet makes use of browsers sessionStorage which is a unique storage for every window/tab.

XPages
Knut Herrmann
January 3, 2015 6:50 AM
Rating
37

All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.



1 comment(s)Login first to comment...
Bill Fox
(at 13:24 on 04.03.2015)
This works really well with a browser, but causes the screen to flicker every time it does an "I am Alive" in the Notes client. I have set the run on server in the Notes Client but that does not appear to help. The screen flicker makes it almost unusable in the Notes Client.