-
Notifications
You must be signed in to change notification settings - Fork 963
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
support multiple livesockets #3564
Open
gfrancischelli
wants to merge
5
commits into
phoenixframework:main
Choose a base branch
from
gfrancischelli:support-multiple-livesockets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ab8e7f0
support multiple livesockets
gfrancischelli 616bec4
fix livesocket test that always pass
gfrancischelli 04ed4eb
fix main view selection for dead views
gfrancischelli 9f7150f
test liveSocket.owner with rootViewSelector
gfrancischelli 6302343
rename viewSelector option
gfrancischelli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,12 @@ except for the following LiveView specific options: | |
* `uploaders` – a reference to a user-defined uploaders namespace, containing | ||
client callbacks for client-side direct-to-cloud uploads. See the | ||
[External uploads guide](external-uploads.md) for details. | ||
* `rootViewSelector` - the optional CSS selector to scope which root LiveViews to connect. | ||
Useful when running multiple liveSockets, each connected to a different application. | ||
See the [Connecting multiple livesockets](#connecting-multiple-livesockets) | ||
section below for details. | ||
|
||
a CSS selector to scope which | ||
|
||
## Debugging client events | ||
|
||
|
@@ -313,3 +319,42 @@ Hooks.Chart = { | |
``` | ||
*Note*: In case a LiveView pushes events and renders content, `handleEvent` callbacks are invoked after the page is updated. Therefore, if the LiveView redirects at the same time it pushes events, callbacks won't be invoked on the old page's elements. Callbacks would be invoked on the redirected page's newly mounted hook elements. | ||
### Connecting multiple liveSockets | ||
LiveView allows connecting more than one `liveSocket`, each targeting different HTML nodes. This is useful to | ||
isolate the development cycle of a subset of the user interface. This means a different Phoenix application hosted | ||
in a different domain, can fully support an embedded LiveView. Think of it as Nested LiveViews, but instead of | ||
process-level isolation, it is a service-level isolation. | ||
Annotate your root views with a unique HTML attribute or class: | ||
```elixir | ||
# Main application serving a regular LiveView | ||
use GreatProductWeb.LiveView, container: {:div, "data-app": "root"} | ||
|
||
# Cats application, which will serve the cats component | ||
use CatsWeb.LiveView, container: {:div, "data-app": "cats"} | ||
``` | ||
And initialise the liveSockets: | ||
```javascript | ||
# Fetch the disconnected render | ||
let disconnectedCatsHTML = await fetch("https://cats.io/live", { credentials: 'include' }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would that be a controller doing a |
||
.then((response) => response.text()) | ||
.catch((error) => console.error(error)); | ||
|
||
# Append it to HTML | ||
document.queryElementById("#cats-slot").innerHTML = disconnectedCatsHTML | ||
|
||
|
||
# Connect main liveSocket | ||
let liveSocket = new LiveSocket("https://root.io/live", Socket, {rootViewSelector: "[data-app='root']"}) | ||
liveSocket.connect() | ||
|
||
# Connect the cats liveSocket | ||
let liveSocketCats = new LiveSocket("https://cats.io/live", Socket, {rootViewSelector: "[data-app='cats']"}) | ||
liveSocketCats.connect() | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I wouldn't document this as prominently. I think the documentation for the liveSocket option is enough.