chanon
I am using something like this to to handle Websocket requests:
def handle_in("new_msg", data, socket) do
# do something with data that might result in exceptions/exits/errors raised
{:reply, {:ok, output}, socket}
end
And in JavaScript side, something like this
channel.push("new_msg", data, 10000)
.receive("ok", (output) => console.log("created message", output) )
.receive("error", (reasons) => console.log("create failed", reasons) )
.receive("timeout", () => console.log("Networking issue...") )
What happens is, when an exception is raised it seems the whole channel process (?) goes down, and the client which is waiting for a response gets no response and just times out.
What I would like instead is to be able to return an error immediately with maybe “unexpected error” reason kind of like a http 500 error.
I don’t want to leave the client hanging and it isn’t a “Networking issue” so timeout isn’t appropriate result.
After some trial and error, I now have the following, which seems to work nicely:
def handle_in("new_msg", data, socket) do
try do
# do something with data that might result in exceptions/exits/errors raised
{:reply, {:ok, output}, socket}
catch
:exit, error ->
Logger.error(Exception.format_exit(reason))
{:reply, {:error, %{reason: "Unexpected Error"}}, socket}
end
end
But I am a bit unsure, as
- I think for http requests Phoenix handles this and automatically responds with 500 code? But for websocket requests I have to handle this myself?
- And also the Elixir tutorial seems to say that you wouldn’t normally need to use try/catch
- And would I also need a rescue?
EDIT: I am now doing it like this as all my messages need replies:
# try catch here
def handle_in(event, params, socket) do
try do
{:reply, handle(event, params, socket), socket}
catch
:exit, reason ->
Logger.info("responding with unexpected error")
Logger.error(Exception.format_exit(reason))
{:reply, {:error, %{reason: "Unexpected Error."}}, socket}
end
end
# handle each message type, no boilerplate
defp handle("new_msg", params, socket) do
# do stuff
{:ok, %{result: output}}
end
defp handle("another_msg", params, socket) do
# do stuff
{:ok, %{result: output}}
end
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New
Latest Phoenix Threads
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
lud
Hello,
I have the same problem. I use promises to call into the channel from different parts of my javascrtipt code :
But I have no idea how to catch exits from the channel. It would be very useful during development process.
Of course we could handle
channel.onErrorbut you have to handle all errors, not only from the current push.idi527
You can write a custom phoenix channel which would send an error message to the client on exception and keep working. It would probably be similar to the default channel implementation bot with plug’s error handler try/catch logic like here (I think).
Is it a good idea? ¯\(ツ)/¯
lud
I’d rather stick with a JS only solution as the default channel implementation is great but I believe it is not possible because of how websockets work : you can send multiple messages from the browser in a timespan an not be able to known wich one caused the server channel process to exit.
A satisfying solution would be to
.receive('error', fn)when the socket is disconnected while doing a push instead of only receiving a timeout, something like that:But I do not know if
socket.onErrordoes always mean that it’s disconnected or if a push could resolve anyway. Maybe a check toisConnectedwould be necessary with something likemaybeRejectUnsubIfDisconnectedalong withrejectUnsub.idi527
I think there is also
channel.onErrorwhich is supposed to handle channel process dying and socket disconnecting. I’d guesssocket.onErroronly handles the latter.