Packages
phoenix_gen_api
2.17.0
2.22.0
2.21.1
2.21.0
2.20.1
2.20.0
2.19.0
2.18.1
2.18.0
2.17.1
2.17.0
2.16.0
2.15.0
2.14.0
2.13.0
2.12.0
2.11.0
2.10.0
2.9.1
2.9.0
2.8.0
2.7.0
2.6.1
2.6.0
2.5.0
2.4.0
2.3.0
2.2.0
2.1.1
2.1.0
2.0.1
2.0.0
1.2.0
1.1.3
1.1.2
1.1.1
1.1.0
1.0.1
1.0.0
0.2.1
0.2.0
0.1.1
0.1.0
0.0.16
0.0.15
0.0.14
0.0.13
0.0.12
0.0.11
0.0.10
0.0.9
0.0.8
0.0.7
0.0.6
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1
A library for fast develop APIs in Elixir cluster, using Phoenix Channels for transport data, auto pull api configs from service nodes.
Current section
Files
Jump to
Current section
Files
lib/phoenix_gen_api/helpers/channel_helpers.ex
defmodule PhoenixGenApi.ChannelHelpers do
@moduledoc """
Shared push-handling logic for GenApi WebSocket channels.
Inject into a channel with `use PhoenixGenApi.ChannelHelpers`.
Provides common `handle_info` clauses for:
- synchronous push results
- streaming responses
- async RPC call results
## Configuration
The event name used for pushing responses to the client can be configured
via the `:event` option. This should match the event name used in the
channel's `handle_in/3` clause.
use PhoenixGenApi.ChannelHelpers, event: "my_custom_event"
By default, the event name is `"phoenix_gen_api"`, consistent with the
main `use PhoenixGenApi` macro.
## Migration from Previous Versions
Previously, this module hardcoded the event name as `"gen_api_result"`.
If you relied on that behavior, configure it explicitly:
use PhoenixGenApi.ChannelHelpers, event: "gen_api_result"
"""
defmacro __using__(opts) do
event = Keyword.get(opts, :event, "phoenix_gen_api")
quote location: :keep, bind_quoted: [event: event] do
@phoenix_gen_api_channel_event event
require Logger
@doc false
def handle_info({:push, result}, socket) do
Logger.debug(fn ->
"[ChannelHelpers] push result: #{inspect(result)}, module: #{__MODULE__}"
end)
push(socket, @phoenix_gen_api_channel_event, result)
{:noreply, socket}
end
@doc false
def handle_info({:stream_response, result}, socket) do
Logger.debug(fn ->
"[ChannelHelpers] stream response: #{inspect(result)}, module: #{__MODULE__}"
end)
push(socket, @phoenix_gen_api_channel_event, result)
{:noreply, socket}
end
@doc false
def handle_info({:async_call, result}, socket) do
Logger.debug(fn ->
"[ChannelHelpers] async call result: #{inspect(result)}, module: #{__MODULE__}"
end)
push(socket, @phoenix_gen_api_channel_event, result)
{:noreply, socket}
end
@doc false
def handle_info({:stream_started, request_id, pid}, socket) do
Logger.debug(fn ->
"[ChannelHelpers] stream started: request_id=#{inspect(request_id)}, module: #{__MODULE__}"
end)
{:noreply, socket}
end
end
end
end