Adding Instant Messaging to a Python/Django App—How to Approach This?

Real Python · Beginner ·🛠️ AI Tools & Apps ·8y ago

Key Takeaways

The video discusses how to add an instant messaging feature to a Python/Django app, covering topics such as creating a model for messages, using standard Django forms, and implementing caching to reduce database queries. Tools and techniques demonstrated include Django, Python, Unicode, HTML, ASCII, Emojis, Forms, Models, Databases, SQL, and caching.

Full Transcript

hey what's up guys so I got another YouTube question that I want to talk about so there's more of a system design question about building a specific feature in a django app so this is a question about building an instant messaging feature into into an app using Python or into django app using Python and just want to read out the question so so this question is from random hahahaha hi random so hi Dan I hope you are doing great yes I am thank you very much um I love watching your videos I am a beginner Python programmer and I am very curious about this ubiquitous actually how he pronounced that word ubiquitous I guess I think it's ubiquitous somebody called please call me out in the comments that is instant messaging where the users off the application can send text messages to each other how would you implement such a feature using the Django framework in Python do I need to be an expert if I need to scale such an application to suppose around 10,000 users okay so just a quick rundown in terms of how would I implement this and add this feature to an existing Django app well what does it mean to send a message from one user to another how what would this look like you know what do you want this to look like from the perspective of your user do you want them to log in to the application you know let's suppose I send you a message in this application I type it probably type it into some kind of form hit the send button and when you log into the app or you know you're gonna see maybe a little notification up there like a little link that says hey there's one new message available here for random from damn and you click on it and you see the text content and you maybe get the same message box or form back we can reply to me that I feel like that would be like a basic use case for this now maybe that you would want some other features like email notifications for pending messages or maybe you want some kind of wet push notification where people can get these little pop-ups on their phone or in their browser window when there new message and we can talk about that but I'm mainly going to focus on this like central use case of okay you know you enter a message and the other user sees it the next time they log into the app well so how would I do this well in terms of the the user interface you need some kind of forum where people can pick another pick a pick a user they want to send a message to and then have a forum where they can enter text and you can make that more or less complicated you know it maybe we're just allowing like very simple a plain ASCII text messages or maybe you want to allow rich text messages where people can you know use some formatting tools like make things bold or you know use emoji and stuff like that and the question is like do we need that so you know I always like approaching these things from what is sort of the bare-bones implementation that you can start out with and then grow it from there so I'm just gonna assume we have a simple text box this is just a plain text field we can just enter you know plain ASCII text or Unicode text you know I mean without rich text formatting or any kind of HTML formatting so just text and you know stored Unicode so that will support all kinds of different characters and we can use this application internationally so you know just whip up or to create a standard Django form for dad I mean you know I'm not gonna do this now like coat this out this is more like a system design question but essentially what you're gonna have a model for these messages and they're gonna have a from and a2 field that are gonna link that model to to user account so I know who's writing a message to who and it will also have a body field or a text field on that model where we store the text content now when I send a message to you we're gonna create an instance of that model and we're just gonna store that then when whenever you log in or whenever your user account requests a page and that's getting that's getting rendered and send out to you to your browser we're just gonna check if there's a new message for your user you know in the to field so we're just going to run a database query just we're just gonna do it every single time you request the page and if there is a pending message that you haven't read yet so actually you probably won at this point I want to add some kind of flag on that message where you can mark a message as delivered or read so we can we know when to display this so we're just gonna but for the base case you know for the absolute like basic basic implementation here we're just look if there's any messages available for you and then maybe show you a little counter or show you some link when you where you can view that message so that's probably the simplest implementation that I could think of you know we can add a new message in and you you will see that you can click on that and then we need another you know we need another view for that to be able to see that message maybe we could just display it in the same edit view in the beginning just you know just had to get something running very quickly but you know we probably want to grow it from there so maybe one of the next things we want to do maybe we want like headline and the body text you know to make this more similar to email or maybe we want we want real-time updates so that two people can chat back and forth it would be more like a chat system and not so much just a simple you know email based or you know modeled after an email exchange more like a an asynchronous system like you write something I'll get it a little bit later and I write back but we're not both sitting in front of a computer and sending sending each other these things so you can make the UI nicer for that right but like the absolute base case would just be hey we have a model in the database and or we have a model in Django and then we use that to persist that information to the database and we're just gonna story from two maybe has his message been delivered or red flag and we're also going to add a text field and then and that's that's essentially it for the absolute basic implementation now I want to talk a little bit about the need to scale such an application and I think here you know when you're talking about 1,000 users for estimating any kind of you know making any kind of performance estimate estimations I think it's important to know what what time frame we're looking at you know what's how often is is this app seeing but if this app has 10,000 registered users how active are they you know are they are there ten thousand concurrent users that is gonna be more challenging than just having ten thousand users in general they're all serve roughly distributed you know in the world or let's say northern America around a couple of times owns and you only ever have maybe ten ten of them on the site at the same time in that case it's not gonna hurt you at all just to run that query every single time one of the users requests a page and and just to display that message counter you know just display like hey you have a new message counter here but as the traffic increases you probably want to think about some wave of caching so maybe we're not gonna check every single page view but maybe we're gonna only check once a minute again you know it's not gonna make a huge difference to our users it might depending on the requirements and kind of hot what you want the user experience to be like but it's going to reduce the amount of queries that we're gonna have to run to do this and there there are strategies like that you can apply the you know the the the more load your application gets but just based on that number you know ten thousand users I wouldn't worry about too much about optimizing the performance for that you could probably get away with some basic some basic caching and you know only updating that message count like every every minute every thirty seconds so whenever it is actually room to do it you know maybe it's like a permanent background task that just go through all the users and checks if there's a new message or even better it could be done in a way where when when somebody sends a new message the act of sending that message will actually go and update the cache so that the next time that user logs in or requests a page the recipient of that message requests a page they're gonna get that little that little updated message counter and that way you know we still have to query the cache but we're only ever gonna hit the the data vise when the database when the cash gets invalidated or when we post a new message in or we actually need to fetch it so you know there's essentially I would start with a dead simple a simple implementation and by dead-simple i mean this is already pretty complicated if you're if you're saying you're beginner right like I touched on a number of things and and I would actually love to provide some kind of code sample for this in the future but for the very very basic implementation you don't have to go too crazy and I wouldn't really worry about performance it gets things get a lot more complicated when you're factoring in other deliver mechanisms like email you know when is that email gonna get delivered and you probably don't want to do that in the in the handler for that particular web request you know the function that that executes before the response is generated or that it actually generates the response well you probably want to offload that to some background task that just goes hey now I need to send an email and handle staff because you don't want to be blocking on that email sending and and it's the same with wet push for example again you probably want to build out some asynchronous system there that delivers that in the background or offload that to some third party integration as well where maybe you're not supporting web push directly but maybe you're going through another system like like I'm using push crew on my own site and they handle most of that and I can just you know it's it's a different system really like it's a different use case but you know there's a couple of other options you can use there so but just in terms of the basic implementation this is what I what I would do you know keep it straightforward keep it simple don't worry about too much about scaling this this is it to me doesn't seem like it would really dramatically increase the load on your site if it did a system like that and adding that one query you know it's just everything is keyed by by the user ID there are those messages so you're gonna have you know the database has gonna have a really easy time finding that alright I hope this was helpful it was more you know I I feel like I should have shown you some actual like diagrams and maybe written some code to show you this but this is kind of the high level approach that I would pick here and would be great to see when you implement that implicit application and just just let me know about your progress there all right best of luck with that and happy Python me

