Implement an asyncio-based chat server
Async Chat Server (Python asyncio)
Implement a TCP chat server using Python's asyncio library.
Part 1: Single Global Room
Your server must listen on a specified port and support multiple concurrent clients.
Protocol (line-based text):
- Upon client connection, the server prompts for a username (single line input).
- After successful login, any subsequent line sent by a client is treated as a chat message.
- The server must broadcast each message to all other connected clients in the format:
<username>: <message>.
Behavior Requirements:
- A client should not receive its own message back.
- Handle client disconnects gracefully, ensuring no server crashes and proper resource cleanup.
- The server must remain operational and continue serving other clients even if one client misbehaves (e.g., disconnects mid-line or sends malformed data).
Part 2: Basic Chat Commands
Extend the server protocol to support the following commands:
/join <room>: Allows a user to join a named room. If the room does not exist, it should be created.- Each user can only be in exactly one room at a time.
- After joining a room, messages should only be broadcast to users within the same room.
/who: Returns a list of usernames currently present in the user’s current room./quit: Disconnects the client from the server.
Constraints and Assumptions:
- The server should be designed to handle up to approximately 1,000 concurrent connections.
- Only the Python standard library (e.g.,
asyncio,socket) is permitted. No external dependencies. - Prioritize clear concurrency handling and correctness over implementing advanced features (e.g., no message persistence or history is required).
AI interview prep
Core dilemma
Managing concurrent client connections and their dynamic state (username, current room) efficiently and correctly within a single-threaded event loop, ensuring reliable message delivery to the right recipients while handling I/O operations and client lifecycle events without blocking.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement an asyncio-based chat server
Async Chat Server (Python asyncio)
Implement a TCP chat server using Python's asyncio library.
Part 1: Single Global Room
Your server must listen on a specified port and support multiple concurrent clients.
Protocol (line-based text):
- Upon client connection, the server prompts for a username (single line input).
- After successful login, any subsequent line sent by a client is treated as a chat message.
- The server must broadcast each message to all other connected clients in the format:
<username>: <message>.
Behavior Requirements:
- A client should not receive its own message back.
- Handle client disconnects gracefully, ensuring no server crashes and proper resource cleanup.
- The server must remain operational and continue serving other clients even if one client misbehaves (e.g., disconnects mid-line or sends malformed data).
Part 2: Basic Chat Commands
Extend the server protocol to support the following commands:
/join <room>: Allows a user to join a named room. If the room does not exist, it should be created.- Each user can only be in exactly one room at a time.
- After joining a room, messages should only be broadcast to users within the same room.
/who: Returns a list of usernames currently present in the user’s current room./quit: Disconnects the client from the server.
Constraints and Assumptions:
- The server should be designed to handle up to approximately 1,000 concurrent connections.
- Only the Python standard library (e.g.,
asyncio,socket) is permitted. No external dependencies. - Prioritize clear concurrency handling and correctness over implementing advanced features (e.g., no message persistence or history is required).