mortenlund

mortenlund

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

Showing Posts 1 to 2

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
mortenlund

mortenlund OP

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:

— All posts loaded —

Where Next? Top

Trending in Proposals: Ideas Top

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews