A hands-on workshop for undergraduate students to learn websockets through building a real-time chat application with Flask.
- Python 3.8+
- uv package manager
- Basic knowledge of HTML, CSS, and Python
# On macOS and Linux
curl -LsSf https://astral.sh/uv/install.sh | sh
# On Windows (untested)
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"# Create virtual environment
uv venv
# Activate virtual environment
# On Linux/macOS:
source .venv/bin/activate
# On Windows:
.venv\Scripts\activateNote that the uv.lock and related files are intentionally left out of this repo so that you can run through all the commands and try it on your own. The aim of this repo is to encourage experimentation, so if something is broken you can fix it and feel good about it.
# Start a project
uv init
# For Demo 1 & 2 (basic Flask)
uv add flask
# For Demo 3+ (websockets - install when you reach Demo 3)
uv add flask-socketio
# For the sidebar on websockets
uv add websocketsEach demo is self-contained and can be run independently. Navigate to the demo directory and run the Flask application.
WARNING: The demos here have been set to run on all network interfaces: in general this is insecure as it allows outsiders to connect to a server on your system. You should either run a firewall, or change this (delete the host='0.0.0.0' bit).
cd demo1
python app.pyVisit: http://localhost:5001
Purpose: Review
- Basic Flask application structure
- Routing and templates
- Serving static files
cd demo2
python app.pyVisit: http://localhost:5002
Purpose: Review
- HTML forms and POST requests
- Form data processing
- Template variables and loops
- Limitation: On each form post, a complete new request is being sent to the server that then responds. This is not much overhead for a small application like this, but is not really interactive.
Before starting on the websocket demos, check out the demo-sidebar/ directory to see a side-by-side comparison of raw WebSockets vs Flask-SocketIO. This will help you understand why we use SocketIO as a simpler abstraction.
cd demo-sidebar
# See the README.md there for instructionscd demo3
python app.pyVisit: http://localhost:5003
What you'll learn:
- Introduction to Flask-SocketIO
- Basic websocket connection
- Real-time communication (echo functionality)
cd demo4
python app.pyVisit: http://localhost:5004
What you'll learn:
- Broadcasting messages to all users
- Multiple concurrent connections
- Basic chat room functionality
cd demo5
python app.pyVisit: http://localhost:5005
What you'll learn:
- User nicknames and join/leave notifications
- Message timestamps
- Online user list
cd demo6
python app.pyVisit: http://localhost:5006
What you'll learn:
- "User is typing" indicators
- Private messages/whispers
- Multiple chat rooms
cd demo7
python app.pyVisit: http://localhost:5007
What you'll learn:
- Real-time voting/polling
- Emoji reactions
- Collaborative features
During the workshop, we will work through the examples, but also have brief interludes talking about background and related knowledge. If you are only seeing this repo, then it is suggested that you:
- Start with Demo 1 - Get familiar with Flask basics
- Progress through each demo - Each builds on the previous
- Test each demo before moving to the next
- Experiment - Try modifying the code to see what happens
Port already in use:
# Kill process using the port (replace 5001 with your port)
# (Note: `lsof` probably only works on Linux)
lsof -ti:5001 | xargs kill -9Dependencies not found:
# Make sure virtual environment is activated
source .venv/bin/activate # Linux/macOS
# or
.venv\Scripts\activate # Windows
# Reinstall dependencies
uv add flask flask-socketioBrowser not connecting to websockets:
- Check browser developer tools (F12) for errors
- Ensure you're using the correct port
- Try refreshing the page
To test chat functionality with multiple users:
- Open multiple browser tabs/windows
- Or use different browsers
- Or use incognito/private browsing mode
- All pointing to the same localhost URL
After completing the workshop, consider exploring:
- Database integration (SQLite, PostgreSQL)
- User authentication and sessions
- Deployment to cloud platforms
- More advanced websocket features
- Integration with frontend frameworks (React, Vue)
This material was prepared for use in the "Fun with Websockets" workshop as part of Paradox 2025 at IIT Madras. The demo examples were prepared with coding help from Claude. Any errors that still exist are intentional and left as exercises to the reader.
None of this code is remotely production ready, and should not be deployed as such on an actual user facing system. In particular, the code has not been reviewed for security vulnerabilities, and it is quite likely there are simple injection issues that could be exploited without much effort. The code also displays poorly on mobile platforms - some basic responsive layout has been implemented, but is not well tested, and is known to have issues with scrolling on mobile browsers. Feel free to fix these and improve the demos.
During the workshop, all the demos can be accessed at the following links. However, they may go down randomly at any time and are not guaranteed to be available, as there will be tweaking of the code and updating live during the workshop. These are provided only for the case where someone is not able to access the IITM Intranet ports.
Nitin Chandrachoodan, IIT Madras, 2025