Phoenix.Channel.Transport

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

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 assets/cs/phoenix.coffee for an example transport client implementation.

Summary

dispatch(message, sockets, adapter_pid, router)

Dispatches %Phoenix.Socket.Message{} to Channel. All serialized, remote client messages should be deserialized and forwarded through this function by adapters

dispatch_info(sockets, data)

Arbitrary Elixir processes are received by adapters and forwarded through this function to be dispatched as "info" events on each socket channel

dispatch_info(socket, channel, data)
dispatch_leave(sockets, reason)

Whenever a remote client disconnects, the adapter must forward the event through this function to be dispatched as "leave" events on each socket channel

Functions

dispatch(message, sockets, adapter_pid, router)

Dispatches %Phoenix.Socket.Message{} to Channel. All serialized, remote client messages should be deserialized and forwarded through this function by adapters.

The following return signatures must be handled by transport adapters:

  • {:ok, sockets} - Successful dispatch, with updated HashDict of sockets
  • {:error, sockets, reason} - Failed dispatched with updatd HashDict of sockets

The returned HashDict of %Phoenix.Socket{}s must be held by the adapter

dispatch_info(sockets, data)

Arbitrary Elixir processes are received by adapters and forwarded through this function to be dispatched as "info" events on each socket channel.

The returned HashDict of %Phoenix.Socket{}s must be held by the adapter

dispatch_info(socket, channel, data)
dispatch_leave(sockets, reason)

Whenever a remote client disconnects, the adapter must forward the event through this function to be dispatched as "leave" events on each socket channel.

Most adapters shutdown after this dispatch as they client has disconnected