-
Notifications
You must be signed in to change notification settings - Fork 962
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 2-way messaging to/from iframes through window.postMessage (#1469)
Co-authored-by: mauanga <[email protected]> Co-authored-by: Mathijs de Bruin <[email protected]>
- Loading branch information
1 parent
ff26451
commit a5612aa
Showing
11 changed files
with
154 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import chainlit as cl | ||
|
||
|
||
@cl.on_window_message | ||
async def window_message(message: str): | ||
if message.startswith("Client: "): | ||
await cl.send_window_message("Server: World") | ||
|
||
|
||
@cl.on_message | ||
async def message(message: str): | ||
await cl.Message(content="ok").send() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Chainlit iframe</title> | ||
</head> | ||
<body> | ||
<h1>Chainlit iframe</h1> | ||
<iframe src="http://127.0.0.1:8000/" id="the-frame" data-cy="the-frame" width="100%" height="500px"></iframe> | ||
<div id="message">No message received</div> | ||
<script> | ||
window.addEventListener('message', function(event) { | ||
if (event.data.startsWith("Server: ")) { | ||
document.getElementById('message').innerText = event.data; | ||
} | ||
}); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { runTestServer } from '../../support/testUtils'; | ||
|
||
const getIframeWindow = () => { | ||
return cy | ||
.get('iframe[data-cy="the-frame"]') | ||
.its('0.contentWindow') | ||
.should('exist'); | ||
}; | ||
|
||
describe('Window Message', () => { | ||
before(() => { | ||
runTestServer(); | ||
}); | ||
|
||
it('should be able to send and receive window messages', () => { | ||
cy.visit('/public/iframe.html'); | ||
|
||
cy.get('div#message').should('contain', 'No message received'); | ||
|
||
getIframeWindow().then((win) => { | ||
cy.wait(1000).then(() => { | ||
win.postMessage('Client: Hello', '*'); | ||
}); | ||
}); | ||
|
||
cy.get('div#message').should('contain', 'Server: World'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters