View Source ChannelHandler.Dsl (channel_handler v0.6.3)
index
Index
- join
- router
- plug
- event
- delegate
- handle
- scope
- plug
- event
- delegate
- handle
docs
Docs
join
join
Defines the join/3
function for a Phoenix channel.
If channel/1
is also used, the channel name will be store in the socket
assigns under the :__channel__
key. This is useful if you have multiple
channels and you need to perform runtime introspection, for example to map
a channel name to an AsyncAPI channel name.
:channel
(String.t/0
) - The channel path for this handler. By default this has no runtime effect, but can be used by plugs and handlers if necessary.
The channel value is stored in the socket's:__channel__
assign.:join
(function of arity 3) - The function to run in the channel'sjoin
callback.
router
router
Defines the handle_in/3
functions for a Phoenix channel by matching on exact
event names, or prefixes.
Example
# Adds a module plug to the list of plugs to be run before each event
plug MyApp.ChannelPlugs.EnsureAuthenticated
# Delegate all events starting with `"foo:"` to the `FooHandler` module
delegate "foo:", FooHandler
# Delegates `"create"` events to the `FooHandler.create/3` function
event "create", FooHandler, :create
# Defines an inline handler
handle "delete", fn payload, context, socket ->
result delete_post(payload)
{:reply, result, socket}
end
# Defines a scope, which is useful to add plugs for a specific scope of
# events
scope "comments:" do
# Adds a capture function as a plug to be run before each event in the
scope
plug &check_permission/4, :comment
event "create", CommentsHandler, :create
end
plug
plug
Registers a plug for the current router/scope. Plugs are run in the order they are defined before the event handler.
An optional argument can be passed as the options for the plug.
:plug
(module that adoptsChannelHandler.Plug
, a module and options, or a function of arity 4) - Required.:options
(term/0
) -
event
event
:name
(String.t/0
) - Required.:module
(atom/0
) - Required.:function
(atom/0
) - Required.
delegate
delegate
Defines a handler that delegates all events matching the prefix
to the
specified module
's ChannelHandler.Handler.handle_in/4
callback.
Example
delegate "posts:", PostsHandler
defmodule PostsHandler do
def handle_in("create", payload, _context, socket) do
post = Posts.create(payload)
{:reply, {:ok, post}, socket}
end
end
:prefix
(String.t/0
) - Required.:module
(atom/0
) - Required.
handle
handle
Defines a handler for the event
. function
must be an arity 3 function
taking the payload, context and socket.any()
Example
handle "create", fn payload, _context, socket ->
post = create_post(payload)
{:reply, post}
end
:name
(String.t/0
) - Required.:function
- Required.
scope
scope
* plug
* event
* delegate
* handle
:prefix
(String.t/0
) -
plug
Registers a plug for the current router/scope. Plugs are run in the order they are defined before the event handler.
An optional argument can be passed as the options for the plug.
:plug
(module that adoptsChannelHandler.Plug
, a module and options, or a function of arity 4) - Required.:options
(term/0
) -
event
:name
(String.t/0
) - Required.:module
(atom/0
) - Required.:function
(atom/0
) - Required.
delegate
Defines a handler that delegates all events matching the prefix
to the
specified module
's ChannelHandler.Handler.handle_in/4
callback.
Example
delegate "posts:", PostsHandler
defmodule PostsHandler do
def handle_in("create", payload, _context, socket) do
post = Posts.create(payload)
{:reply, {:ok, post}, socket}
end
end
:prefix
(String.t/0
) - Required.:module
(atom/0
) - Required.
handle
Defines a handler for the event
. function
must be an arity 3 function
taking the payload, context and socket.any()
Example
handle "create", fn payload, _context, socket ->
post = create_post(payload)
{:reply, post}
end
:name
(String.t/0
) - Required.:function
- Required.