Keep Channels Asynchronous

Learn how to use asynchronous channels.

We'll cover the following...

Asynchronous channels

Elixir is a parallel execution machine. Each Channel can leverage the principles of OTP design to execute work in parallel with other Channels since the BEAM executes multiple processes at once. Every message processed by a Channel, whether incoming or outgoing, must go through the Channel process to execute. This can stop working well if we’re not careful about how our Channel is designed. This is easiest to see when we have an example of the problem in front of us.

We’ll leverage our existing StatsChannel to see the effect of process slowness. Let’s add a new message handler that responds very slowly.

Press + to interact
# hello_sockets/lib/hello_sockets_web/channels/stats_channel.ex
def handle_in("slow_ping", _payload, socket) do
Process.sleep(3_000)
{:reply, {:ok, %{ping: "pong"}}, socket}
end

We have copied our existing ping handler but have ensured that every request takes a full three seconds to complete. We ...