How can I close a session from within the session?

1,549 views
Skip to first unread message

Norman Packard

unread,
May 22, 2015, 9:09:10 PM5/22/15
to shiny-...@googlegroups.com

Is there a way to close a session from within a session?

Like, effectively same as closing the window/tab (so that session$onSessionEnded() gets called, etc.).

I found a comment by Joe Cheng last March saying it might be nice to have a close() method for a session object, which sounds like what I want.  If it doesn't exist, is there a graceful workaround?

I have tried using stopApp(), but it is not very graceful, and shuts down all of shiny, not just the session.

I have also tried sendCustomMessage to a little java function that simply calls close() to close the current window:

Shiny.addCustomMessageHandler("logout",
  function() {
    close()
  }
);

This also isn't very graceful, and the window does not close  (it throws an error, greys out the window; same behavior as for stopApp()).

Any suggestions appreciated!

Joe Cheng

unread,
May 22, 2015, 9:45:27 PM5/22/15
to Norman Packard, shiny-...@googlegroups.com
I'm not 100% sure what behavior you're looking for, but session$close() will stop the session (which will result in grey out on the client). 

Sent from Outlook




--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/95502d21-ec16-4e76-a657-23c22a00d79c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Norman Packard

unread,
May 22, 2015, 9:58:58 PM5/22/15
to shiny-...@googlegroups.com
Thanks, Joe, for your quick response.

Ah.  so there is a session$close().

But... it is certainly not equivalent to closing the browser window/tab.  The window is grey, not vanished, and before the window is grey, an error is thrown.

My desired behavior is:
- client window disappears
- no error is thrown on the client (or at least no error notification window appears)
- onSessionEnded is called (umm, I guess this would be on the server...)




Joe Cheng

unread,
May 22, 2015, 10:25:15 PM5/22/15
to Norman Packard, shiny-...@googlegroups.com
As far as I know, the browser won't let you close the window or tab from JavaScript except in very specific circumstances (you are allowed to close windows you opened from JavaScript). 

Sent from Outlook




--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.

Norman Packard

unread,
May 26, 2015, 4:32:05 PM5/26/15
to shiny-...@googlegroups.com, nhpa...@gmail.com


On Friday, May 22, 2015 at 7:25:15 PM UTC-7, Joe Cheng [RStudio] wrote:
As far as I know, the browser won't let you close the window or tab from JavaScript except in very specific circumstances (you are allowed to close windows you opened from JavaScript). 


 Well... isn't the shiny window opened from JavaScript?  So why can't shiny give me a mechanism to close it (without throwing an error)?

I am essentially trying to implement a logout functionality (with associated cleanup implemented by onSessionEnded).  I can't be the only one wishing to do this.  It is so... standard.

Also, Shiny server pro has a timeout functionality.  However that closes a session down might be appropriate for my needs, if it implements onSessionEnded on the way.

Also shiny-io pages have a time-out that grays out the page without throwing an error.  If this session close implements a call to onSessionEnded, maybe that mechanism would work for me.

Joe Cheng

unread,
May 26, 2015, 5:06:38 PM5/26/15
to Norman Packard, shiny-...@googlegroups.com
The Shiny window is not opened from a JavaScript call. It's either from the user navigating to it (Shiny Server case) or the operating system invoking the browser (local R session case).

It is standard to log out, what's not standard is for the window/tab to be closed. Almost every website works this way--after you log out, it sends you to a different page, it doesn't close the window/tab.

No matter how the session ends, be it timeout, accidental disconnect, whatever, onSessionEnded will be called on the server side (unless the R process itself crashes, obviously then we don't get a chance to clean up). Keep in mind though, that when onSessionEnded is called, you can clean up anything on the server but it's too late to communicate with the client--onSessionEnded means the connection has already been broken.

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.

Norman Packard

unread,
May 26, 2015, 5:31:41 PM5/26/15
to shiny-...@googlegroups.com, nhpa...@gmail.com


On Tuesday, May 26, 2015 at 2:06:38 PM UTC-7, Joe Cheng [RStudio] wrote:


It is standard to log out, what's not standard is for the window/tab to be closed. Almost every website works this way--after you log out, it sends you to a different page, it doesn't close the window/tab.

No matter how the session ends, be it timeout, accidental disconnect, whatever, onSessionEnded will be called on the server side (unless the R process itself crashes, obviously then we don't get a chance to clean up). Keep in mind though, that when onSessionEnded is called, you can clean up anything on the server but it's too late to communicate with the client--onSessionEnded means the connection has already been broken.


Thanks for your patient education, Joe.

Sending my user to a 'logged out' page  instead of closing the window would be fine (presumably with an href).  Is onSessionEnded called in this case?

I get the point about cleanup happening only on the server.  This is reasonable...

Thanks again.


Reply all
Reply to author
Forward
0 new messages