0Pricing
Elixir & Phoenix: Scalable Backend Development · บทเรียน

การติดตามการมีอยู่และการอัปเดตข้อมูลสด

เพิ่มการติดตามการมีอยู่ให้แอปพลิเคชัน และจัดการการอัปเดตข้อมูลสดสำหรับอินเทอร์เฟซผู้ใช้แบบไดนามิก

การติดตามการมีอยู่และการอัปเดตข้อมูลสด เป็นบทเรียน Elixir & Phoenix: Scalable Backend Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Elixir & Phoenix: Scalable Backend Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What is User Presence?

In real-time applications, presence refers to tracking who is currently online, active, or viewing a specific resource.

  • Think of a chat application showing 'online' status.
  • Or a collaborative document editor indicating who else is viewing or editing.
  • It's crucial for dynamic, interactive user interfaces.

Phoenix.Presence Explained

Phoenix Framework provides a powerful, built-in module called Phoenix.Presence to manage presence information.

  • It leverages Elixir's concurrency and distribution features.
  • It automatically handles users joining, leaving, and even network disconnections.
  • This makes building robust presence features much simpler.

Integrating Presence in Channels

To use Phoenix.Presence, you typically integrate it into your Phoenix Channel modules.

First, ensure your UserSocket defines a pubsub_server (e.g., MyApp.PubSub). Then, within your channel module, you add use Phoenix.Presence.

This mixin provides helpers for tracking and retrieving presence data.

Tracking Users with `track/3`

The core function for adding a user to presence is Phoenix.Presence.track/3.

  • It takes the socket, a unique key (e.g., user ID), and metadata (e.g., username, status).
  • You usually call this in your channel's join/3 callback.
  • When a user joins a channel, they are tracked; when they leave, their presence is automatically removed.

Example: Basic Presence Track

Let's see how track/3 is called when a user joins a channel. We'll simulate a socket for this example.

defmodule MyRoomChannel do
  use Phoenix.Channel
  use Phoenix.Presence

  @impl true
  def join("room:lobby", _payload, socket) do
    # In a real app, user_id would come from auth
    user_id = "user_#{System.unique_integer([:positive])}"
    metadata = %{username: "Guest_#{user_id}", status: "online"}

    # Track the user's presence for this topic
    Phoenix.Presence.track(socket, user_id, metadata)
    IO.puts "User #{user_id} joined and tracked in room:lobby"
    {:ok, socket}
  end

  # Minimal run_example to simulate join
  def run_example do
    mock_socket = %{topic: "room:lobby", assigns: %{}}
    join("room:lobby", %{}, mock_socket)
  end
end

MyRoomChannel.run_example()

Accessing Presence Information

Once users are tracked, you can retrieve their presence information:

  • Phoenix.Presence.list(socket): Returns a map of all presences for the channel's topic.
  • Phoenix.Presence.get_by_key(socket, user_key): Retrieves presence data for a specific user key.

These functions allow you to build dynamic UI elements based on who is online.

Live Data Updates Concept

Beyond just knowing who is online, real-time apps often need to push live data updates to clients.

  • This could be a new chat message, a score update in a game, or a change in a shared item's status.
  • The key is to send structured data to all connected clients on a specific channel topic.
  • Clients then receive this data and update their UI accordingly.

Broadcasting Data with `broadcast!`

You can send live data updates using the broadcast!/3 function (or push/3 for direct messages) from your channel or another process.

  • broadcast!(socket, "event_name", %{data: "payload"}): Sends an event and payload to all clients subscribed to the socket's topic.
  • The "event_name" helps clients differentiate between types of updates.

This is how you keep all connected clients in sync with server-side changes.

Example: Broadcasting Live Data

Here's how you might broadcast a new message to all clients in a room. We simulate the broadcast call.

defmodule MyDataUpdater do
  # We use Phoenix.Channel for its broadcast! helper
  use Phoenix.Channel

  # Function to simulate sending an update
  def send_new_message(topic, sender, text) do
    event = "new_message"
    payload = %{sender: sender, text: text, timestamp: System.system_time(:second)}

    # In a real app, you'd call broadcast! from a channel context
    # or use Phoenix.PubSub.broadcast from a GenServer.
    # For this runnable example, we'll just demonstrate the payload.
    IO.puts "Simulating broadcast to '#{topic}' with event '#{event}' and payload:"
    IO.inspect payload
    :ok
  end

  # To run this example
  def run_example do
    send_new_message("room:lobby", "Alice", "Hello everyone!")
  end
end

MyDataUpdater.run_example()

Presence & Updates Check

Which of the following statements about Phoenix.Presence and live data updates are TRUE?

Summary: Presence & Live Data

Great job! You've learned how Phoenix.Presence simplifies tracking user activity in real-time applications.

  • We covered using Phoenix.Presence.track/3 to mark users as present.
  • You also saw how to retrieve presence information with list/1 and get_by_key/2.
  • Finally, we explored sending dynamic live data updates to clients using broadcast!/3 for interactive UIs.

These tools are fundamental for building engaging real-time features!

คำถามที่พบบ่อย

บทเรียน “การติดตามการมีอยู่และการอัปเดตข้อมูลสด” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การติดตามการมีอยู่และการอัปเดตข้อมูลสด” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Elixir & Phoenix: Scalable Backend Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การติดตามการมีอยู่และการอัปเดตข้อมูลสด”

เพิ่มการติดตามการมีอยู่ให้แอปพลิเคชัน และจัดการการอัปเดตข้อมูลสดสำหรับอินเทอร์เฟซผู้ใช้แบบไดนามิก คุณปฏิบัติ Elixir & Phoenix: Scalable Backend Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Elixir & Phoenix: Scalable Backend Development หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Elixir & Phoenix: Scalable Backend Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การติดตามการมีอยู่และการอัปเดตข้อมูลสด” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Elixir & Phoenix: Scalable Backend Development นี้ได้ไหม

ได้ บทเรียน Elixir & Phoenix: Scalable Backend Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. บทนำสู่ Phoenix Channels
  2. การกระจายข้อความและการรับส่งแบบ Pub/Sub
  3. การติดตามการมีอยู่และการอัปเดตข้อมูลสด
  4. การยืนยันตัวตนและการอนุญาตสำหรับ Channel
← กลับไปที่ Elixir & Phoenix: Scalable Backend Development