View Source ChannelHandler.API (channel_handler v0.2.0)

Lists all functions allowed in the ChannelHandler API.

Note the functions in this module exist for documentation purposes and one should never need to invoke them directly.

Link to this section Summary

Functions

Defines a handler for the event. function must be an arity 3 function taking the payload, context and socket.any()

The channel path for this handler. By default this has no runtime effect, but can be used by plugs and handlers if necessary.

Registers a plug for the current router/group. Plugs are run in the order they are defined before the event handler.

Defines the handle_in/3 functions for a Phoenix channel by matching on exact event names, or prefixes.

Link to this section Functions

Defines a handler for the event. function must be an arity 3 function taking the payload, context and socket.any()

example

Example

router do
  handle "create", fn payload, _context, socket ->
    post = create_post(payload)

    {:reply, post}
  end
end

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.

Registers a plug for the current router/group. 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.

Defines the handle_in/3 functions for a Phoenix channel by matching on exact event names, or prefixes.

example

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 group, which is useful to add plugs for a specific group of
  # events
  group "comments:" do
    # Adds a capture function as a plug to be run before each event in the
    group
    plug &check_permission/4, :comment

    event "create", CommentsHandler, :create
  end
end