💬

Chat Demo

Real-time messaging with instant delivery and message history

Live Demo

Send messages and see them appear instantly

💬

No messages yet. Start the conversation!

Chatting as Guest

Implementation

Full source code for this demo

views.py Python
from djust import LiveView
import datetime

class ChatView(LiveView):
    """
    Chat demo - showcases real-time
    communication
    """
    template_name = "demos/chat.html"

    def mount(self, request, **kwargs):
        self.messages = []
        self.username = (
            request.user.username
            if request.user.is_authenticated
            else "Guest"
        )

    def send_message(self, message=""):
        if message.strip():
            self.messages.append({
                'user': self.username,
                'text': message,
                'time': datetime.datetime.now()
                    .strftime("%H:%M"),
            })
chat.html HTML
<!-- Chat Messages -->
<div class="chat-messages">
  {% for message in messages %}
    <div class="message">
      <strong>{{ message.user }}:</strong>
      <span>{{ message.text }}</span>
      <small>{{ message.time }}</small>
    </div>
  {% endfor %}
</div>

<!-- Message Input -->
<form @submit="send_message">
  <input type="text"
         name="message"
         placeholder="Type a message..."
         autocomplete="off" />
  <button type="submit">Send</button>
</form>

<!-- Current User -->
<div>
  Chatting as <strong>{{ username }}</strong>
</div>

How It Works

📨

Message State

Server maintains message history as a list of dictionaries

⏱️

Timestamps

Python datetime automatically adds current time to each message

👤

User Context

Request object provides authenticated username or defaults to "Guest"