KekKekington

KekKekington

I’m working with Channels and Endpoint to send messages between the channels and their topics. I recently had to add a new ‘inter-channel’ communication path to remove some data. Basically, an admin will send a message to its own admin channel to cancel the order from employee x, the handle_in will then do a broadcast to employee:x with the message “finish_order”, which already has its handle_in function since the employee can also finish the order itself.

Problem is that I can’t seem to be able to send the message to the employee channel:topic. I’ve even tried hardcoding the employee topic but it just never hits the channel. What feels even more strange is that I’ve been using Endpoint.broadcast! throughout the program and it’s been working perfectly. Employee can send broadcasts to Clients, Clients can send to Employee. Only Admin isn’t able to broadcast and I just ran out of things to try.

# Admin
def handle_in("remove_order_from_driver", %{"client_id" => client_id, "order_id" => order_id, "employee_id" => employee_id}, socket) do

    Logger.info("Admin is removing an order from #{driver_id}")

    Endpoint.broadcast!("employee:#{employee_id}" ,"finished_order", %{client_id: client_id, order_id: order_id})

    {:noreply, socket}

  end
# Employee
def handle_in("finished_order", %{"client_id" => client_id, "order_id" => order_id_client},socket) do
# Logic to end the order, which is already being used by the employee frontend.
{:noreply, socket}

Showing Posts 1 to 6

outlog

outlog

think you are mixing/mismatching string/atom keys in your maps.. (“client_id” => value VS client_id: value etc)

so
Endpoint.broadcast!("employee:#{employee_id}" ,"finished_order", %{client_id: client_id, order_id: order_id})

will never be matched to
def handle_in("finished_order", %{"client_id" => client_id, "order_id" => order_id_client},socket) do

so use one or the other..

KekKekington

KekKekington OP

Thanks for replying. One of the first permutations of code that I tried actually had string keys but I changed it since elsewhere in my (apparently) working code I’m doing it that way. As in atom keys being sent to string key handlers. Now I’m left wondering if other parts of the code are simply not working but it certainly doesn’t seem that way…

Regardless I just substituted this particular instance so they both are string keys and I’m getting the same result. Admin channel does receive the message and shows the log I placed but the Driver channel handler is just not responding…

Is there a way to debug MyApp.Endpoint.broadcast so I can see what’s going on?

outlog

outlog

bit rusty on the broadcast usage actually..

from a quick look in my own code, Endpoint.broadcast! goes straight to the clients (usually js).. so that is probably why everything else is working just fine..

if you want to intercept the message, you have to use an “intercept” :grinning:
eg add this to the channel code


intercept(["finished_order"])

and then it’s handle_out instead of handle_in.. see: Phoenix.Channel — Phoenix v1.8.8

eg:

intercept ["new_msg"]

def handle_out("new_msg", payload, socket) do
  push(socket, "new_msg", Map.merge(payload,
    is_editable: User.can_edit_message?(socket.assigns[:user], payload)
  ))
  {:noreply, socket}
end

handle_out/3 callbacks must return one of:

{:noreply, Socket.t} |
{:noreply, Socket.t, timeout | :hibernate} |
{:stop, reason :: term, Socket.t}
outlog

outlog

but it seems like you are coupling code and violating seperation of concerns with your code..

can’t you have the logic separated or in it’s own module.. and the js client just receives updates/changes on the orders?

KekKekington

KekKekington OP

Employee channel is mostly sending messages to Client channel directly, I think I made a post about it when I was first working on it. It might be slightly broken it’d be pretty evident if it wasn’t working, since updates to clients depend on those messages being passed correctly. I’m still going to double check just in case.

I’m going to read the intercept docs, that might be useful. I was also thinking about passing regular messages and do a handle_info but I’m not certain how to obtain the PID of a specific channel.

You’re also right, the code really could use a refactoring but for now I need to solve the issue and then I can focus on fixing my own mess :sweat_smile:

KekKekington

KekKekington OP

I was trying to avoid having to release an update to the JS client but it seems like it’ll be much worse in the end. Based on all that @outlog mentioned, I’m broadcasting the message to the employee topic:subtopic, receiving that on the frontend and having it send its own “finish order” message. I’ve yet to try this but from everything I’ve gathered, it should work and I’ll avoid a mess in my Elixir code.

— 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
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
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
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
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
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
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews