A full-featured collaborative text editor built with Django and WebSockets that allows multiple users to edit documents simultaneously in real-time.
- Real-time collaboration: Multiple users can edit the same document simultaneously
- Live cursors: See other users' cursor positions and text selections
- User presence: View who's currently online and editing
- Document sharing: Share documents with read/write permissions
- Auto-save: Automatic saving of changes
- Rich text formatting: Basic formatting tools (bold, italic, underline)
- User authentication: Secure login/registration system
- Permission system: Control who can read, write, or admin documents
- Python 3.8+
- Redis server (for WebSocket channel layer)
- pip (Python package manager)
python -m venv editor-env
source editor-env/bin/activatepip install -r requirements.txtOn Ubuntu/Debian:
sudo apt update
sudo apt install redis-server
sudo systemctl start redis-server
sudo systemctl enable redis-serverOn macOS (using Homebrew):
brew install redis
brew services start redisOn Windows: Download and install Redis from the official website or use WSL.
Create the following directory structure:
collaborative_editor/
├── manage.py
├── collaborative_editor/
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── asgi.py
│ └── wsgi.py
├── editor/
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── views.py
│ ├── urls.py
│ ├── consumers.py
│ ├── routing.py
│ └── migrations/
├── templates/
│ ├── base.html
│ ├── editor/
│ │ ├── document_list.html
│ │ ├── editor.html
│ │ ├── create_document.html
│ │ └── share_document.html
│ └── registration/
│ ├── login.html
│ └── register.html
└── static/
└── (CSS and JS files)
Run the following commands to set up the database:
python manage.py makemigrations
python manage.py makemigrations editor
python manage.py migrate
python manage.py createsuperuserStart the development server:
python manage.py runserverThe application will be available at http://localhost:8000
The default Redis configuration in settings.py assumes Redis is running on localhost:6379. If your Redis setup is different, update the CHANNEL_LAYERS setting:
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
'hosts': [('your-redis-host', 6379)],
},
},
}For production deployment:
- Update
SECRET_KEY: Generate a new secret key and keep it secure - Set
DEBUG = False: Disable debug mode - Configure
ALLOWED_HOSTS: Add your domain names - Use a production database: PostgreSQL or MySQL instead of SQLite
- Set up a reverse proxy: Use Nginx or Apache
- Configure SSL: Enable HTTPS for WebSocket security
For production, ensure WebSocket connections use WSS (secure WebSocket) by configuring your reverse proxy to handle SSL termination.
- Register an account or log in
- Click "New Document" on the dashboard
- Enter a document title
- Start editing in the collaborative editor
- Open a document you own
- Click the "Share" button
- Enter the username of the person you want to share with
- Select their permission level (Read, Write, or Admin)
- Click "Add" to share the document
- Text Editing: Changes appear instantly for all users
- Cursor Tracking: See where other users are typing
- User Presence: View who's currently online in the top-right corner
- Auto-save: Changes are automatically saved
Ctrl+B(orCmd+B): Bold textCtrl+I(orCmd+I): Italic textCtrl+U(orCmd+U): Underline text
- Django Models: Document storage and user management
- WebSocket Consumer: Handles real-time communication
- Channel Layers: Redis-backed message passing
- Operational Transforms: Conflict resolution for simultaneous edits
- WebSocket Client: Maintains real-time connection
- Text Editor: Captures and applies text changes
- Cursor Tracking: Visual indicators for user positions
- Collaboration UI: Online users and connection status
- User types in the editor
- Change is detected and sent via WebSocket
- Server processes the change and broadcasts to other users
- Other users receive and apply the change
- Change is persisted to the database
Extend the toolbar in editor.html and add corresponding JavaScript handlers:
<button type="button" id="strikeBtn" title="Strikethrough">
<i class="fas fa-strikethrough"></i>
</button>document.getElementById('strikeBtn').addEventListener('click', () => {
this.wrapSelection('~~', '~~');
});Add CSS classes to customize the editor appearance:
.editor-dark-theme {
background-color: #2d3748;
color: #e2e8f0;
border-color: #4a5568;
}The system supports three permission levels:
- Read: Can view documents only
- Write: Can view and edit documents
- Admin: Can view, edit, and manage sharing
- Check Redis: Ensure Redis server is running
- Firewall: Verify WebSocket ports are open
- Browser Console: Check for JavaScript errors
- Django Logs: Review server logs for connection errors
- Database Indexing: Add indexes for frequently queried fields
- Caching: Implement Redis caching for document content
- Connection Limits: Set appropriate limits for concurrent users
- Operational Transforms: Implement more sophisticated conflict resolution
Issue: WebSocket fails to connect Solution: Verify Redis is running and accessible
Issue: Changes not syncing Solution: Check browser console for JavaScript errors
Issue: Users not appearing online Solution: Ensure WebSocket connection is established
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Submit a pull request
- Rich Text Editor: Integration with WYSIWYG editors
- Document History: Version control and change tracking
- File Upload: Support for images and attachments
- Comments: Inline comments and suggestions
- Export Options: PDF, Word, and other format exports
- Voice/Video Chat: Integrated communication features