-
Notifications
You must be signed in to change notification settings - Fork 903
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 window.postMessage support. #1162
base: main
Are you sure you want to change the base?
Conversation
Interesting feature! Code looks clean, on quick review. @mauanga Is there any chance you could add a test for this feature in https://github.com/Chainlit/chainlit/tree/main/cypress/e2e? |
@dokterbob Thanks for taking the time to review the PR. I’ve made a small update to include support for 2-way messaging, which now allows messages to also be sent from the backend to a listening host window. I'll start working on the cypress e2e test and will add it to the PR soon. |
@mauanga Just tested this PR and it's great! I would suggest initializing the context inside @sio.on("window_message")
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()
if config.code.on_window_message:
try:
await config.code.on_window_message(data)
except asyncio.CancelledError:
pass
finally:
await context.emitter.task_end() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing from BaseChainlitEmitter:
async def send_window_message(self, data: Any):
"""Stub method to send custom data to the host window."""
pass
Description
This update enhances the functionality of the Chainlit application when it is embedded within another web application via an iframe. By enabling message exchange between the parent and child windows, the application can now support extended features. Some examples of the use cases would be; allowing users to select datasets from their dashboard, supply saved prompts, and more.
Changes
message
events inAppWrapper.tsx
.window_message
event inuseChatInteract.ts
on_window_message
to__init__.py
config.py
window_message
insocket.py