bianchidotdev

bianchidotdev

Liveview: is_connected macro?

I came across a blog post that implemented a function guard for a liveview socket connection’s presence. It seems like a really ergonomic way to separate out mount functions for connected sockets and disconnected sockets, and I was surprised I wasn’t able to find any existing proposals or suggestions to implement connected? as a macro (very possibly my search fu failing me).

Is there any appetite in implementing this function guard in core liveview? Are there reasons it’s not implemented as a guard yet.

Code referenced in the blog post:

defguard is_connected?(socket) when socket.transport_pid != nil

Enabling:

def mount(params, session, socket) when is_connected?(socket) do
  # Connected mount
end

def mount(params, session, socket) do
  # Disconnected mount
end

Most Liked

al2o3cr

al2o3cr

One reason this may not exist already - the map.field notation wasn’t supported in guards until this PR, released in 1.11:

https://github.com/elixir-lang/elixir/pull/9711

Phoenix 1.6 only requires Elixir 1.9+, so Phoenix 1.7 is the earliest version this guard could have been included in.

JohnnyCurran

JohnnyCurran

Glad you liked the article! I have been thinking about making a PR to add it to LiveView. I wanted to get some feedback on it first. I suppose this thread might serve that purpose

I haven’t tested it, but I imagine the map.field syntax could be worked around by matching on the fields in the struct:

defguard connected?(%Socket{transport: transport}) when transport != nil

But that’s just a guess. I wrote that in the forum post editor

codeanpeace

codeanpeace

Gave it a whirl and it doesn’t look like you can pattern match when defining guards:

** (ArgumentError) invalid syntax in defguard is_connected?(%Socket{transport_pid: transport_pid})

Where Next?

Popular in Proposals: Ideas Top

skyqrose
Proposal: I wish the socket connect/3 callback could allow returning a reply to the frontend, which would then be visible in phoenix.js ...
New
woylie
We are seeing a lot of warning logs like this: navigate event to "https://someurl" failed because you are redirecting across live_sessio...
New
mythicalprogrammer
Hello, since the 1.8rc0 was out, DaisyUI was noted to have the benefit of light and dark mode. From the elixir subreddit, it seem a few ...
New
bianchidotdev
I came across a blog post that implemented a function guard for a liveview socket connection’s presence. It seems like a really ergonomic...
New
superchris
Hello! After seeing a couple of posts from Chris and Jose about supporting web components in LiveView, I thought it would be great to ini...
New
Oliver
One common problem we face in constructing lists is that there is (AFAIK) no support for conditionally inserting members into list declar...
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
dimitarvp
To @jonatanklosko and @the-mikedavis: I see that there is a Rust crate at crates.io: Rust Package Registry but it is pointing at https:/...
New
c4710n
This is an idea for improving the workflow of iex -S mix phx.server The Problem and The Solution in use When running multiple Phoenix en...
New
munksgaard
I’m copying and pasting this issue from @Terbium-135 here verbatim, because I would be interested in such a feature as well, and because ...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
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 39297 209
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

We're in Beta

About us Mission Statement