Membrane.WebRTC.Server.Peer behaviour (WebRTC Server v0.1.2) View Source

Module that manages websocket lifecycle and communication with client.

Adapts the :cowboy_websocket behaviour.

Link to this section Summary

Types

Defines custom state of a peer, passed as argument and returned by callbacks.

Defines unique indetifier (UUIDv4) bound to peer.

Functions

Encodes message into JSON and sends it to client.

Stops peer process.

Callbacks

Callback invoked when a peer process is started.

Callback invoked after successful decoding received JSON message.

Callback invoked when a peer is shutting down.

Callback invoked to extract credentials and metadata from request.

Link to this section Types

Specs

internal_state() :: any()

Defines custom state of a peer, passed as argument and returned by callbacks.

Specs

peer_id() :: String.t()

Defines unique indetifier (UUIDv4) bound to peer.

Link to this section Functions

Link to this function

send_to_client(peer, message)

View Source

Specs

send_to_client(peer :: pid(), message :: Membrane.WebRTC.Server.Message.t()) ::
  :ok

Encodes message into JSON and sends it to client.

Specs

stop(peer :: pid()) :: :ok

Stops peer process.

Link to this section Callbacks

Link to this callback

on_init(context, auth_data, options)

View Source (optional)

Specs

on_init(
  context :: Membrane.WebRTC.Server.Peer.Context.t(),
  auth_data :: Membrane.WebRTC.Server.Peer.AuthData.t(),
  options :: any()
) ::
  {:ok, state :: internal_state()}
  | {:ok, websocket_options :: :cowboy_websocket.opts(),
     state :: internal_state()}
  | {:error, cause :: any()}

Callback invoked when a peer process is started.

Useful both for confirming the identity of the client, as well as setting up state and/or custom Cowboy WebSocket options, like timeout or maximal frame size.

Options argument is value given under the :custom_options field in Membrane.WebRTC.Server.Options.

Returning {:ok, websocket_options, state} will set up WebSocket options to the ones returned.

Returning {:ok, state} will set up default WebSocket options with 15 minutes timeout.

Returning {:error, details} will abort initialization of WebSocket and return a response with status 401.

Link to this callback

on_receive(message, context, state)

View Source (optional)

Specs

on_receive(
  message :: Membrane.WebRTC.Server.Message.t(),
  context :: Membrane.WebRTC.Server.Peer.Context.t(),
  state :: internal_state()
) ::
  {:ok, message :: Membrane.WebRTC.Server.Message.t(),
   state :: internal_state()}
  | {:ok, state :: internal_state()}

Callback invoked after successful decoding received JSON message.

Peer will proceed to send message returned by this callback to room, ergo returning {:ok, state} will cause ignoring message.

Useful for modifying or ignoring messages.

Link to this callback

on_terminate(context, state)

View Source (optional)

Specs

on_terminate(
  context :: Membrane.WebRTC.Server.Peer.Context.t(),
  state :: internal_state()
) :: :ok

Callback invoked when a peer is shutting down.

Useful for any cleanup required.

Specs

parse_request(request :: :cowboy_req.req()) ::
  {:ok, credentials :: map(), metadata :: any(), room_name :: String.t()}
  | {:error, cause :: any()}

Callback invoked to extract credentials and metadata from request.

After successfully parsing the request, {:ok, credentials, metadata, room_name} should be returned. Credentials and metadata will be used to create Membrane.WebRTC.Server.Peer.AuthData which is passed to on_init/3 and Membrane.WebRTC.Server.Room.on_join/2.

Returning {:error, details} will abort initialization of WebSocket and return a response with status 400.

Peer will later try to join the room registered under room_name. If no such room can be found, peer will abort initialization of WebSocket and return a response with status 404.