Step 8. Background Tasks: Celery + Redis + Browser Notifications¶
Learning project: notes_chat_app, the same one as in Steps 1–7.
Result: note reminders are sent in the background through Celery Beat, and the browser receives Toast messages through HTTP polling.
Prerequisites
Before you start, make sure you have completed:
- Step 7. Async Django: WebSocket, Channels, Redis
- You need a running Redis and Docker (already there since Step 7)
What you will learn¶
- Why a Django view cannot run long tasks: the Request → Response model and its limitations
- Celery: Worker, Beat, Broker, Result Backend
@shared_task: the standard for Django projects- Celery Beat: a scheduler for tasks on a schedule (cron-like)
- Redis DB 0 vs DB 1: isolating Channels and Celery
CELERY_BEAT_SCHEDULEinsettings.py: no external packages- Browser Notifications through JS polling: when WebSocket is overkill
- Toast UI: Bootstrap Toast, aria-live, XSS protection, sessionStorage deduplication
- Testing: unit tests for the task,
@override_settings,mail.outbox, Selenium E2E
The learning project¶
The same notes_chat_app we have been building since Step 1. In this step we add:
notes_project/celery.py: initialization of the Celery appnotes_app/tasks.py: the background tasksend_reminder_notifications- New
celery-workerandcelery-beatservices indocker-compose.yml - The JSON endpoint
/reminders/check/for browser polling reminders.js: fetch + Bootstrap Toast- Tests:
test_tasks.py, additions totest_views.pyandtest_selenium.py
The resulting architecture¶
Celery Beat (every minute)
│
▼
@shared_task send_reminder_notifications()
│
├─ email → Console backend (dev)
└─ reminder.is_sent = True
Browser (every 60 seconds, HTTP polling)
│
▼
GET /reminders/check/ → JSON [{id, note__title, message, remind_at}]
│
▼
reminders.js → Bootstrap Toast (bottom right)
Reading order¶
- Celery architecture: the blocking problem, what Celery is, an ASCII diagram, Redis as the broker
- Implementation:
celery.py,tasks.py,settings.py,docker-compose.ymlstep by step - Browser Notifications: the JSON endpoint, JavaScript polling, the Toast container
- Tests: unit tests for the task, view tests, Selenium E2E
- Checkpoint: a practical exercise, a self-check checklist