LiveViewPortal - embed LiveViews in other websites

LiveViewPortal is a small library that allows third-party sites to embed LiveView apps.
The library is distributed as an NPM package. Only a small part of the LiveView JS code is modified.

What problem does this solve?

From the LiveView repo:

Phoenix LiveView enables rich, real-time user experiences with server-rendered HTML.

This matches Doofinder’s use case for our ecommerce products except for one point: rendering LiveViews from third party pages. We needed to embed LiveView for everybody that wanted to give their users rich and real-time experiences.

We have been using a modified version of LiveView with this purpose for a long time. We wanted to extend this to other projects and decided to make it open source. Our goal is to make this feature a part of LiveView, and we are working on merging this to LiveView. In the mean time, you can use this library to try it out.

If you want to know more about the technical details, you can check out the repo and our blog post. Also, if you have any questions, I’ll be more than happy to answer here!

31 Likes

Amazing job, @victor23k!
Excited to see what use cases the community comes up with!

1 Like

Great job! I wanted to do this for sometime but never managed to do it myself! I had a similar one done for a comment server alternative for Disqus that I did for an organisation using LiveView but with a lot of hacks. However something like what you have with LVPortal would make it easier to implement without the ugly hacks. Congrats on making this available for everyone!

2 Likes

Thanks!! This has some minimal hacks, as it modifies javascript LiveView code. Hopefully we can find a way to make this available in LiveView.

The comment solution is definitely a great idea, I’m sure you must have been happy once you managed to implement that with all the workarounds that you mention :smile:.

Woah, thanks for releasing this, it looks awesome. :purple_heart:

I will try it soon. I can think of many uses for embedding live counters and reports from Rocket Validator into our users’ sites. :rocket:

2 Likes

Amazing work! This will be very useful with the stuff we are planning to do in the company I work!

Here are some questions, do you already have an github issue in the LV or Phoenix github repo asking to merge this so I can follow it?

I’m not sure if this is possible with shadow dom or not, but how do you handle navigate and patch? Is it possible to change the page URL?

1 Like

Very nice! Several potential upstream contributions!
Thanks for sharing :purple_heart:

Is it viable to use LiveViewPortal and regular LiveView (for other pages in the same app) simultaneously?

2 Likes

Hi @rhcarvalho

Yes It is possible, that’s one of our use cases where we embed a chat in Doofinder admin panel (two distinct phoenix apps)

2 Likes

@sezaru thanks!

Not yet, but I’ll post the PR in this thread so you and others can follow the progress.

I have not explored this yet, but it is definitely something to experiment with. It can be tricky to modify the URL as the host page would probably also do things with the URL. Also you would probably want to reuse the same websocket instead of navigating to a new LiveView and having to mount again. I think live_session could be of use here.

1 Like

Hey @rhcarvalho, thanks. As @sonic182 said, you can embed a LiveViewPortal inside a LiveView hosted in a different Phoenix app.

Also, you can serve, from the same Phoenix app, both regular LiveViews and LiveViewPortals. For that, you can define separate socket endpoints and router pipelines.

1 Like

Excellent questions from @sezaru.

Since we touched the topic of navigation, I’d like to ask how is LiveViewPortal different from LiveView when it comes to state management options? IIUC URL params are not available, are they?

How is the redeployment/reconnection experience? In particularly, with regards to state preservation, e.g. form inputs.

Wow, amazing, that’s exactly what we could use right now. We have an e-commerce webpage and are use Liveview for a kind of designer for placing logo’s on the product, but are using the iframe technology. If this really works on our webpage with LiveviewPortal, that would be super fantastic, thank’s a lot, really a lot…

1 Like

I’d like to ask how is LiveViewPortal different from LiveView when it comes to state management options? IIUC URL params are not available, are they?

Params are not available by default. However, in one of our LiveView apps embedded in third-party sites, we do have URL params — thanks to a custom implementation. That app still uses our old modified LiveView setup, without Portal. When we migrate it to LiveViewPortal, we’ll evaluate whether to keep it as a custom solution per app or extract something reusable for all portal-based apps.

Awesome! I would have been happy to pay a bounty on getting this one :slightly_smiling_face:

https://elixirforum.com/t/re-bounty-for-feature-to-mount-liveview-into-a-web-component/65423

Link is broken or private, didn’t work for me.

Hey @victor23k any news regarding pushing this to LV?

We are waiting for feedback on this before opening a PR in the LiveView repo. Have you tried the library? Do you have any suggestions or have you encountered any shortcomings?