Minimal WebSocket chat demo.
- Python 3.12+
- Postgres running locally with database
chatapp
- Install dependencies:
pip install -r requirements.txt
- websockets library (
pip install -r requirements.txt
)
-
Install dependencies:
pip install -r requirements.txt
-
Run the server:
python chat_server.py
The static file server listens on http://localhost:8000, the auth and conversation API on http://localhost:8001, The HTTP server listens on http://localhost:8000 and the WebSocket server on ws://localhost:6789.
-
Open
http://localhost:8000/index.html
in two browser windows or machines and start chatting.
Messages from one browser window will appear in the other in real time.
Create a user account:
curl -X POST http://localhost:8001/signup \
-H 'Content-Type: application/json' \
-d '{"email": "user@example.com", "password": "secret"}'
Log in with the created account:
curl -X POST http://localhost:8001/login \
-H 'Content-Type: application/json' \
-d '{"email": "user@example.com", "password": "secret"}'
The login response includes a JSON token field which can be used for authenticated requests.
Create a conversation between users:
curl -X POST http://localhost:8001/conversations \
-H "Authorization: Bearer <TOKEN>" \
-H 'Content-Type: application/json' \
-d '{"members": ["user@example.com", "friend@example.com"]}'
Connect via WebSocket using the returned conversation id:
ws://localhost:6789?token=<TOKEN>&conv=<ID>