Quick XPages Utility: Keep Alive and Alert
Tue Aug 30 15:17:40 EDT 2016
For one of my projects recently, I put together a small custom control that I sure wish I had thought to do years ago: a "keep alive and alert" control. For a long time now, the Extension Library has had a "keepAlive" control, which keeps a page session (and, usually, authentication) alive while the user has the browser window open, avoiding the otherwise-common issue of someone sitting on a page "too long" and having it break underfoot. However, that doesn't cover the edge cases: the user putting their computer to sleep and waking it with the same page open, the server rebooting, a server cluster failover, expiring SSO token, or so forth. In those cases, the problem will fall more or less "silently" to the JavaScript console, and the page will just be mysteriously unresponsive.
This control has a similar starting point, where it will ping the server periodically (every 10 seconds in this case), but will also display a Bootstrap modal alert when things go awry. It's not too picky about the cause of the problem: since usually the only practical solution is to reload the page, it just says that and leaves it there. This could also be redone to be more efficient like the ExtLib one (which would avoid the page recomputation inherent in the partial refresh), and it may not cover the case of authorization expiring in an app that allows anonymous access, but it should do the job nicely in the normal case:
<?xml version="1.0" encoding="UTF-8"?> <xp:view xmlns:xp="http://www.ibm.com/xsp/core"> <xp:div id="keepAliveAndAlert" style="display: none"></xp:div> <xp:scriptBlock><xp:this.value><![CDATA[ window.__keepAliveAndAlert = setInterval(function() { XSP.partialRefreshGet("#{id:keepAliveAndAlert}", { onComplete: function() { // Good! }, onError: function() { jQuery(dojo.byId("#{id:keepAliveAndAlertError}")).modal({ backdrop: 'static' }); clearInterval(window.__keepAliveAndAlert); window.__keepAliveAndAlert = null; } }); }, 10 * 1000); ]]></xp:this.value></xp:scriptBlock> <xp:div style="display: none" id="keepAliveAndAlertError" styleClass="modal fade error-modal" role="dialog"> <div class='modal-dialog'><div class='modal-content'> <div class='modal-header'><h4 class='modal-title'>Page Session Expired</h4></div> <div class='modal-body'> <p>Your page session has expired.</p> <p>Please <a href="javascript:location.reload()">reload</a> the page to continue working.</p> </div> </div></div> </xp:div> </xp:view>