hubertlepicki

hubertlepicki

Server-side equivalent to pushEvent/pushEventTo

I wonder if anyone figured out the way to have equivalent of pushEventTo (JavaScript interoperability — Phoenix LiveView v1.2.5) but executed from server side, without the need to go through the client/hook to send events to either parent components.

My use case is that I am building set of components in Surface UI and I am passing them target as a property, and the target is either being one of the parent componets or or nil in case of LiveViews that I want to notify when internal state of the component changes.

This is perfectly doable and works really well when the events are dispatched from the client-side, either with phx-target attribute or with pushEventTo/pushEvent when used from hook, but sometimes I’d like to process the event before sending it in some way in the component.

I can currently “solve” this by pushing the event back to the custom Hook I have on my component, and then dispatching it to destination with pushEventTo/pushEvent but this is less than ideal as there’s an extra round trip from browser to the component with the original event, then I pre-process the event in the component, and send it back to the Hook to be dispatched to the target.

I could also “solve” it by using send_update and send with combination of handle_info, handle_event and update functions depending on how the same event gets sent to the parent entity. But this is kinda ugly and redundant and entirely breaks my clean idea of passing down target to child components.

Any ideas if server-side pushEvent / pushEventTo is possible?

Most Liked

josevalim

josevalim

Creator of Elixir

It is not possible right now but it has been a requested feature, so I assume it will land at some point. The most immediate question I have is what happens when the handle_event has a reply for a server initiated event, since I assume most would want the events to be non-blocking.

trisolaran

trisolaran

It seems to me that many event handlers in the Phoenix/GenServer world are, by design, meant to handle only one specific kind of message. handle_event for LiveView events coming from the client, handle_in for client-side events coming from a channel, handle_cast for GenServer cast messages, and so on. Maybe the problem here is trying to shoehorn the idea of a “generalized event handler” into this architecture. Perhaps a better approach is to see all these event handlers as mere “adapters” for different event sources whose job is solely to convert the event and pass it on to a common event handling function.

On the other hand, I totally see how having to “send” a message to a component via send_update for a lack of a better way to do it server-side, is a stretch and points at something missing in the architecture.

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
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

We're in Beta

About us Mission Statement