WebSockets are a communication protocol that provides a full - duplex, long - lived connection between a client and a server. It starts with an HTTP handshake and then upgrades to a WebSocket connection. Once the connection is established, both the client and the server can send data to each other without the need for repeated handshakes.
fastapi
and uvicorn
installed. You can install them using pip install fastapi uvicorn
.Create a new Python file, for example, main.py
:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
To run the application, use the following command in the terminal:
uvicorn main:app --reload
Now you can access the application at http://127.0.0.1:8000
.
from fastapi import FastAPI, WebSocket
app = FastAPI()
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
while True:
data = await websocket.receive_text()
await websocket.send_text(f"Message text was: {data}")
In this example:
/ws
.await websocket.accept()
.await websocket.receive_text()
and then send a response back to the client.You can use JavaScript in the browser to connect to the WebSocket server:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<title>WebSocket Client</title>
</head>
<body>
<input type="text" id="messageInput" placeholder="Type a message">
<button onclick="sendMessage()">Send</button>
<div id="messages"></div>
<script>
const socket = new WebSocket('ws://127.0.0.1:8000/ws');
socket.onmessage = function (event) {
const messages = document.getElementById('messages');
const newMessage = document.createElement('p');
newMessage.textContent = event.data;
messages.appendChild(newMessage);
};
function sendMessage() {
const input = document.getElementById('messageInput');
const message = input.value;
socket.send(message);
input.value = '';
}
</script>
</body>
</html>
This HTML file creates a simple WebSocket client. When the user types a message and clicks the “Send” button, the message is sent to the server, and the server’s response is displayed on the page.
In the WebSocket endpoint, it’s important to handle connection errors gracefully. For example:
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
try:
while True:
data = await websocket.receive_text()
await websocket.send_text(f"Message text was: {data}")
except Exception as e:
print(f"WebSocket error: {e}")
finally:
await websocket.close()
This code catches any exceptions that occur during the WebSocket communication and closes the connection properly.
In a chat application, you may want to broadcast messages to all connected clients. Here is an example:
from fastapi import FastAPI, WebSocket
from typing import List
app = FastAPI()
connected_clients: List[WebSocket] = []
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
connected_clients.append(websocket)
try:
while True:
data = await websocket.receive_text()
for client in connected_clients:
if client != websocket:
await client.send_text(f"New message: {data}")
except Exception as e:
print(f"WebSocket error: {e}")
finally:
connected_clients.remove(websocket)
await websocket.close()
This code maintains a list of all connected clients. When a client sends a message, the message is broadcast to all other connected clients.
Origin
header.asyncio
, so make sure to use await
appropriately.FastAPI provides a simple and efficient way to set up WebSockets for real - time communication. By understanding the basic concepts, following common practices, and implementing best practices, you can build robust and scalable real - time applications. Whether it’s a chat application, a live dashboard, or a collaborative tool, WebSockets in FastAPI can help you achieve seamless real - time communication between the client and the server.
asyncio
Documentation