Real-time messaging with instant delivery and message history
Send messages and see them appear instantly
No messages yet. Start the conversation!
Full source code for this demo
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 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>
Server maintains message history as a list of dictionaries
Python datetime automatically adds current time to each message
Request object provides authenticated username or defaults to "Guest"