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

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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42533 114
New

We're in Beta

About us Mission Statement