Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Session appears to not shared between HTTP endpoints and websockets #601

Open
lianggecm opened this issue Dec 11, 2024 · 0 comments
Open
Labels
bug Something isn't working

Comments

@lianggecm
Copy link

If you're reporting a bug, please continue with this template.

Describe the bug
Session sets in HTTP end points and websockets have different values.

Minimal Reproducible Example
Provide a minimal code snippet that reproduces the issue. This is crucial for us to understand and fix the bug quickly.

from fasthtml.common import *
#from fh_frankenui.core import *
import sys
import uuid

app, rt = fast_app(
    pico=True,
    hdrs=(
        Script(src="https://unpkg.com/htmx.org/dist/ext/ws.js"),  # Add WebSocket extension
        ),
)
@rt("/")
async def home(session):
    old_session_id = session.get('session_id', None)
    # Get a new session_id
    session['session_id'] = str(uuid.uuid4())
    return Div(P(f"Original session_id: {old_session_id}"),
               P(f"New session_id created in Main: {session['session_id']}"),
               Button("Settings", hx_get="/settings", hx_target="#settings-container"),Div(id="settings-container"),
               Form(Input(name="msg"), Button("Send to ws"), hx_ext="ws",
            ws_connect="/ws-test",ws_send=True,), Div(id="chat-container"))


@rt('/settings')
def get(session):
    session_id_in = session.get('session_id', None)
    session['session_id'] = str(uuid.uuid4())

    return Div(P(f"Session_id sent to settings: {session_id_in}"), P(f"New session_id created in settings: {session.get('session_id', None)}"), id="settings-container")
    
@app.ws('/ws-test')
async def ws(msg:str, send, session):
    session_id_in = session.get('session_id', None)
    session['session_id'] = str(uuid.uuid4())
    await send(Div(P(f"Session_id sent to ws: {session_id_in}"),P(f"New session_id set in ws: {session.get('session_id', None)}"), id="chat-container"))

Expected behavior
In this example I fist set session_id in "/". Then change the session_id in "/settings". Afterwards, I send the session to "/ws" and change the values of session_id. The Session_id sent to the "/ws" websocket should be the session_id value set in the "/settings" endpoint. However, as seen in the picture here, the session_id sent to the "/ws" is still the same as the value that was set in "/".

image

Further clicks on the two buttons would show that session_id sets in either endpoints doesn't exchange/share at all.

Environment Information
Please provide the following version information:

  • fastlite version: 0.0.13
  • fastcore version: 1.7.19
  • fasthtml version: 0.10.1

Confirmation
Please confirm the following:

  • [ X] I have read the FAQ (https://docs.fastht.ml/explains/faq.html)
  • [ X] I have provided a minimal reproducible example
  • [ X] I have included the versions of fastlite, fastcore, and fasthtml
  • [ X] I understand that this is a volunteer open source project with no commercial support.

Additional context
Add any other context about the problem here.

Screenshots
If applicable, add screenshots to help explain your problem.

@lianggecm lianggecm added the bug Something isn't working label Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant