Phoenix.Socket.Handler

Summary

hibernate(socket)

Hibernates the socket

info(info, req, state)

Handles receiving messages from processes

init(arg1, req, opts)

Initializes cowboy websocket

reply(socket, frame)

Sends a reply to the socket. Follow the cowboy websocket frame syntax

terminate(socket)

Terminates a connection

websocket_handle(arg1, req, socket)

Dispatches multiplexed socket message to Router and handles result

websocket_info(message, req, state)

Handles regular messages sent to the socket process

websocket_init(transport, req, opts)

Handles initalization of the websocket

websocket_terminate(reason, req, socket)

This is called right before the websocket is about to be closed

Functions

hibernate(socket)

Hibernates the socket.

info(info, req, state)

Handles receiving messages from processes

init(arg1, req, opts)

Initializes cowboy websocket

The following transport options can be provided:

  • {:tcp, :http} - Insecure transport over http
  • {:ssl, :http} - Secure transport over https
reply(socket, frame)

Sends a reply to the socket. Follow the cowboy websocket frame syntax

Frame is defined as:

  • :close | :ping | :pong
  • {:text | :binary | :close | :ping | :pong, iodata()}
  • {:close, close_code(), iodata()}

Options:

  • :state
  • :hibernate # (true | false) if you want to hibernate the connection

close_code: 1000..4999

terminate(socket)

Terminates a connection.

websocket_handle(arg1, req, socket)

Dispatches multiplexed socket message to Router and handles result

Join Event

“join” events are specially treated. When {:ok, socket} is returned from the Channel, the socket is subscribed to the channel and authorized to pubsub on the channel/topic pair When {:error, socket, reason} is returned, the socket is denied pubsub access

Leave Event

“leave” events call the channels leave/2 function only if the socket has previously been authorized via join/2

Arbitrary Events

Any other event calls the channel’s event/3 function, with the event name as the first argument. Event handlers are only invoked if the socket was previously authorized via join/2.

Heartbeat

Clients can send a heartbeat message on the phoenix channel and receive the same heartbeat messsage as a response. This is useful to ensure long-running sockets are kept alive. Message format;

%Message{channel: "phoenix", topic: "conn", event: "heartbeat", message: %{}}
websocket_info(message, req, state)

Handles regular messages sent to the socket process

Each message is forwarded to the “info” event of the socket’s authorized channels

websocket_init(transport, req, opts)

Handles initalization of the websocket

Possible returns:

  • :ok
  • {:ok, req, state}
  • {:ok, req, state, timeout} - Timeout defines how long it waits for activity

    from the client. Default: infinity.
websocket_terminate(reason, req, socket)

This is called right before the websocket is about to be closed.

Reason is defined as:

  • {:normal, :shutdown | :timeout} - Called when erlang closes connection
  • {:remote, :closed} - Called if client formally closes connection
  • {:remote, close_code(), binary()}
  • {:error, :badencoding | :badframe | :closed | atom()} - Called for many reasons

    tab closed, conn dropped.