Skip to main content
Shane Dewar - Software Developer

Live Chat with Python, Flask and Flask-SocketIO.

#

Description #

This project was created to experiment with a light real time chat program which works in a browser using Flask. The SocketIO protocol appeared as a great option after a little investigation and the direction I decided to pursue. There are numerous implementations of the SocketIO protocol and I chose to use the flask-socketio package which provides "low latency bi-directional communications between the clients and the server." This was exactly what I was looking for. Additionally, by using the SocketIO protocol, clients have already been implemented in Python, JavaScript, Swift, Java, C++, and as long as the SocketIO client protocol is implemented a client can be built for a huge array of platforms.

A feature I found attractive about SocketIO besides the low latency event based messaging was the ability to message to groups of users by rooms. This fit into the "chat room" concept very neatly and made SocketIO the clear choice. It was easy to find a good guide fitting the concept from Tech With Tim's Python Live Chat Room Tutorial Using Flask & SocketIO. I used the tutorial as a starting point and have extended it slightly. I added a Leave Room button and server side timestamp generation which is stored with the messages as they're sent. I decided this would be a good simple project to build out a more involved GitHub README setup guide for after getting the project up and running.

Some potential future features could be users having the ability to name the chatrooms, perhaps enabling different colors for each user name or some other way to make users easier to distinguish when talking in a room, and adding a database to store messages in a persistant way. Additionally, I am not sold on the message log storage implementaton and think that I would modify it to display a fixed amount of previous messages upon joining a room to limit server load, storage size, and latency to the user.

Table of Contents #

Installation #

Python verion 3.11.3 and pip version 23.1.2 were used to develop this project. To install once these prerequisites are installed:

Usage #

Maria creates a chat room

Maria enters the chat room

Pete joins Maria's chat room

Maria and Pete can chat

Pete disconnects from Maria's chatroom

Pete rejoins Maria's chatroom

Pete creates another chatroom

Credits #

I followed Tech With Tim's Python Live Chat Room Tutorial Using Flask & SocketIO tutorial for this project.

I extended it a bit beyond the tutorial, adding server based time stamps and a "leave room" button. I think further extending it to allow users do have different chat room colors and name colors and selecting room codes would be a good next step. Additonally, hooking up a database to store messages and perhaps only indexing a fixed amount of messages when a user joins would be another worthwhile improvement.

Flask-SocketIO's documentation was very helpful in getting a grasp with how messages are emitted and broadcast, how rooms are constructed, and other tips.

License #

Licensed under the MIT license.

Code #

The source code is here on my GitHub.