Original Description

https://dbader.org/python-tricks ► This is an ongoing experiment: Make your code more Pythonic with short & sweet code examples Ever wondered how to add an instant messaging/chat system to your Python web application powered by Django? This week I'm discussing how to approach this problem based on a question that I got from You Tube subscriber "Ran Dom": "I am a beginner Python programmer. And I am very curious about this ubiquitous feature i.e instant messaging where the users of the application can send text messages to each other. How would you implement such a feature using the Django framework and Python? Do I need to be an expert if I need to scale such an application to suppose 10000 users?" How would you go about implementing this feature? Leave a comment below and let me know. FREE COURSE – "5 Thoughts on Mastering Python" https://dbader.org/python-mastery SUBSCRIBE TO THIS CHANNEL: https://dbader.org/youtube * * * ► Python Developer MUGS, T-SHIRTS & MORE: https://nerdlettering.com ► PythonistaCafe – A peer-to-peer learning community for Python developers: https://www.pythonistacafe.com FREE Python Coding Tutorials & News: » Python Tutorials: https://dbader.org » Python News on Twitter: https://twitter.com/@dbader_org » Weekly Tips for Pythonistas: https://dbader.org/newsletter » Subscribe to this channel: https://dbader.org/youtube
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Real Python · Real Python · 0 of 60

