Phoenix v1.3.0 Phoenix.Endpoint.CowboyHandler View Source

The Cowboy adapter for Phoenix.

It implements the required child_spec/3 function as well as the handler for the WebSocket transport.

Custom dispatch options

NOTE: This feature depends on the internals of Cowboy 1.0 API and how it integrates with Phoenix. Those may change at any time, without backwards compatibility, specifically when Cowboy 2.0 is released.

You can provide custom dispatch options in order to use Phoenix’s builtin Cowboy server with custom handlers. For example, to handle raw WebSockets as shown in Cowboy’s docs).

The options are passed to both :http and :https keys in the endpoint configuration. However, once you pass your custom dispatch options, you will need to manually wire all Phoenix endpoints, including the socket transports.

You will need the following rules:

  • Per websocket transport:

    {“/socket/websocket”, Phoenix.Endpoint.CowboyWebSocket, {Phoenix.Transports.WebSocket,

    {MyApp.Endpoint, MyApp.UserSocket, :websocket}}}
  • Per longpoll transport:

    {“/socket/long_poll”, Plug.Adapters.Cowboy.Handler, {Phoenix.Transports.LongPoll,

    {MyApp.Endpoint, MyApp.UserSocket, :longpoll}}}
  • For the endpoint:

    {:_, Plug.Adapters.Cowboy.Handler, {MyApp.Endpoint, []}}

For example:

config :myapp, MyApp.Endpoint,
  http: [dispatch: [
          {:_, [
              {"/foo", MyApp.CustomHandler, []},
              {"/bar", MyApp.AnotherHandler, []},
              {:_, Plug.Adapters.Cowboy.Handler, {MyApp.Endpoint, []}}
            ]}]]

It is also important to specify your handlers first, otherwise Phoenix will intercept the requests before they get to your handler.

Link to this section Summary

Functions

Generates a childspec to be used in the supervision tree

Callback to start the Cowboy endpoint

Link to this section Functions

Link to this function child_spec(scheme, endpoint, config) View Source

Generates a childspec to be used in the supervision tree.

Link to this function start_link(scheme, endpoint, arg) View Source

Callback to start the Cowboy endpoint.