I want to inspect socket’s (or channel’s) messages and act on some of them while letting others be handled normally, kinda what Plugs do with requests.
It seems somewhat tricky, I could probably try a handle_in that modifies the socket and uses push with some guards but it doesn’t feel right.
Am I missing something? How does one go about say rate limiting requests on the login topic in a public channel?
You could write a single handle_in clause and do your rate limiting there. For example:
def handle_in(event, payload, socket) do
case RateLimiter.log_channel_event(socket.assigns.user_id, event) do
:ok -> incoming(event, payload, socket)
{:error, :limited} -> {:reply, {:error, %{reason: "rate_limited"}}, socket}
end
end
defp incoming("new_msg", payload, socket) do
end
defp incoming("...", payload, socket) do
end
hm so do general stuff in handle_in and delegate the response giving to another function, that’s a step farther then I thought, makes sense, thank you!