Membrane.WebRTC.Server.Room behaviour (WebRTC Server v0.1.3) View Source

A behaviour module for WebRTC room that manages peers and mediate in their communication.

Rooms have to be created explicitly (preferably by start_supervised/2 function).

Link to this section Summary

Types

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

Functions

Returns a specification to start this module under a supervisor.

Forwards the message to the addressees given under message.to key.

Starts a room based on the options.module, registers it in options.registry (under the options.name) and links it to the current process.

Creates a room under the default room supervisor.

Stops room process.

Callbacks

Callback invoked before forwarding messages either peers.

Callback invoked when a room is created.

Callback invoked when a peer is about to join the room.

Callback invoked when a peer is leaving the room.

Callback invoked when the room is shutting down.

Link to this section Types

Specs

internal_state() :: any()

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

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

forward_message(room, message)

View Source

Specs

forward_message(room :: pid(), message :: Membrane.WebRTC.Server.Message.t()) ::
  :ok | {:error, :no_such_peer} | {:error, :unknown_error}

Forwards the message to the addressees given under message.to key.

Messages meant to be broadcasted should have message.to set to "all". Broadcasted message will be forwarded to all peers, except for sender (given under message.from).

Specs

Starts a room based on the options.module, registers it in options.registry (under the options.name) and links it to the current process.

options.custom_options are passed to module's on_init/1 callback.

Link to this function

start_supervised(options)

View Source

Specs

Creates a room under the default room supervisor.

Calls start_link/1 underneath.

Specs

stop(room :: pid()) :: :ok

Stops room process.

Link to this section Callbacks

Link to this callback

on_forward( message, state )

View Source (optional)

Specs

on_forward(
  message :: Membrane.WebRTC.Server.Message.t(),
  state :: internal_state()
) ::
  {:ok, internal_state()}
  | {:ok, Membrane.WebRTC.Server.Message.t(), internal_state()}

Callback invoked before forwarding messages either peers.

This mean this callback will be invoked every time message is forwarded or the room broadcasts messages by itself (e.g. when peer joins the room).

Room will forward_message message returned by this callback, ergo returning {:ok, state} will cause ignoring message.

Link to this callback

on_init(options)

View Source (optional)

Specs

Callback invoked when a room is created.

Link to this callback

on_join(auth_data, state)

View Source (optional)

Specs

on_join(
  auth_data :: Membrane.WebRTC.Server.Peer.AuthData.t(),
  state :: internal_state()
) ::
  {:ok, internal_state()} | {{:error, error :: atom()}, internal_state()}

Callback invoked when a peer is about to join the room.

Useful for authorizing or performing other checks (e.g. controlling number of peers in room).

Returning {:error, error} will cause peer sending Membrane.WebRTC.Server.Message.error_message/0 to the client and closing WebSocket.

Link to this callback

on_leave(peer_id, state)

View Source (optional)

Specs

on_leave(
  peer_id :: Membrane.WebRTC.Server.Peer.peer_id(),
  state :: internal_state()
) ::
  {:ok, internal_state()}

Callback invoked when a peer is leaving the room.

Link to this callback

on_terminate(state)

View Source (optional)

Specs

on_terminate(state :: internal_state()) :: :ok

Callback invoked when the room is shutting down.

Useful for any cleanup required.