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

fix: pass headers when connecting sockets #1575

Merged
merged 10 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions backend/chainlit/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ def emit_call_fn(event: Literal["ask", "call_fn"], data, timeout):
async def connection_successful(sid):
context = init_ws_context(sid)

if context.session.restored:
return

await context.emitter.task_end()
await context.emitter.clear("clear_ask")
await context.emitter.clear("clear_call_fn")

if context.session.restored:
return

if context.session.thread_id_to_resume and config.code.on_chat_resume:
thread = await resume_thread(context.session)
if thread:
Expand Down Expand Up @@ -312,17 +312,13 @@ async def message(sid, payload: MessagePayload):
async def window_message(sid, data):
"""Handle a message send by the host window."""
session = WebsocketSession.require(sid)
context = init_ws_context(session)

await context.emitter.task_start()
init_ws_context(session)

if config.code.on_window_message:
try:
await config.code.on_window_message(data)
except asyncio.CancelledError:
pass
finally:
await context.emitter.task_end()


@sio.on("audio_start")
Expand Down
2 changes: 1 addition & 1 deletion cypress/e2e/copilot/.chainlit/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ session_timeout = 3600
cache = false

# Authorized origins
allow_origins = ["*"]
allow_origins = ["http://127.0.0.1:8000"]
Copy link
Collaborator

Choose a reason for hiding this comment

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

I love finally seeing this! :)

We should document that on a server users need to configure the origin.


# Follow symlink for asset mount (see https://github.com/Chainlit/chainlit/issues/317)
# follow_symlink = false
Expand Down
2 changes: 2 additions & 0 deletions libs/react-client/src/useChatSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ const useChatSession = () => {

const socket = io(uri, {
path,
withCredentials: true,
transports: ['websocket'],

Choose a reason for hiding this comment

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

This change works for our use-case, but I believe adding the transports option here means that Socket.IO will not fall back to long-polling if a WS connection can't be established. There are probably still some load balancers/browsers out there that can't handle web-sockets properly. It might be helpful for these options to become chainlit initialization options that the user can select?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

For instance the gcp load balancer only works with this. We can make it configurable I think.

extraHeaders: {
Authorization: accessToken || '',
'X-Chainlit-Client-Type': client.type,
Expand Down
Loading