0Pricing
Elixir & Phoenix: Scalable Backend Development · Lesson

Real-Time Features with Phoenix Channels

Add bidirectional, real-time communication to your Phoenix app using Channels over WebSockets.

Real-Time Features with Phoenix Channels is a free Elixir & Phoenix: Scalable Backend Development lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Elixir & Phoenix: Scalable Backend Development learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Are Channels?

Phoenix Channels provide a real-time, bidirectional messaging layer over WebSockets (with fallbacks). Perfect for chat, notifications, and live dashboards.

The Socket Layer

Clients connect to a Socket, which authenticates the connection and routes to topics. It is defined in your endpoint.

socket "/socket", MyAppWeb.UserSocket,
  websocket: true,
  longpoll: false

Defining a UserSocket

The socket module declares which channels handle which topic patterns and how to connect.

defmodule MyAppWeb.UserSocket do
  use Phoenix.Socket
  channel "room:*", MyAppWeb.RoomChannel
  def connect(_params, socket, _info), do: {:ok, socket}
  def id(_socket), do: nil
end

Topics and Subscriptions

A topic is a string like room:lobby. Clients join a topic to send and receive messages scoped to it.

Creating a Channel

A channel module handles join and incoming events with handle_in.

defmodule MyAppWeb.RoomChannel do
  use MyAppWeb, :channel
  def join("room:" <> _id, _params, socket) do
    {:ok, socket}
  end
end

Handling Incoming Messages

handle_in/3 reacts to events pushed by clients. Return :noreply or reply to the sender.

def handle_in("new_msg", %{"body" => body}, socket) do
  broadcast!(socket, "new_msg", %{body: body})
  {:noreply, socket}
end

Broadcasting to a Topic

broadcast!/3 sends a message to EVERY subscriber of the topic, enabling real-time fan-out.

broadcast!(socket, "new_msg", %{body: "Hello everyone"})

Replying to the Sender

Return {:reply, {:ok, payload}, socket} to answer only the client that sent the event.

def handle_in("ping", _payload, socket) do
  {:reply, {:ok, %{pong: true}}, socket}
end

Pushing from the Server

Use push/3 to send a message to one connected client, for example after async work completes.

push(socket, "notification", %{text: "Job finished"})

The JavaScript Client

On the front end, the Phoenix JS client connects, joins a topic, and listens for events.

let channel = socket.channel("room:lobby", {})
channel.join()
channel.on("new_msg", payload => console.log(payload.body))

Presence Tracking

Phoenix.Presence builds on Channels to track who is online across the cluster, syncing join and leave events automatically.

Quick Check

Test your Channels knowledge.

Recap

You added real-time features with Channels:

  • Clients connect to a Socket and join topics
  • Channels handle join and handle_in events
  • broadcast! fans out, push/reply target one client
  • Presence tracks online users on top of Channels

Frequently asked questions

Is the “Real-Time Features with Phoenix Channels” lesson free?

Yes — the full text of “Real-Time Features with Phoenix Channels” is free to read here on the web, and the Elixir & Phoenix: Scalable Backend Development course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Elixir & Phoenix: Scalable Backend Development course, upgrade to CoddyKit PRO.

What will I learn in “Real-Time Features with Phoenix Channels”?

Add bidirectional, real-time communication to your Phoenix app using Channels over WebSockets. You practise Elixir & Phoenix: Scalable Backend Development with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Elixir & Phoenix: Scalable Backend Development?

No prior experience is required. Elixir & Phoenix: Scalable Backend Development on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Real-Time Features with Phoenix Channels” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Elixir & Phoenix: Scalable Backend Development lesson?

Yes. Every Elixir & Phoenix: Scalable Backend Development lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Phoenix Project Setup and Structure
  2. Routing, Controllers, and Plugs
  3. Views, Templates, and LiveView Basics
  4. Real-Time Features with Phoenix Channels
← Back to Elixir & Phoenix: Scalable Backend Development