From 8fc85bab9807d308ea077dad66564f1dd74cfbf2 Mon Sep 17 00:00:00 2001 From: Francis Jervis Date: Mon, 1 Sep 2025 15:45:47 -0300 Subject: [PATCH 1/5] add on_socket_connect documentation --- api-reference/lifecycle-hooks/on_socket_connect | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 api-reference/lifecycle-hooks/on_socket_connect diff --git a/api-reference/lifecycle-hooks/on_socket_connect b/api-reference/lifecycle-hooks/on_socket_connect new file mode 100644 index 0000000..67c12ea --- /dev/null +++ b/api-reference/lifecycle-hooks/on_socket_connect @@ -0,0 +1,17 @@ +--- +title: "on_socket_connect" +--- + +Hook to react to a new websocket connection to the front end. Note: this event should be triggered on all connection/reconnection events, including after client-side (eg browser moved to background on mobile), server-side (eg timeout) or connectivity issues. + +## Usage + +```python +from io import BytesIO +import chainlit as cl + + +@cl.on_audio_chunk +async def on_audio_chunk(chunk: cl.InputAudioChunk): + pass +``` From d6e04691ce9e795b7a5ae48759028c0aa56fffa5 Mon Sep 17 00:00:00 2001 From: Francis Jervis Date: Mon, 1 Sep 2025 15:51:27 -0300 Subject: [PATCH 2/5] Enhance documentation for on_socket_connect with example --- .../lifecycle-hooks/on_socket_connect | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/api-reference/lifecycle-hooks/on_socket_connect b/api-reference/lifecycle-hooks/on_socket_connect index 67c12ea..03af33f 100644 --- a/api-reference/lifecycle-hooks/on_socket_connect +++ b/api-reference/lifecycle-hooks/on_socket_connect @@ -2,16 +2,27 @@ title: "on_socket_connect" --- -Hook to react to a new websocket connection to the front end. Note: this event should be triggered on all connection/reconnection events, including after client-side (eg browser moved to background on mobile), server-side (eg timeout) or connectivity issues. +Hook to react to a new websocket connection to the front end. Note: this event should be triggered on all connection/reconnection events, including after client-side (eg browser moved to background on mobile), server-side (eg timeout) or connectivity issues. This event does not depend on authentication or data persistence - you can use the Chainlit session id to restore the chat state if required. ## Usage ```python -from io import BytesIO import chainlit as cl - -@cl.on_audio_chunk -async def on_audio_chunk(chunk: cl.InputAudioChunk): - pass +@cl.on_socket_connect +async def on_socket_connect: + print("Websocket connection established") + wsgi_env = cl.context.session.environ + # print(f"WSGI Environment: {wsgi_env}") + cookies = wsgi_env.get("HTTP_COOKIE", "No cookies found") + print(f"Cookies: {cookies}") + # Parse the cookie string to extract X-Chainlit-Session-id + session_id = "unknown" + if cookies != "No cookies found": + cookie_parts = [part.strip().split("=") for part in cookies.split(";")] + session_cookie = next((p[1] for p in cookie_parts if len(p) == 2 and p[0] == "X-Chainlit-Session-id"), None) + if session_cookie: + session_id = session_cookie + cl.user_session.set("session_id", session_id) + print(f"X-Chainlit-Session-id: {session_id}") ``` From a4940e0f518d30fff3cf6a269a982eec1fb99f8e Mon Sep 17 00:00:00 2001 From: Francis Jervis Date: Mon, 1 Sep 2025 15:53:12 -0300 Subject: [PATCH 3/5] Rename on_socket_connect to on_socket_connect.mdx --- .../lifecycle-hooks/{on_socket_connect => on_socket_connect.mdx} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename api-reference/lifecycle-hooks/{on_socket_connect => on_socket_connect.mdx} (100%) diff --git a/api-reference/lifecycle-hooks/on_socket_connect b/api-reference/lifecycle-hooks/on_socket_connect.mdx similarity index 100% rename from api-reference/lifecycle-hooks/on_socket_connect rename to api-reference/lifecycle-hooks/on_socket_connect.mdx From 434fedf908ab6741b849a23c33238816e160af29 Mon Sep 17 00:00:00 2001 From: Francis Jervis Date: Mon, 1 Sep 2025 15:57:45 -0300 Subject: [PATCH 4/5] Add documentation for on_socket_disconnect hook This document describes the on_socket_disconnect lifecycle hook, detailing its purpose and providing usage examples. --- .../lifecycle-hooks/on_socket_disconnect.mdx | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 api-reference/lifecycle-hooks/on_socket_disconnect.mdx diff --git a/api-reference/lifecycle-hooks/on_socket_disconnect.mdx b/api-reference/lifecycle-hooks/on_socket_disconnect.mdx new file mode 100644 index 0000000..e9fcdf3 --- /dev/null +++ b/api-reference/lifecycle-hooks/on_socket_disconnect.mdx @@ -0,0 +1,21 @@ +--- +title: "on_socket_disconnect" +--- + +Hook to react to websocket disconnection. Note: this event should be triggered on all disconnection events, including after client-side (eg browser moved to background on mobile), server-side (eg timeout) or connectivity issues. Unlike `on_chat_end` it is not called in response to user-initiated changes in chat context, eg clicking "New Chat." The session id should be available in subsequent reconnection events - see `on_socket_connect` for how to retrieve it. + +## Usage + +```python +import chainlit as cl + +@cl.on_socket_disconnect +async def on_socket_disconnect(): + print("WebSocket connection closed") + print(f"Context available: {hasattr(cl, 'context')}") + if hasattr(cl, "context"): + print(f"Session ID: {getattr(cl.context, 'session', None) and cl.context.session.id}") + print(f"Session type: {type(getattr(cl.context, 'session', None)).__name__ if hasattr(cl.context, 'session') else 'No session'}") + else: + print("No context available in on_socket_disconnect") +``` From 4d583189988cd54d95f54d7ecc469c10b1d9d658 Mon Sep 17 00:00:00 2001 From: Francis Jervis Date: Mon, 1 Sep 2025 22:45:34 -0300 Subject: [PATCH 5/5] Update description for on_chat_start hook --- api-reference/lifecycle-hooks/on-chat-start.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api-reference/lifecycle-hooks/on-chat-start.mdx b/api-reference/lifecycle-hooks/on-chat-start.mdx index f1e6434..d6ad916 100644 --- a/api-reference/lifecycle-hooks/on-chat-start.mdx +++ b/api-reference/lifecycle-hooks/on-chat-start.mdx @@ -2,7 +2,7 @@ title: "on_chat_start" --- -Hook to react to the user websocket connection event. +Hook to react to the initial user websocket connection event. Note: this callback will only fire on the initial connection for a user's session - use `on_socket_connect` to react to the connection being reestablished. ## Usage