kklisura

kklisura

How to Intercept Incoming Events in Channels?

Is there any way to intercept incoming events (before they are being sent to handle_in/3 callback) in Channels. From the docs. I can see one can only intercept outgoing events.

If not, is there any specific reason why is this not supported?

Specifically, my use case: I’m trying to set the Sentry context when handling input messages, so I get additional information on the errors (event name, last message) if such error occurs in the handle_in/3 callback. Currently, I’m forced to write some prologue code in all handle_in/3 callbacks, which I would like to escape if I can.

Thanks

Marked As Solved

cevado

cevado

why don’t you just make the handle_in generic and create another function to deal with the message…

def handle_in(event, payload, socket) do
  SentryContext.withing_chanel9event(event, payload)
  process_incoming(event, payload, socket)
end

and then in the process_incoming/3 you do the specific stuff. you see that you can compose with the handle_in without the need to introduce more steps to the abstraction. if that is something that you want to use on all your channels you can even abstract it away.

my point on making sense is not that “it doesn’t have usage”, but that it doesn’t fit the way the framework designed the channels to work.

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

This reminds me a bit behind the motivating case for Phoenix LiveView hooks: Phoenix.LiveView — Phoenix LiveView v1.2.5

Channels / LiveView don’t have an equivalent to plug style middleware, where you can easily go to a channel or a live view and just say “oh also do X when you get a message”. LiveView handled this by adding hooks since it was a pretty common ask to deal with auth / telemetry / etc, but it doesn’t look like Channels have an equivalent yet. Probably the hooks design could also work here? That said, @kklisura’s solution is definitely the way to go for the moment.

cevado

cevado

because it doesn’t make sense to have a step before the handle in. the handle_in is the moment you gonna process it comming from the client. intercepting the outgoing events happens because phoenix abstract away the need to handle all outgoing messages.

handle_in - messages from client(FE) to the channel(BE).
handle_out - messages that gets to the channel(BE) through the system(other processes in the BE, usually through the pubsub) and needs to be sent to the client(FE).

phoenix automate that any message sent to the topic of a channel(the name of the channel is the topic) through the phoenix pubsub is something that should go to the client. so you only needs to implement the handle_out callback if the channel subscribe to other topics, or if you wanna do something with the message before sending it to the client(like filtering messages).

Last Post!

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

This reminds me a bit behind the motivating case for Phoenix LiveView hooks: Phoenix.LiveView — Phoenix LiveView v1.2.5

Channels / LiveView don’t have an equivalent to plug style middleware, where you can easily go to a channel or a live view and just say “oh also do X when you get a message”. LiveView handled this by adding hooks since it was a pretty common ask to deal with auth / telemetry / etc, but it doesn’t look like Channels have an equivalent yet. Probably the hooks design could also work here? That said, @kklisura’s solution is definitely the way to go for the moment.

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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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

We're in Beta

About us Mission Statement