I have a request come into my through the :api pipeline. In order to process response it needs the socket inside the liveview to process a reply. If the liveView is closed, or for many other reasons I don’t get the LV is disconnected, the reply just hangs and fails. When I started using liveView I didn’t realize it had these limitations, i.e. needed to be physically open.
How can I check if the liveView is connected from outside (from a controller)? If it is disconnected I can do one thing, like send a server response saying temp offline, etc.
Process.send & Kernel.send both need the pid and I don’t know how to get this from outside.
pubsub just does nothing if the liveView is not open - no error, no response, nothing.
Otherwise, when I process a request over the :api line, how can I access the state of my current sessions? It needs this info. I wish it didn’t, but it does
So far I’ve found no way to access this, aside from Pubsub messaging if the LV happens to be running when the pubsub is sent.
Not sure if I understand your actual use case and end goal, but you could cache the LiveView pid and tie it to a user id for example using Elixir’s built-in Registry as described in this blog post on Using Channels with LiveView for Better UX. Depending on what you’re trying to achieve, it’s worth keeping in mind that there could be multiple LiveView processes running for a given user e.g. nested LiveViews or multiple tabs/devices.
Agreed with @codeanpeace and it would be best if you could describe what you’re trying to do from a business logic standpoint. You shouldn’t ever rely on a LiveView process to process an external request. A LiveView process models one connection which, as you’re realizing, can die at any time.
Who is making the API requests? Are they being made by another part of your app or are they external?
Okay, just to give and update so U don’t feel like U wasted your time replying, b/c it did help.
The problem: I’m receiving a twilio message where the only unique identifier is the phone number. (If anyone is a twilio expert maybe then can offer more advice about how to match received messages to sent messages - this has been tough.)
Anyway, it’s possible that the system could find two users with the same phone number that are considered valid; valid in the system, not in reality. So essentially I am handling a rare edge case.
When this happened I thought to send a msg to the LV, alert the user that this occurred, and require a manual override where the user clicks on the correct one. I even thought (wrongly) that I could pause the HTTP response back to twilio until after the user like clicked the button, or whatever.
The Gist: I need to save these things to the DB and not just to memory. Then when the LV comes online it will pick-up whatever data, etc. While this seems obvious now, it somehow didn’t before. Not sure why. I got glamoured by all the magic maybe