LiveView unmount/terminate callback

Hi all, I’m wondering how one would do an unmount/terminate callback inside LiveView?

Right now I am simulating this with Phoenix Presence, but I think it’s a smelly solution and requires me to store data in Presence that is already in each User’s socket.

Instead of checking presence_diff to see if someone has left and then check their Presence data for something that I put there (which is already part of their socket), the unmount or After Leave for the LiveView would just take their socket and broadcast changes to the other users.

e.g. What I am doing is like a multi-user spreadsheet, where only one user can edit a cell of the spreadsheet at a time. So I need to unlock the cell if someone disconnects or leaves (closing the browser tab or disconnecting won’t call phx_blur).

I’m sure there are some other use cases, but generally I feel LiveView is lacking simple way to tap into the channel it’s already using. This post has some creative workarounds: Doing JS events in LiveView

But these are workarounds and are a bit convoluted. I would love to see Phoenix LiveView have a way to work with this out of the box.

1 Like