Replies: 4 comments 6 replies
-
UpdateWhen running into this issue, I switched from global/shared pages (which nicegui calls auto-index) to local pages (which they call private). I think my issues had to do with leftover state outside page scope being carried over from one page to the next. Anyway I tested with query id as described below. Reloading page still wipes page data even when query id is the same. Preserving state across reloads / changes to client id needs more than just a url invariant. Still have to manually pack / unpack state with a mechanism that persists across reloads, like cookies or storage. There's still a use for query id parameters, so below post is still relevant. It just takes a bit more work. |
Beta Was this translation helpful? Give feedback.
-
Couldn't this user state use |
Beta Was this translation helpful? Give feedback.
-
Hu? So much text and hard to figure out the core. While reading the initial text I had the feeling that there is something strange in your assumptions drawn from some tests you made. Which you confirm with
If you use a page-generator, every visitor (speak every tab) becomes it's own context (stored in the |
Beta Was this translation helpful? Give feedback.
-
Thanks Natan, appreciate the suggestions. If I reach a point where I'm
stuck then I'll definitely post code. For now I just wanted to see if
others already solved the same general issues.
…On Sun, Nov 26, 2023 at 9:10 PM Natan Keddem ***@***.***> wrote:
My apologies, I misunderstood the scope of what you were trying to share
data between. If you think there is no "clean" way to write the type of
functionality you are trying to achieve I think a working example of this
"dirty" code might go a long way toward getting any sort of traction here.
It sounds like you don't like the syntax provided so perhaps there is some
way to make it better structured and documented.
—
Reply to this email directly, view it on GitHub
<#2082 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AIKNAODY72BBEGCIYIKXVGLYGOV2RAVCNFSM6AAAAAA72DICGWVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM3TMNZUG4ZDI>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Update
Parts of this post about client id may be somewhat inaccurate. See next post below.
Original post
I'm currently figuring out the best way to handle multiple clients to the same page from the same web browser with separate state. Nicegui doesn't do this by default. Since this is a general issue for many web apps, I'm posting my approach here. Maybe others will benefit or suggest an alternative.
Issue
I'm trying to match my app model with nicegui's architecture. Nicegui is different from typical client-side web apps where user data is remote but most app state is maintained in the client wepage. New tab = new client with separate state. User data is same, UI state is separate.
Nicegui doesn't distinguish multiple tabs to same page from same client (or maybe it does, but I haven't found it yet). The server doesn't support running several copies of the same page in parallel from one browser. This is something my users need, similar to opening multiple tabs with different google searches, or linkedin, or amazon, .... The way most web apps work.
Nicegui's server-side architecture plays a role in this approach, as does its async execution model. A forking or threaded server that spawned a new fork / thread for each connection would maintain separate state for each connected websocket by default. But nicegui runs everything in one process because async's all the rage somehow. And it treats every (browser, url) pair like one monolithic page for some reason. So we have to deal with those architectural decisions.
Solution
I'm figuring out how to distinguish multiple clients to same page from same browser. The little-documented client id
nicegui.Client.id
could work for this purpose. But I'm concerned about involuntary socket disconnections forcing a page reload, which happens regularly with nicegui doc site. This seems to generate a new client id, meaning user loses their current state and their progress if client id were used as session key.Cookies and local storage don't work because those are per browser. Each tab to my app accesses the same cookie and storage values. You could index those by client id, but when client id is regenerated on reload, how do you know which previous id to retrieve from? I don't think you can.
My current plan is to use query parameters:
Pros and Cons
This is actually more robust than current CGI app, which loses state when page is reloaded (but this rarely happens). Whether that's a good or bad thing depends on the users.
Copying and linking the url with query id is a no-no, as every visit to the link will see the same state. Bookmarking also preserves state (as long as server is not reloaded), which can be confusing. So users need to remove the id in these cases.
My app will always present naked urls to create a fresh page. But I can't control what users do when bookmarking or sharing links. We'll see how it goes, if support incidents rise or not.
Code sample
To illustrate, no promises this code runs as is.
Yes there's a chance of collisions in my query id. It's fine for my purposes. Adjust as needed.
Beta Was this translation helpful? Give feedback.
All reactions