Skip to content

[Chat] Design Database Schema for Messaging [40-2] #48

Description

@mehalyna

Description

Design and implement a MongoDB database schema for storing chat messages and conversation metadata, while utilizing Django Channels for real-time chat functionality. Each chat session between users will be managed as a "Room," which facilitates message exchanges and organizes conversations.

Technical Steps:

1. Setting Up Django with MongoDB

  • Ensure your Django project is configured to use MongoDB.
  • Modify your settings.py to use MongoDB.

2. Create Django Models for Rooms and Messages

Models Definition:

Room Model:

This model stores metadata about each chat room, such as the participants and the creation and update timestamps.

Message Model:

Each message will be stored as a separate record linked to a room. This setup supports efficient message retrieval and storage.

Example Models in Django:

User = get_user_model()

class Room(models.Model):

    participants = models.ManyToManyField(User, related_name='rooms')

    created_at = models.DateTimeField(auto_now_add=True)

    updated_at = models.DateTimeField(auto_now=True)



class Message(models.Model):

    room = models.ForeignKey(Room, related_name='messages', on_delete=models.CASCADE)

    sender = models.ForeignKey(User, related_name='sent_messages', on_delete=models.CASCADE)

    text = models.TextField()

    timestamp = models.DateTimeField(auto_now_add=True)

Integrate Django Channels for Real-Time Communication

Django Channels will handle the WebSocket connections for real-time communication.
Update settings.py to include Channels settings:
Create routing and consumer configurations to manage WebSocket connections and messages for each Room.

Save Chat History in Rooms

Every message sent through Django Channels within a Room should be saved to the database. The consumer handling WebSocket messages ensures that all messages are recorded, enabling persistent message history and retrieval.

Task #40

Metadata

Metadata

Assignees

Labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions