This project is a real-time chat app that uses Redis Pub/Sub and Stream to enable real-time messaging between users. The app allows users to create channels, send messages, and subscribe to channels.
- Real-time messaging using Redis Pub/Sub and Stream
- Channel creation and management
- User subscription and message broadcasting
- Terminal UI for easy user interaction
- Redis installed and running on your local machine or a cloud provider
- Python 3.8 or higher installed on your local machine
- Clone the repository:
git clone https://github.com/PEMIDI/real-time-chat-app.git
- Install the required packages:
pip install -r requirements.txt
- Start the Redis server:
redis-server
- Run the app:
python app.py
- Open a terminal and run the app:
python app.py
- Create a channel:
/join <channel_name>
- Send a message:
/send <message>
- Subscribe to a channel:
/join <channel_name>
- Leave a channel:
/leave
- List all channels:
/channels
- Quit the app:
/quit
REDIS_HOST
: the hostname or IP address of the Redis instance (default:localhost
)REDIS_PORT
: the port number of the Redis instance (default:6379
)REDIS_DB
: the database number to use (default:0
)REDIS_PASSWORD
: the password to use for authentication (optional)
CHANNEL_PREFIX
: the prefix for channel names (default:channel:
)MESSAGE_PREFIX
: the prefix for message IDs (default:message:
)
- Check the Redis server logs for errors
- Check the app logs for errors
- Verify that the Redis instance is running and accessible
Contributions are welcome Please fork the repository and submit a pull request with your changes.
This project is licensed under the MIT License. See the LICENSE
file for details.
This project uses the following open-source libraries, you can use any library to develop this app:
redis
: a Python client for Redis
I hope this helps Let me know if you have any questions or need further assistance.
Task Title: Real-Time Chat App with Redis Pub/Sub and Stream 📱💬
Product Requirements Document
Overview: Create a real-time chat application using Redis Pub/Sub and Stream. The application should allow users to send and receive messages in real-time. The application should have the following features:
- Users can create channels and send messages to specific channels.
- Users can subscribe to channels and receive messages in real-time.
- Messages should be stored in a Redis Stream for persistence and scalability.
Project Structure:
real-time-chat-app/
app.py
models.py
redis_config.py
requirements.txt
README.md
tests/
test_app.py
test_models.py
Task Requirements:
Part 1: Setting up Redis and Python Client (1 hour) 🚀
- Install Redis on your local machine or use a cloud provider like Redis Labs.
- Install the required Python packages, including:
redis
for interacting with Redis.pytest
for unit testing.
- Create a
redis_config.py
file to store Redis connection settings, including:REDIS_HOST
: the hostname or IP address of the Redis instance.REDIS_PORT
: the port number of the Redis instance.REDIS_DB
: the database number to use.REDIS_PASSWORD
: the password to use for authentication (optional).
Part 2: Implementing Redis Pub/Sub (2 hours) 📢
- Create a
models.py
file to define the following models:User
: a model with a uniqueid
andusername
.Channel
: a model with a uniqueid
andname
.
- Implement a
publish_message
function inapp.py
to publish a message to a specific channel using Redis Pub/Sub. The function should:- Take a
channel_id
andmessage
as input. - Use the
redis
client to publish the message to the specified channel. - Return a success message indicating that the message was published.
- Take a
- Implement a
subscribe_to_channel
function inapp.py
to subscribe to a specific channel and receive messages in real-time. The function should:- Take a
channel_id
as input. - Use the
redis
client to subscribe to the specified channel. - Return a success message indicating that the subscription was successful.
- Take a
Part 3: Implementing Redis Stream (2 hours) 📊
- Modify the
models.py
file to add aStream
model with the following attributes:id
: a unique identifier for the message.channel_id
: the ID of the channel the message was sent to.message
: the text of the message.timestamp
: the timestamp when the message was sent.
- Implement a
produce_message
function inapp.py
to produce a message to a Redis Stream. The function should:- Take a
channel_id
andmessage
as input. - Use the
redis
client to produce a message to the Redis Stream. - Return a success message indicating that the message was produced.
- Take a
- Implement a
consume_message
function inapp.py
to consume messages from a Redis Stream and store them in theStream
model. The function should:- Take a
channel_id
as input. - Use the
redis
client to consume messages from the Redis Stream. - Store each message in the
Stream
model. - Return a success message indicating that the messages were consumed.
- Take a
Part 4: Integrating Pub/Sub and Stream (2 hours) 🤝
- Modify the
publish_message
function to produce a message to a Redis Stream instead of publishing to a channel. - Modify the
subscribe_to_channel
function to consume messages from a Redis Stream and broadcast them to subscribers. - Implement a
broadcast_message
function inapp.py
to broadcast a message to all subscribers of a channel. The function should:- Take a
channel_id
andmessage
as input. - Use the
redis
client to broadcast the message to all subscribers of the channel. - Return a success message indicating that the message was broadcast.
- Take a
Part 5: Testing and Debugging (1 hour) 🧐
- Write unit tests for each function using
pytest
. - Test the following scenarios:
- Publishing a message to a channel and verifying that it is received by subscribers.
- Producing a message to a Redis Stream and verifying that it is stored in the
Stream
model. - Consuming messages from a Redis Stream and verifying that they are broadcast to subscribers.
- Debug and fix any issues that arise during testing.
Estimated Time: 8 hours
Deliverables:
- A working real-time chat application using Redis Pub/Sub and Stream.
- A
README.md
file with instructions on how to run the application. - A
requirements.txt
file with the required Python packages. - Unit tests for each function using
pytest
.
Note: This task is designed to take around 8 hours to complete, but you can adjust the scope and complexity to fit your needs. Good luck 💪
Here is a high-level design for the Real-Time Chat App with Redis Pub/Sub and Stream:
Components:
- Client: A web-based client that allows users to create channels, send messages, and subscribe to channels.
- Server: A Python-based server that handles incoming requests from clients, publishes messages to Redis Pub/Sub, and produces messages to Redis Stream.
- Redis: A Redis instance that stores channel information, user subscriptions, and message data.
Architecture:
+---------------+
| Client |
+---------------+
|
| (HTTP Requests)
v
+---------------+
| Server |
+---------------+
|
| (Redis Pub/Sub)
v
+---------------+
| Redis |
+---------------+
|
| (Channel Info)
| (User Subscriptions)
| (Message Data)
v
+---------------+
| Redis Stream |
+---------------+
Server Components:
- Channel Manager: Responsible for creating, updating, and deleting channels.
- Subscription Manager: Responsible for managing user subscriptions to channels.
- Message Publisher: Responsible for publishing messages to Redis Pub/Sub.
- Message Producer: Responsible for producing messages to Redis Stream.
- Message Consumer: Responsible for consuming messages from Redis Stream and broadcasting them to subscribers.
Redis Data Structures:
- Channels: A Redis Hash that stores channel information, including channel ID, name, and description.
- User Subscriptions: A Redis Set that stores user subscriptions to channels, including user ID and channel ID.
- Message Data: A Redis Stream that stores message data, including message ID, channel ID, message text, and timestamp.
Flow:
- A client sends a request to create a channel to the server.
- The server creates a new channel and stores it in Redis.
- A client sends a request to subscribe to a channel to the server.
- The server adds the user to the channel's subscription list in Redis.
- A client sends a message to a channel to the server.
- The server publishes the message to Redis Pub/Sub.
- The server produces the message to Redis Stream.
- The message consumer consumes the message from Redis Stream and broadcasts it to all subscribers of the channel.
Benefits:
- Real-time messaging: Redis Pub/Sub and Stream enable real-time messaging between clients.
- Scalability: Redis Stream allows for horizontal scaling and high-throughput message processing.
- Persistence: Redis Stream provides persistence for message data, ensuring that messages are not lost in case of failures.
This design provides a basic architecture for a real-time chat app using Redis Pub/Sub and Stream. You can modify and extend it to fit your specific requirements.
Here is a design for a Terminal UI for the Real-Time Chat App with Redis Pub/Sub and Stream:
Terminal UI Components:
- Header: Displays the app name and version.
- Channel List: Displays a list of available channels.
- Message Input: Allows users to input and send messages.
- Message Log: Displays a log of messages received from the channel.
- Status Bar: Displays the user's current channel and subscription status.
Terminal UI Layout:
+---------------------------------------+
| Header |
+---------------------------------------+
| App Name: Real-Time Chat App |
| Version: 1.0 |
+---------------------------------------+
| Channel List |
+---------------------------------------+
| Channel 1: General |
| Channel 2: Development |
| Channel 3: Design |
+---------------------------------------+
| Message Input |
+---------------------------------------+
| > Type a message... |
+---------------------------------------+
| Message Log |
+---------------------------------------+
| [10:00] User1: Hello |
| [10:01] User2: Hi |
| [10:02] User3: Hey |
+---------------------------------------+
| Status Bar |
+---------------------------------------+
| Current Channel: General |
| Subscribed: Yes |
+---------------------------------------+
Terminal UI Commands:
- /join <channel_name>: Join a channel and subscribe to its messages.
- /leave: Leave the current channel and unsubscribe from its messages.
- /channels: List all available channels.
- /send : Send a message to the current channel.
- /quit: Quit the app.
Terminal UI Features:
- Real-time messaging: Messages are displayed in real-time as they are received from the channel.
- Channel switching: Users can switch between channels using the
/join
command. - Message input: Users can input and send messages using the message input field.
- Message log: The message log displays a history of messages received from the channel.
- Status bar: The status bar displays the user's current channel and subscription status.
Color Scheme:
- Header: #333 (dark gray)
- Channel List: #666 (medium gray)
- Message Input: #999 (light gray)
- Message Log: #CCC (light gray)
- Status Bar: #666 (medium gray)
Font:
- Monospace font: Consolas or Courier New