MissaouiChedy

MissaouiChedy

Why aren't phoenix channel instances supervised?

I was recently experimenting with the possibility of retaining a channel instance’s state (assigns) between crashes.

In my testing application, I am causing the channel instance to crash and I noticed that the instance is not automatically restarted. It seems that channel instances are not supervised.

Why is it the case? Do we rely on the channel’s client to reestablish a connection thus creating a new channel instance on the server side?

DETAILS
Here is the code for the channel and the test that I am using:

defmodule ChanState.KablamChannel do
  use ChanState.Web, :channel

  def join("kablam", payload, socket) do
      {:ok, assign(socket, :ns, [])}
  end

  def handle_in("add", %{"n" => n}, socket) do
    ns = socket.assigns[:ns]
    {:noreply, assign(socket, :ns, ns ++ [n])}
  end

  def handle_in("get", _, socket) do
    ns = socket.assigns[:ns]
    {:reply, {:ok, %{"ns" => ns}}, socket}
  end

  def handle_in("oopsy", _, socket) do
    a = 1 / 0
    {:noreply, socket}
  end
  
end

TEST:

defmodule ChanState.KablamChannelTest do
  use ChanState.ChannelCase

  alias ChanState.KablamChannel

  setup do
    {:ok, _, socket} =
      socket()
      |> subscribe_and_join(KablamChannel, "kablam")

    {:ok, socket: socket}
  end

  test "main", %{socket: socket} do
    Process.unlink(socket.channel_pid)

    push socket, "add", %{"n" => 2149}
    push socket, "add", %{"n" => 1234}
    
    ref = push socket, "get", %{}
    assert_reply ref, :ok, %{"ns" => [2149, 1234]}
    
    push socket, "oopsy", %{}

    ref = push socket, "get", %{}
    

    ## The following returns undefined
    IO.inspect(:erlang.process_info(socket.channel_pid))
    assert_reply ref, :ok, %{"ns" => []}, 5000
  end
  
end

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Supervision does not entail automatic restart. A process can be supervised with the :transient flag and will not be restarted if it crashes. Supervisors exist for more than restarting processes.

In this case restarting the process can’t be easily done because it is now out of sync with the client. The client needs to re-connect to the channel in order for it to be properly re-established.

Also Liked

NobbZ

NobbZ

It is very hard to supervise dynamic incarnations of processes, because the supervisor might restart processes that went down on purpose. Again not here have been experiments when genstage was developed.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48475 226
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43806 214
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New

We're in Beta

About us Mission Statement