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

add mapping between client_token and socket id #3388

Merged
merged 1 commit into from
May 28, 2024

Conversation

Lendemor
Copy link
Collaborator

@Lendemor Lendemor commented May 27, 2024

Keep an internal mapping between client token and sid.

Can be used by end user to terminate some logic/tasks if the websocket associated with a given state is disconnected.

Will also be helpful later for some internal use.

Can be used like this to terminate a background task for example :

class State(rx.State):
    @rx.background
    async def loop_function(self):
        while True:
            if self.router.session.client_token not in app.event_namespace.token_to_sid:
                break
            await asyncio.sleep(2)


@rx.page(on_load=State.loop_function)
def index():
    return rx.text("Hello")

Possible improvement : Provide a "cleaner" access to this mapping for end user?

Close #3387

@Lendemor Lendemor requested a review from ElijahAhianyo May 27, 2024 11:42
@Lendemor Lendemor mentioned this pull request May 27, 2024
Copy link
Contributor

@picklelo picklelo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - is there a concrete use case for this yet? Or just in case we need it later?

@Lendemor
Copy link
Collaborator Author

LGTM - is there a concrete use case for this yet? Or just in case we need it later?

Without this users can not terminate logic in background tasks, so I'd say yes.

but there will also be other potential future use too.

@picklelo picklelo merged commit 33f71c6 into main May 28, 2024
47 checks passed
benedikt-bartscher pushed a commit to benedikt-bartscher/reflex that referenced this pull request Jun 3, 2024
@masenf masenf deleted the lendemor/ws_token_mapping branch December 12, 2024 07:20
@5quinque
Copy link

How can you use this method if you have your app initialized in one module, and your State class defined in another?
If I want the background task to run when the page loads, there isn't a way without causing a circular import?

For example,

from app_name.state import State

app = rx.App()

app.add_page(component=chat_page(), route="/", on_load=State.background_event)
from app_name.app_name import app

class State(rx.State):
    @rx.event(background=True)
    async def background_event(self):
        while True:
            if self.router.session.client_token not in app.event_namespace.token_to_sid:
                ...

@benedikt-bartscher
Copy link
Contributor

@5quinque maybe you can use get_app inside the handler?

@5quinque
Copy link

@5quinque maybe you can use get_app inside the handler?

Didn't know about this method, but it works perfectly, thanks :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can I detect if a client is disconnected within a State's method?
5 participants