mortenlund

mortenlund

LiveView - Live Component - send_update with socket as argument

Hi!

Suggest to add the ability to do use the components socket as input into Phoenix.LiveView.send_update/3 function to make it easier to send an update to self.

Using @myself as an argument is good, but think it would be easier to grasp when just using the current socket.
I think the cid is present in the socket and the module can be fetched using __MODULE__ is guess? :slight_smile:

Example:

@impl true
def update(%{title: title}, socket) do
  updated_socket = socket
  |> assign(:title, title)

  {:ok, updated_socket}
end

@impl true
def handle_event("update_title", %{"title" => title} = _params, socket) do
  updated_socket = socket
  |> send_update(title: title)

  {:noreply, updated_socket}
end

Most Liked

steffend

steffend

Phoenix Core Team

You can always write a helper function and import that:

def send_update(%Phoenix.LiveView.Socket{assigns: %{myself: cid}}, assigns) do
  Phoenix.LiveView.send_update(cid, assigns)
end

Your example is also a bit contrived, since you could just assign the value directly. Also note that send_update is asynchronous. It sends a message to the LiveView process, which invokes the update callback as soon as it handles the message. If your code relies on some data wrangling update/2 does, you can just directly call update instead:

@impl true
def handle_event("update_title", %{"title" => title} = _params, socket) do
  {:ok, updated_socket} = update([title: title], socket)

  {:noreply, updated_socket}
end

Last Post!

mortenlund

mortenlund

Hi, yes I can write such a function, and I have :slight_smile:
Just thought it would be a nice addition to the standard library :slight_smile:

And yes, you are right, my example was not very good, and after your example I am not so sure why I did not think of that :stuck_out_tongue:

Where Next?

Popular in Proposals: Ideas Top

hst337
Elixir compiler and language specification Purpose of the proposal Elixir language is in mature state and no breaking or heavy changes ar...
New
dkuku
This is a proposal to make the map key mismatch errors a bit better: Every time I have a typo It’s very challenging for me even when I u...
New
markevans
Hi! I feel like Phoenix is slightly missing a trick when it comes to front-end Javascript libraries like React, Svelte, etc. I feel tha...
New
zachdaniel
One thing I find is that I typically “start” with a LiveView even for static pages (in cases where I know I’m likely to add interactivity...
New
dogweather
Hi Phoenix community! First off, huge thanks for an amazing framework - Phoenix has been a joy to work with. I have a small UX suggestio...
New
Matsa59
Lets start by a story, I work with a team on a pretty big CMS with lots of modules. We split the cms using umbrella apps. So we have comp...
New
engineeringdept
In 2026 double submit/session tokens are no longer necessary to prevent against CSRF attacks. Instead, we can use the Sec-Fetch-Site head...
New

Other popular topics Top

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
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

We're in Beta

About us Mission Statement