derek-zhou

derek-zhou

How do I send a unsolicited and generic event from the javascript side to the root LiveView, to be handled in handle_event/3? Do I have to use a Hook object attached to something, or is there a simpler way?

I was trying to do:

liveSocket.root.pushEvent(...)

But don’t know what to put in the parenthesis.

Also, How do I do the reverse, sending an generic event from the liveview to window or window.document object?

Showing Posts 1 to 6

mindok

mindok

Hi @derek-zhou - have you read through the JS interop docs at JavaScript interoperability — Phoenix LiveView v1.2.5?

You will need hooks attached to an element in your liveview. The docs referenced above give an example of a hook setting up a listener for window events.

derek-zhou

derek-zhou OP

I did. I can attach a hook to an element and achieve back and forth event. However, it feels awkward, because what I am doing has nothing to do with the element, or UI at all; I was just passing some data back and forth.

I was hoping since the Phoenix.LiveView is built on top of a Phoenix.Channel, I can use it to pass my sideband signals using the existing LiveView, without causing any harm to the LiveView.

Ljzn

Ljzn

I’m using Vue as UI render in my LiveView project. It will call Vue.createApp to render whole page every time when the html elements mounted or updated. (It’s not the best way but fast enough and simple).

function renderApp() {
    let fields = {}
    // Vue read data from html element
    document.querySelectorAll('.cg-v-model').forEach((p) => {
        Object.assign(fields, JSON.parse(p.getAttribute('vdatajson')))
    })
    const component = {
        data() {
            return fields
        },
        methods: {
            push: (event, params) => {
                this.pushEvent(event, params)
            }
        }
    }
    const app = Vue.createApp(component)
    app.mount('#' + this.el.id)
}

Hooks.createApp = {
    mounted() {
        renderApp.call(this)
    },
    updated() {
        renderApp.call(this)
    }
}

Then I can push event directly from vue components:

<el-button type="primary" @click="push('reset', resetForm.pass)">RESET</el-button>
LostKobrakai

LostKobrakai

That‘s correct, but Liveview uses a custom handler on the client side. It uses the same infrastructure, but is more specialized than plain channels. So it doesn‘t necessarily do what plain channels do anymore.

derek-zhou

derek-zhou OP

With a hook I can push_event from the server side and catch in the client side, and the reverse is also true. So I can achieve my goal with just a little detour. However, If I have a push_event followed by a push_redirect, the push_redirect will cull the earlier push_event, as documented in the doc at the very end. So my question is:

Do we have a guaranty that push_event themselves are handled in order? How about the other direction, from client to the server?

To get around the problem of push_redirect culling push_event, I am doing 2 push_events, the latter will trigger something like this:

        this.handleEvent("redirect", ({href}) => {
            let url = new URL(href, location.href);
            liveSocket.historyRedirect(url.toString(), "push");
        });

So far with limited testing it seems to work well.

Miserlou

Miserlou

I’ve packaged a little utility library for interacting with the client JS context from LV - GitHub - Miserlou/live_json: LiveJSON - LiveView for JSON · GitHub

To send data to the server:

import { sendData } from 'live_json';
sendData('your_handler', your_data); 

To send data to the client:

socket
|> LiveJson.assign("foo", "bar")
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews