View Source ChannelHandler.Dsl (channel_handler v0.4.2)
index
Index
docs
Docs
join
join
: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.:handler
(function of arity 3) - Required. 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
router do
# 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
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
- 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
router do
delegate "posts:", PostsHandler
end
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
router do
handle "create", fn payload, _context, socket ->
post = create_post(payload)
{:reply, post}
end
end
:name
(String.t/0
) - Required.:function
(function of arity 3) - Required.
scope
scope
:prefix
(String.t/0
) - Required.
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
- 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
router do
delegate "posts:", PostsHandler
end
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
router do
handle "create", fn payload, _context, socket ->
post = create_post(payload)
{:reply, post}
end
end
:name
(String.t/0
) - Required.:function
(function of arity 3) - Required.