← Previous Next →
1 A better Python REPL – bpython vs python interpreter
A better Python REPL – bpython vs python interpreter
Real Python
2 Introducing large-type.com – A Utility Website
Introducing large-type.com – A Utility Website
Real Python
3 Reading Hacker News Without Wasting Tons of Time
Reading Hacker News Without Wasting Tons of Time
Real Python
4 Forward References and Python 3 Type Hints
Forward References and Python 3 Type Hints
Real Python
5 Using Sublime Text as your Git Editor
Using Sublime Text as your Git Editor
Real Python
6 Python Code Linting and Auto-Complete for Sublime Text
Python Code Linting and Auto-Complete for Sublime Text
Real Python
7 Make your Python Code More Readable with Custom Exceptions
Make your Python Code More Readable with Custom Exceptions
Real Python
8 Write Better Tests with Sublime Text's Split Layout Feature
Write Better Tests with Sublime Text's Split Layout Feature
Real Python
9 How to Use Sublime Text from the Command Line
How to Use Sublime Text from the Command Line
Real Python
10 Rename Variables with Multiple Selection in Sublime Text
Rename Variables with Multiple Selection in Sublime Text
Real Python
11 Sublime Text Settings for Writing PEP 8 Python
Sublime Text Settings for Writing PEP 8 Python
Real Python
12 Write Cleaner Python with Sublime Text's Indent Guides
Write Cleaner Python with Sublime Text's Indent Guides
Real Python
13 Sublime Text Whitespace Settings for Python Development
Sublime Text Whitespace Settings for Python Development
Real Python
14 Function Argument Unpacking in Python
Function Argument Unpacking in Python
Real Python
15 Python Code Review: Debugging and Refactoring "Conway's Game of Life" +  Automated Tests
Python Code Review: Debugging and Refactoring "Conway's Game of Life" + Automated Tests
Real Python
16 Using "get()" to Return a Default Value from a Python Dict
Using "get()" to Return a Default Value from a Python Dict
Real Python
17 A Python Shorthand for Swapping Two Variables
A Python Shorthand for Swapping Two Variables
Real Python
18 Python Code Review: Refactoring a Web Scraper, PEP 8 Style Guide Compliance, requirements.txt
Python Code Review: Refactoring a Web Scraper, PEP 8 Style Guide Compliance, requirements.txt
Real Python
19 Click & Jump to Test Failures from the Command Line (iTerm2)
Click & Jump to Test Failures from the Command Line (iTerm2)
Real Python
20 Setting up Sublime Text for Python Developers
Setting up Sublime Text for Python Developers
Real Python
21 Sublime Text + Python Guide Overview
Sublime Text + Python Guide Overview
Real Python
22 Python Code Review: Adding Pytest Tests to an Existing Python Web Scraper
Python Code Review: Adding Pytest Tests to an Existing Python Web Scraper
Real Python
23 Type-Checking Python Programs With Type Hints and mypy
Type-Checking Python Programs With Type Hints and mypy
Real Python
24 A Shorthand for Merging Dictionaries in Python 3.5+
A Shorthand for Merging Dictionaries in Python 3.5+
Real Python
25 Python Code Review Flask Web Security Tutorial + Virtualenvs, requirements.txt
Python Code Review Flask Web Security Tutorial + Virtualenvs, requirements.txt
Real Python
26 My Python Code Looks Ugly and Confusing – Help!
My Python Code Looks Ugly and Confusing – Help!
Real Python
27 Setting Up a Programmer Portfolio/Developer Blog – How To Get Started
Setting Up a Programmer Portfolio/Developer Blog – How To Get Started
Real Python
28 Do I Need a GitHub/GitLab/Bitbucket Profile as a Developer?
Do I Need a GitHub/GitLab/Bitbucket Profile as a Developer?
Real Python
29 Programmer Portfolio – Example and Walkthrough
Programmer Portfolio – Example and Walkthrough
Real Python
30 How to Get Your 1st Speaking Gig at a Tech Conference
How to Get Your 1st Speaking Gig at a Tech Conference
Real Python
31 How to Build Your Public Speaking Skills as a Developer
How to Build Your Public Speaking Skills as a Developer
Real Python
32 The Object-oriented Version of "Spaghetti Code" is "Lasagna Code" ?!
The Object-oriented Version of "Spaghetti Code" is "Lasagna Code" ?!
Real Python
33 Setting up Sublime Text for Python Developers – Lesson #1
Setting up Sublime Text for Python Developers – Lesson #1
Real Python
34 Cool New Features in Python 3.6
Cool New Features in Python 3.6
Real Python
35 "is" vs "==" in Python – What's the Difference? (And When to Use Each)
"is" vs "==" in Python – What's the Difference? (And When to Use Each)
Real Python
36 Emulating switch/case Statements in Python with Dictionaries
Emulating switch/case Statements in Python with Dictionaries
Real Python
37 Python Function Argument Unpacking Tutorial (* and ** Operators)
Python Function Argument Unpacking Tutorial (* and ** Operators)
Real Python
38 What Code Should I Put On My GitHub/GitLab/BitBucket Profile?
What Code Should I Put On My GitHub/GitLab/BitBucket Profile?
Real Python
39 A Crazy Python Dictionary Expression ?!
A Crazy Python Dictionary Expression ?!
Real Python
40 String Conversion in Python: When to Use __repr__ vs __str__
String Conversion in Python: When to Use __repr__ vs __str__
Real Python
41 Method Types in Python OOP: @classmethod, @staticmethod, and Instance Methods
Method Types in Python OOP: @classmethod, @staticmethod, and Instance Methods
Real Python
42 Optional Arguments in Python With *args and **kwargs
Optional Arguments in Python With *args and **kwargs
Real Python
43 Python Context Managers and the "with" Statement (__enter__ & __exit__)
Python Context Managers and the "with" Statement (__enter__ & __exit__)
Real Python
44 Installing Python Packages with pip and virtualenv / venv
Installing Python Packages with pip and virtualenv / venv
Real Python
45 "For Each" Loops in Python with enumerate() and range()
"For Each" Loops in Python with enumerate() and range()
Real Python
46 Python Code Review: LibreOffice Automation and the Python Standard Library
Python Code Review: LibreOffice Automation and the Python Standard Library
Real Python
47 Managing Python Dependencies With Pip and Virtual Environments – Lesson #1
Managing Python Dependencies With Pip and Virtual Environments – Lesson #1
Real Python
48 Python Tutorial: List Comprehensions Step-By-Step
Python Tutorial: List Comprehensions Step-By-Step
Real Python
49 Leveraging Python's Implicit "return None" Statements
Leveraging Python's Implicit "return None" Statements
Real Python
50 What's the meaning of underscores (_ & __) in Python variable names?
What's the meaning of underscores (_ & __) in Python variable names?
Real Python
51 Python Data Structures: Sets, Frozensets, and Multisets (Bags)
Python Data Structures: Sets, Frozensets, and Multisets (Bags)
Real Python
52 Writing automated tests for Python command-line apps and scripts
Writing automated tests for Python command-line apps and scripts
Real Python
53 How to find great Python packages on PyPI, the Python Package Repository
How to find great Python packages on PyPI, the Python Package Repository
Real Python
54 Immutable vs Mutable Objects in Python
Immutable vs Mutable Objects in Python
Real Python
55 PyPI vs Warehouse, the Next-Generation Python Package Repository
PyPI vs Warehouse, the Next-Generation Python Package Repository
Real Python
56 pep8.org — The Prettiest Way to View the PEP 8 Python Style Guide
pep8.org — The Prettiest Way to View the PEP 8 Python Style Guide
Real Python
57 My Experience at PyCon 2017 in Portland
My Experience at PyCon 2017 in Portland
Real Python
58 Pylint Tutorial – How to Write Clean Python
Pylint Tutorial – How to Write Clean Python
Real Python
59 "Reverse a List in Python" Tutorial: Three Methods & How-to Demos
"Reverse a List in Python" Tutorial: Three Methods & How-to Demos
Real Python
60 Python Refactoring: "while True" Infinite Loops & The "input" Function
Python Refactoring: "while True" Infinite Loops & The "input" Function
Real Python

The video teaches how to add an instant messaging feature to a Python/Django app, covering topics such as creating a model for messages, using standard Django forms, and implementing caching to reduce database queries. This is useful for developers who want to build a chat system into their web application. The video provides a high-level approach to building an instant messaging system and discusses the importance of caching and performance optimization.

Key Takeaways
  1. Create a model for messages with from and to fields to link to user accounts
  2. Use a standard Django form for a simple text box to enter plain ASCII text or Unicode text
  3. Store messages in a database and retrieve them when a user logs in
  4. Run a database query to check for new messages every time a user requests a page
  5. Add a flag to mark messages as delivered or read
  6. Implement caching to reduce database queries as traffic increases
  7. Use a permanent background task to update the cache
  8. Cache can be invalidated when a new message is posted or fetched
  9. Use asynchronous systems for tasks like email sending and web push
💡 Implementing caching is crucial to reduce database queries and improve performance as traffic increases

Related Reads

Up next
Build an MCP Server with n8n | Full MCP Tutorial
Thomas Janssen
Watch →