Hone logo
Hone
Problems

Real-Time Chat Application with WebSockets

This challenge tasks you with building a simple real-time chat application using Python and the websockets library. WebSockets enable persistent, bidirectional communication between a client and a server, making them ideal for applications requiring real-time updates, such as chat applications, online games, and collaborative tools. This exercise will solidify your understanding of WebSocket concepts and their implementation in Python.

Problem Description

You are to implement a basic WebSocket server and client for a chat application. The server should accept incoming WebSocket connections from clients. When a client sends a message, the server should broadcast that message to all connected clients. The client should connect to the server, send a message, and receive messages from the server and other clients.

What needs to be achieved:

  • Server: A WebSocket server that listens for incoming connections, receives messages from clients, and broadcasts those messages to all connected clients.
  • Client: A WebSocket client that connects to the server, sends a message, and receives messages from the server and other clients.

Key Requirements:

  • Use the websockets library for both the server and client.
  • The server should maintain a list of connected clients.
  • Messages should be simple strings.
  • The client should display received messages.
  • Error handling should be included to gracefully handle connection errors and disconnections.

Expected Behavior:

  1. The server starts listening for connections on a specified port (e.g., 8765).
  2. The client connects to the server.
  3. The client sends a message (e.g., "Hello, server!").
  4. The server receives the message and broadcasts it to all connected clients.
  5. All connected clients (including the sender) receive the message and display it.
  6. If a client disconnects, the server removes it from the list of connected clients.

Edge Cases to Consider:

  • Multiple clients connecting simultaneously.
  • Clients disconnecting unexpectedly.
  • Handling invalid messages (though simple strings are expected, consider potential future expansion).
  • Server errors (e.g., port already in use).

Examples

Example 1:

Input (Server):  Listening on port 8765
Input (Client 1): Connects to server, sends "Hi everyone!"
Input (Client 2): Connects to server
Output (Client 1): Receives "Hi everyone!"
Output (Client 2): Receives "Hi everyone!"
Explanation: Client 1 sends a message, which is broadcast to both Client 1 and Client 2.

Example 2:

Input (Server): Listening on port 8765
Input (Client 1): Connects to server, sends "This is a test."
Input (Client 2): Connects to server
Input (Client 1): Disconnects
Output (Client 2): Receives "This is a test."
Explanation: Client 1 sends a message, Client 2 receives it. Client 1 disconnects, but Client 2 still functions.

Example 3: (Error Handling)

Input (Client): Attempts to connect to port 8765, but the server is not running.
Output (Client):  Prints an error message indicating connection failure.
Explanation: The client gracefully handles the inability to connect to the server.

Constraints

  • The server should listen on port 8765.
  • Messages should be strings.
  • The client should be able to send and receive messages.
  • The solution must use the websockets library.
  • The code should be well-structured and readable.
  • The server should be able to handle at least 5 concurrent client connections without significant performance degradation.

Notes

  • You'll need to install the websockets library: pip install websockets.
  • Consider using asynchronous programming (async and await) for efficient handling of multiple connections.
  • Think about how to manage the list of connected clients on the server. A simple list or set will suffice for this basic implementation.
  • Focus on the core WebSocket functionality – message sending and receiving. Advanced features like user authentication or message history are not required for this challenge.
  • Start by implementing the server first, then the client. This will make debugging easier.
  • Remember to handle potential exceptions, such as connection errors and disconnections.
Loading editor...
python