clemensm

clemensm

Connect multiple instances of LiveView dynamically after page load

Hi there

I’m currently working on an application for personal knowlege managment. We’ve recently added a new feature where you can interactively layout your notes, and decided to go with LiveView for it, which has been working very nicely so far.

However we’ve now added an option to have multiple of those note layout views at the same time on your page, and the user can create them dynamically (the page is a SPA, and we’ve wrapped the code for handling the LiveView into a custom WebComponent).

The custom WebComponent looks roughly like this, and is activated by receiving some HTML via JSON from the server which is then appended to the server:

<div>
  <notes-view>
    <%= live_render .... %>
  </notes_view>
</div>
class NoteView extends HTMLElement {
  connectedCallback() {
     this.__liveSocket = new LiveSocket("/live/view-editor", Socket, { hooks: hooks });
     this.__liveSocket.connect();
  }
}

So everytime the user adds a <notes-view>, we connect a new LiveSocket. However by doing this, the previously connected LiveView reconnects on the new socket, and becomes basically unusable (it’s mount function is called again, and it becomes confused for phx events send from hooks).

Currently I’m a bit lost as how to solve this, I’ve already tried to only connecte one LiveSocket, but then the later LiveViews do not connect at all… Any pointers how this could be done? Thanks a lot already :slight_smile:

Marked As Solved

clemensm

clemensm

Ok, I’ve found the solution and my mistaken assumptions :wink:. I’m not sure if this is the idiomatic way to do this, but it does work at without problems:

  1. Do not open more then one LiveSocket (should have been obvious). Instead create a global LiveSocket and connect that one:

    const __liveSocket = new LiveSocket("/live/view-editor", Socket, { hooks: hooks });
    __liveSocket.connect();
    
  2. When the WebComponent connects, call some functions on the LiveView to join the views manually (This is the part I’m nor sure about. The functions do not really look like they are private, but I guess that could change at anytime):

    connectedCallback() {
        __liveSocket.joinRootViews();
        __liveSocket.detectMainView();
    }
    

    It might be possible to call liveSocket.joinView(...)directly, but I haven’t given that a try.

  3. Once your WebComponent disconnects, destroy the associated view (this will also destroy the process on the server for this view):

    disconnectedCallback() {
        __liveSocket.destroyViewByEl(yourViewElement);
    }
    

And that’s it

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52774 488
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29703 241
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43806 214
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement