WebSockets are a communication protocol that provides full-duplex, real-time communication channels over a single TCP connection. Unlike traditional HTTP requests, WebSockets allow for bi-directional, event-driven communication between a client (typically a web browser) and a server.
WebSockets enable:
- Real-time, bi-directional communication
- Persistent connections between client and server
- Low-latency data transfer
- Server-initiated communication to the client
WebSockets are particularly useful for applications that require real-time updates or frequent communication between client and server. Some common use cases include:
- Chat applications
- Live sports updates or stock tickers
- Online gaming
- Collaborative editing tools
- Real-time data visualization
- IoT device communication
This project implements a simple real-time chat application using Python and the websockets
library. It consists of two main components:
- A WebSocket server that handles multiple client connections and broadcasts messages.
- A WebSocket client that can send and receive messages.
- Multiple clients can connect to the server simultaneously
- Messages sent by one client are broadcast to all other connected clients
- Simple command-line interface for sending and receiving messages
Ensure you have Python 3.7 or later installed on your system.
- Clone this repository or download the source files.
- Install the required dependencies:
pip install websockets aioconsole asyncio
-
Start the server:
python server.py
The server will start running on
localhost:8765
. -
In separate terminal windows, run the client for each user:
python client.py
-
Once connected, you can type messages in the client terminal. Press Enter to send a message.
-
Messages will be broadcast to all connected clients.
To test the application:
- Run the server in one terminal window.
- Run multiple instances of the client in separate terminal windows.
- Type messages in each client window and observe that they are received by all other clients.
This is a basic implementation that can be extended with features such as:
- User authentication
- Private messaging
- Persistent chat history
- Web-based front-end
- Message encryption