Octoroom.Phoenix.Channel (octoroom v0.1.0)

Copy Markdown View Source

A Phoenix channel that connects one browser to a room.

# user_socket.ex
channel "room:*", MyAppWeb.RoomChannel

defmodule MyAppWeb.RoomChannel do
  use Octoroom.Phoenix.Channel, room: MyApp.TeamRoom
end

The room id is the topic after its first :, so "room:support:1234" is room "support:1234". The channel never calls Cloudflare: it relays SDP between the browser and the room server, which owns every session.

Client → server: publish, publish_tracks, push_offer, unpublish, mute, connected, answer, close_offer, worker_message.

worker_message is {worker, payload}, with payload a JSON object, for the worker's handle_info/2 (see Octoroom.Room.Server.worker_message/5). It's refused with too_large above config :octoroom, worker_messages: [max_bytes: ...] (16 KB by default), or with no_worker, not_allowed or rate_limited by the room. Server → client: renegotiate, close, push, tracks, gain, room_event, resume_token, republish, worker and worker_event.

worker is {op: "added" | "updated" | "removed", worker}, where worker is {id, outputs, data, state, attributes} (just {id} when removed): a worker this participant now sees, or no longer sees, or whose presence changed. The join reply's workers lists the ones it sees at that moment. worker_event is {worker, output, type, data}, from a worker's notify: (see Octoroom.Worker).

A known event whose body has the wrong shape is refused with {reason: "bad_payload"}, an unknown event with unknown_event.

Resuming

The join reply carries a resume_token, refreshed with a resume_token push whenever what it holds changes (the participant's Cloudflare session, its metadata). The JS client rejoins with it after its connection drops, including when the node restarted, and comes back as the same participant with the same session: the room adopts it, so nobody renegotiates. The token is encrypted with the endpoint's secret_key_base and accepted for config :octoroom, resume_max_age: seconds (default 600), which is the longest outage a room survives.

When the room server crashes while the channel stays up, the channel resumes on its own.