2.7 KiB
WebSocket Setup for Booking Status
Current Implementation
The booking status feature uses WebSocket (Socket.IO) for real-time updates. However, due to CORS restrictions, the WebSocket connection cannot directly connect from the browser to the Python API.
Production Setup Options
Option 1: Reverse Proxy (Recommended)
Configure your web server (nginx/Apache) to proxy WebSocket connections:
# Example nginx configuration
location /socket.io/ {
proxy_pass http://python-api-server:5000/socket.io/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
With this setup, the WebSocket connects to the same origin as your Next.js app, and the web server proxies it to the Python API.
Option 2: Configure CORS on Python API
Add CORS support for WebSocket connections in your Flask-SocketIO setup:
from flask_socketio import SocketIO
socketio = SocketIO(
app,
cors_allowed_origins=["https://playchoo.com", "https://www.playchoo.com"],
async_mode='threading'
)
Then set the environment variable:
NEXT_PUBLIC_WEBSOCKET_URL=https://api.playchoo.com
Option 3: Next.js API Route Proxy (Complex)
Create a custom Next.js API route that proxies WebSocket connections. This is complex because Next.js API routes don't natively support WebSocket upgrades.
Development Setup
For local development, you have several options:
-
Run Python API with CORS enabled for localhost:
socketio = SocketIO(app, cors_allowed_origins=["http://localhost:3000"])Then set:
NEXT_PUBLIC_WEBSOCKET_URL=http://localhost:5000 -
Use a proxy tool like
http-proxy-middlewarein Next.js -
Disable WebSocket feature in development by not setting a booking_id
Current Status
The WebSocket code is ready but requires infrastructure configuration to work properly. The booking feature will fall back to the traditional response model if WebSocket is not available.
Environment Variables
NEXT_PUBLIC_WEBSOCKET_URL:- Leave empty to use same origin (requires reverse proxy)
- Set to
https://api.playchoo.comif CORS is configured on Python API - Set to
http://localhost:5000for local development with CORS enabled
Testing
To test if WebSocket is working:
- Open browser developer tools
- Go to Network tab and filter by WS (WebSocket)
- Make a booking with
async=trueparameter - You should see a WebSocket connection to
/socket.io/ - Check the Messages tab to see the real-time updates