Phoenix.Channel.Transport behaviour

Handles dispatching incoming and outgoing Channel messages

The Transport Adapter Contract

The Transport layer dispatches %Phoenix.Socket.Message{}‘s from remote clients, backed by different Channel transport implementations and serializations.

Server

To implement a Transport adapter, the Server must broker the following actions:

See Phoenix.Transports.WebSocket for an example transport server implementation.

Remote Client

Synchronouse Replies and ref‘s:

Channels can reply, synchronously, to any handle_in/3 event. To match pushes with replies, clients must include a unique ref with every message and the channel server will reply with a matching ref where the client and pick up the callback for the matching reply.

Phoenix includes a JavaScript client for WebSocket and Longpolling support using JSON encodings.

However, a client can be implemented for other protocols and encodings by abiding by the Phoenix.Socket.Message format

See web/static/js/phoenix.js for an example transport client implementation.

Source

Summary

chan_close_message(topic)

Returns the %Phoenix.Message{} for a channel close event

chan_error_message(topic)

Returns the %Phoenix.Message{} for a channel error event

check_origin(conn, allowed_origins, opts \\ [])

Checks the Origin request header against the list of allowed origins configured on the socket’s transport config. If the Origin header matches the allowed origins, no Origin header was sent or no origins configured it will return the given Plug.Conn. Otherwise a 403 Forbidden response will be send and the connection halted

dispatch(msg, sockets, transport_pid, socket_handler, socket, endpoint, transport)

Dispatches %Phoenix.Socket.Message{} in response to a heartbeat message sent from the client

socket_connect(transport_mod, handler, params)

Calls the socket handler’s connect/2 callback and returns the result

Functions

chan_close_message(topic)

Returns the %Phoenix.Message{} for a channel close event

Source
chan_error_message(topic)

Returns the %Phoenix.Message{} for a channel error event

Source
check_origin(conn, allowed_origins, opts \\ [])

Checks the Origin request header against the list of allowed origins configured on the socket’s transport config. If the Origin header matches the allowed origins, no Origin header was sent or no origins configured it will return the given Plug.Conn. Otherwise a 403 Forbidden response will be send and the connection halted.

Source
dispatch(msg, sockets, transport_pid, socket_handler, socket, endpoint, transport)

Dispatches %Phoenix.Socket.Message{} in response to a heartbeat message sent from the client.

The Message format sent to phoenix requires the following key / values:

  • topic - The String value “phoenix”
  • event - The String value “heartbeat”
  • payload - An empty JSON message payload, ie {}

The server will respond to heartbeats with the same message

Source
socket_connect(transport_mod, handler, params)

Calls the socket handler’s connect/2 callback and returns the result.

If the connection was successful, generates Phoenix.PubSub topic from the id/1 callback.

Source

Callbacks

default_config/0

Specs:

  • default_config :: list

Provides a keyword list of default configuration for socket transports

Source