chanon

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

Showing Posts 1 to 4

lud

lud

Hello,

I have the same problem. I use promises to call into the channel from different parts of my javascrtipt code :

function ppush (channel, event, payload) {
  return new Promise((resolve, reject) => {
    console.log("ppush '%s' payload:", event, payload)
    channel.push(event, payload)
      .receive('ok', resp => {
        console.log('received ok: ', resp)
        resolve(resp)
      })
      .receive('error', resp => {
        console.log('received error: ', resp)
        reject(resp)
      })
  })
}

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.onError but you have to handle all errors, not only from the current push.

idi527

idi527

:waving_hand:

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

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:

const errDispatcher = new EventEmitterFromSomeLib()

socket.onError(err => errDispatcher.emit('error', err))

function ppush(channel, event, payload) {
  return new Promise(function (resolve, reject){
    let unsubscribe
    function rejectUnsub(err) {
      unsubscribe()
      reject(err)
    }
    unsubscribe = errDispatcher.subscribe('error', rejectUnsub)
    channel.push(event, payload)
      .receive('ok', data => {unsubscribe(); resolve(data)})
      .receive('error', rejectUnsub)
      .receive('timeout', rejectUnsub)
  })
}

But I do not know if socket.onError does always mean that it’s disconnected or if a push could resolve anyway. Maybe a check to isConnected would be necessary with something like maybeRejectUnsubIfDisconnected along with rejectUnsub.

idi527

idi527

I think there is also channel.onError which is supposed to handle channel process dying and socket disconnecting. I’d guess socket.onError only handles the latter.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New

Other Trending Topics Top

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
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews