axelson

axelson

Scenic Core Team

I’m struggling with how to sustainably organize a Phoenix Channel. Even with the concept/design of “Phoenix Is Not Your Application” I find my channels growing very large as the number of potential messages sent to or from a channel increases.

I’ve sometimes wished for a “routing” mechanism similar to HTTP but specific to a channel. So that certain prefixes of incoming channel messages could be handled in a consistent way. Alternatively I may want to just create more channels to spread out the messages, but then the clients need to join multiple channels when they connect to the socket. Which isn’t a huge burden (and is fine performance-wise since subscriptions are cheap), but feels like unneeded complexity.

How have others tackled this issue? Or is it a non-issue?

Showing Posts 1 to 4

chrismccord

chrismccord

Creator of Phoenix

Can you share your current channel code? it would be easier to say more if we have a real example to discuss. Thanks!

gon782

gon782

Personally I actually find that big channels/gen_servers aren’t much of an issue as most of the logic is descriptive enough for a quick search to jump straight to where you want to go and with something like the structure view in IntelliJ IDEA it becomes even easier.

If you need to offload some of the logic to somewhere else you could just make modules for the different message types you have in your channels, though, and essentially build module based routing into the channel. I don’t think there’s any shame in having you check the type of an incoming message and routing it to a module that really only contains some handle_that_type_of_message functions, all relating to that area (or context, if you will).

I used to do this slavishly for things like gen_servers even because I thought that it was horrible how big they could get, but I realized that everything is so self-contained and neat in a gen_server that I wasn’t accomplishing that much anyway. It’s either that or Stockholm Syndrome.

yurko

yurko

With full blown WebSocket APIs I also dislike them getting too big so I split them into sub-channels with a tiny bit of pattern matching, this way I can have the handlers separate (they still don’t do anything but delegate to context services), that keeps the files reasonably small.

Here is an example of a channel that delegates to subtopics:

alias App.Service.Channel.{Faq, Learning, Search} # etc

  # join clauses
  
  def handle_in("faq:" <> msg, payload, socket), do: Faq.process_message(msg, payload, socket)

  def handle_in("learning:" <> msg, payload, socket), do: Learning.process_message(msg, payload, socket)

  def handle_in("search:" <> msg, payload, socket), do: Search.process_message(msg, payload, socket)

 # etc

Frontend can then send a message such as search:find with some payload, the channel will process it and call process_message in Search service with find message.

axelson

axelson OP

Scenic Core Team

For anyone stumbling on this post now I’d recommend trying out this library that is made to modularize a Phoenix Channel:

https://github.com/doorgan/channel_handler

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews