0Pricing
WebSockets & Real-Time Systems with Spring · บทเรียน

การออกแบบแบบจำลองข้อความแชต

สร้างแบบจำลองข้อมูลสำหรับข้อความแชต ผู้ใช้ และห้อง เพื่อให้การแลกเปลี่ยนข้อมูลมีประสิทธิภาพ

การออกแบบแบบจำลองข้อความแชต เป็นบทเรียน WebSockets & Real-Time Systems with Spring ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน WebSockets & Real-Time Systems with Spring และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส WebSockets & Real-Time Systems with Spring มีบทเรียนทั้งหมด 4 บทเรียน

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

Why Model Chat Data?

When building a chat application, messages, users, and rooms are core components. To handle these effectively, we need to design clear data models.

Data models define the structure of your information. This ensures consistency, makes data storage easier, and simplifies how different parts of your app interact.

The User Model

Every chat app has users! The User Model defines what information we store about each participant.

Key properties typically include:

  • ID: A unique identifier for each user.
  • Username: A human-readable name.

We'll use a simple Java class to represent this model.

User Model: Code Example

Here's a basic Java class for our User model. Notice the private fields with public getters and setters (POJO pattern).

Try running this example to see how a User object is created!

public class Main {

  public static class User {
    private String id;
    private String username;

    public User(String id, String username) {
      this.id = id;
      this.username = username;
    }

    public String getId() {
      return id;
    }

    public void setId(String id) {
      this.id = id;
    }

    public String getUsername() {
      return username;
    }

    public void setUsername(String username) {
      this.username = username;
    }

    @Override
    public String toString() {
      return "User{id='" + id + "', username='" + username + "'}";
    }
  }

  public static void main(String[] args) {
    User user1 = new User("u101", "Alice");
    System.out.println(user1);
  }
}

The Chat Room Model

Chat rooms organize conversations. The ChatRoom Model defines how we structure information about these rooms.

Important properties often include:

  • ID: A unique identifier for the room.
  • Name: The room's display name (e.g., "General Chat").
  • Members: A list of user IDs currently in the room.

Chat Room Model: Code Example

This ChatRoom class includes an ID, a name, and a way to manage its members (using List<String> for user IDs).

Run it to see a room being created and a member added.

import java.util.ArrayList;
import java.util.List;

public class Main {

  public static class ChatRoom {
    private String id;
    private String name;
    private List<String> memberIds; // Stores User IDs

    public ChatRoom(String id, String name) {
      this.id = id;
      this.name = name;
      this.memberIds = new ArrayList<>();
    }

    public String getId() {
      return id;
    }

    public void setId(String id) {
      this.id = id;
    }

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }

    public List<String> getMemberIds() {
      return memberIds;
    }

    public void addMember(String userId) {
      if (!memberIds.contains(userId)) {
        memberIds.add(userId);
      }
    }

    @Override
    public String toString() {
      return "ChatRoom{id='" + id + "', name='" + name + "', members=" + memberIds + "}";
    }
  }

  public static void main(String[] args) {
    ChatRoom generalRoom = new ChatRoom("r201", "General Chat");
    generalRoom.addMember("u101"); // Alice's ID
    System.out.println(generalRoom);
  }
}

The Chat Message Model

The heart of a chat app is the message itself! The ChatMessage Model defines what makes up a single message.

Essential properties include:

  • ID: Unique ID for the message.
  • Sender ID: The ID of the user who sent it.
  • Room ID: The ID of the room where it was sent.
  • Content: The actual text of the message.
  • Timestamp: When the message was sent.

Chat Message Model: Code Example

This ChatMessage class combines elements from our previous models and adds message-specific details.

Run it to see a message being created.

import java.time.LocalDateTime;

public class Main {

  public static class ChatMessage {
    private String id;
    private String senderId;
    private String roomId;
    private String content;
    private LocalDateTime timestamp;

    public ChatMessage(String id, String senderId, String roomId, String content) {
      this.id = id;
      this.senderId = senderId;
      this.roomId = roomId;
      this.content = content;
      this.timestamp = LocalDateTime.now(); // Set current time
    }

    public String getId() {
      return id;
    }

    public void setId(String id) {
      this.id = id;
    }

    public String getSenderId() {
      return senderId;
    }

    public void setSenderId(String senderId) {
      this.senderId = senderId;
    }

    public String getRoomId() {
      return roomId;
    }

    public void setRoomId(String roomId) {
      this.roomId = roomId;
    }

    public String getContent() {
      return content;
    }

    public void setContent(String content) {
      this.content = content;
    }

    public LocalDateTime getTimestamp() {
      return timestamp;
    }

    public void setTimestamp(LocalDateTime timestamp) {
      this.timestamp = timestamp;
    }

    @Override
    public String toString() {
      return "ChatMessage{id='" + id + "', senderId='" + senderId + "', roomId='" + roomId + "', content='" + content + "', timestamp=" + timestamp + "}";
    }
  }

  public static void main(String[] args) {
    ChatMessage msg = new ChatMessage("m301", "u101", "r201", "Hello, everyone!");
    System.out.println(msg);
  }
}

Relationships & Efficiency

Notice how our models use IDs (like senderId, roomId) instead of embedding entire User or ChatRoom objects directly.

  • Efficiency: Sending just an ID is much lighter than sending a full object, especially over a network.
  • Flexibility: If a user's details change, you don't need to update every message they ever sent.
  • Normalization: This approach helps normalize your data, reducing redundancy.

These IDs act as keys to link related data.

Model Design Check

Considering our User, ChatRoom, and ChatMessage models, which property is crucial for linking a message to the room it belongs to?

Recap: Core Chat Models

In this lesson, you learned to design the fundamental data models for a chat application:

  • User Model: Stores user details (ID, username).
  • ChatRoom Model: Defines room properties (ID, name, member IDs).
  • ChatMessage Model: Contains message details (ID, sender ID, room ID, content, timestamp).

Using IDs for relationships ensures efficient and flexible data exchange. Next, we'll implement public chat rooms!

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

บทเรียน “การออกแบบแบบจำลองข้อความแชต” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การออกแบบแบบจำลองข้อความแชต” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebSockets & Real-Time Systems with Spring ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebSockets & Real-Time Systems with Spring มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การออกแบบแบบจำลองข้อความแชต”

สร้างแบบจำลองข้อมูลสำหรับข้อความแชต ผู้ใช้ และห้อง เพื่อให้การแลกเปลี่ยนข้อมูลมีประสิทธิภาพ คุณปฏิบัติ WebSockets & Real-Time Systems with Spring ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebSockets & Real-Time Systems with Spring หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebSockets & Real-Time Systems with Spring บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “การออกแบบแบบจำลองข้อความแชต” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน WebSockets & Real-Time Systems with Spring นี้ได้ไหม

ได้ บทเรียน WebSockets & Real-Time Systems with Spring ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. การออกแบบแบบจำลองข้อความแชต
  2. การสร้างห้องแชตสาธารณะ
  3. การส่งข้อความส่วนตัวด้วย STOMP
  4. ตัวบ่งชี้การพิมพ์และสถานะออนไลน์
← กลับไปที่ WebSockets & Real-Time Systems with Spring