Session variables and multiple browser tabs

108 views
Skip to first unread message

Jim S

unread,
Feb 18, 2016, 10:56:21 PM2/18/16
to web2py-users
Hi

Is there a way to keep independent session variables between two open browser tabs?

Example

1.  write a session variable in tab 2
2.  switch to tab 1 and execute the same page setting the session variable to a different value
3.  go back to tab 2 and request page that reads the session variable and gets the value set from tab 1

Is there any way to force each tab to have it's own session variables?

-Jim

Niphlod

unread,
Feb 19, 2016, 3:45:05 AM2/19/16
to web2py-users
every browser considers tabs as belonging to the same session in regards to cookies. there's no way to separate those using web2py code.
you can leverage sessionstorage in javascript that keeps values alive - and compartimentalized - to a single living tab.

Jim S

unread,
Feb 19, 2016, 8:20:14 AM2/19/16
to web2py-users
Thanks for the reply.  However, I'm unclear on how I might use it effectively.

My scenario in my app:

I have a python object that I pass around to different pages.  At the end of each of my controller methods I do the following:

wo = Workorder(auth.user.id, workorder_id)


#  save to the session
session
.workorder = cPickle.dumps(wo.to_dict())


Then, at the beginning of each controller method I do:

wo = Workorder(auth.user.id)
wo.from_dict(cPickle.loads(session.workorder))


So, with my limited javascript knowledge, I don't see how I'd be able to convert this to sessionstorage.  And actually, I don't know if what I'm doing with pickling/unpickling dicts is a smart thing to do.  If anyone has any pointers/comments, I'd appreciate it.  However, it seems as though I may need to rethink my strategy.

Thanks for taking a look.

-Jim

Niphlod

unread,
Feb 19, 2016, 10:17:11 AM2/19/16
to web2py-users
the less session you use, the more your app is scalable. that being said, it's pretty clear that you can't leverage in python something that is only available on javascript.
maybe but just maybe you can POST back to web2py a variable that is stored on the sessionstorage (i.e. the current date if sessionstorage.currentdate does not exist yet) and use it to namespace your session key names (hence, values) amongst tabs.

Anthony

unread,
Feb 19, 2016, 10:33:13 AM2/19/16
to web2py-users
I think the idea is you would not use the web2py server-side session but would instead handle things on the browser side.

An alternative would be to generate some kind of unique key for each page/tab to use as a namespace within the server-side session, and then either put that key in the URL (as an arg or in the query string), or handle page updates exclusively via Ajax and pass the key with each Ajax request (of course, when making post requests, you can pass the key via the posted data, but that doesn't help for non-post requests).

Also, note that the session is pickled automatically, so there is no need to pickle a dictionary before adding it to the session.

Anthony

Jim Steil

unread,
Feb 19, 2016, 11:18:30 AM2/19/16
to web...@googlegroups.com
I was thinking of that but then couldn't figure out how I'd handle clicking the back button in the browser.  

Doing a little research we've found a plugin for Firefox called MultiFox that does what I want.  We are also going to play later today with the multi-personality features of chrome and firefox to see if that is an alternative for us.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/aEUuKWvkJAY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages