This is a Python implementation of a WebSocket server that can be used to test Azure API Management (APIM) WebSocket capabilities. The server provides both REST API endpoints and WebSocket communication.
- Clone the repository
- Create a virtual environment and install dependencies
- Run the client and server with
npm run dev-full
- Start ngrok with
ngrok http 5001
(install ngrok first)
This starts the client and server locally. The server is available via the ngrok URL to the outside world.
Now deploy APIM. You need an Azure subscription.
- Go the the `deploy' folder
- Modify apim.parameters.json:
- Change the wsBackendUrl to
wss://<ngrok-url>/socket.io/
- Change the httpBackendUrl to
https://<ngrok-url>
- Change the wsBackendUrl to
- Run
./deploy-apim.sh
Ensure that client/src/App.js
is configured to use the APIM instance.
// API Management endpoints
const APIM_HTTP_ENDPOINT = 'https://apim-realtime-dashboard.azure-api.net/dashboard';
const APIM_WS_ENDPOINT = 'wss://apim-realtime-dashboard.azure-api.net/dashboard-ws';
const useApim = true;
Replace the https and wss endpoints with the APIM endpoint.
If you want to test the endpoints with api-requests.http
then set the @baseUrl to the same APIM endpoint for the REST API.
- WebSocket server that handles client connections
- REST API endpoints for creating and retrieving updates
- Automatic broadcasting of updates to all connected WebSocket clients
- Health check endpoint
- CORS support
- OpenAPI documentation
- Robust error handling for WebSocket connections
- Python 3.7 or higher
- pip (Python package manager)
- Install the required dependencies:
pip install -r requirements.txt
python server.py
The server will start on port 5001 by default. If that port is already in use, it will automatically try the next port.
GET /api/updates
- Get all updatesPOST /api/update
- Create a new updateGET /health
- Health check endpointGET /docs
- OpenAPI documentation (Swagger UI)GET /openapi.yaml
- OpenAPI specification
The server provides multiple WebSocket endpoints for flexibility:
- Primary endpoint:
ws://localhost:5001/socket.io/
- Alternative endpoint:
ws://localhost:5001/ws/any-path-here
Both endpoints provide the same functionality, but the alternative endpoint allows any path after /ws/
, which can be useful for testing different routing configurations in APIM.
A simple HTML-based WebSocket client is included for testing in the browser:
- Open the
websocket-test-client.html
file in your browser - The client will automatically connect to
ws://localhost:5001/socket.io/
- You can send messages, heartbeats, and create updates from the UI
Client is in client/src
. Run the client with npm run dev-full
.
To test your APIM instance with this server:
-
Start the server locally:
python server.py
-
Configure your APIM instance to proxy WebSocket connections to your local server.
- You may need to use a tool like ngrok to expose your local server to the internet.
- Example ngrok command:
ngrok http 5001
-
Connect the client to your APIM instance:
Configure the client to connect to your APIM instance.
{
"type": "heartbeat",
"timestamp": 1621234567890
}
{
"type": "initial-updates",
"data": [
{
"id": 1621234567890,
"message": "System is running normally",
"type": "info",
"title": "System Status",
"timestamp": "2023-07-01T12:00:00.000Z"
}
]
}
{
"type": "new-update",
"data": {
"id": 1621234567890,
"message": "System is running normally",
"type": "info",
"title": "System Status",
"timestamp": "2023-07-01T12:00:00.000Z"
}
}
- If you see connection errors, check that your APIM instance is properly configured for WebSockets.
- Ensure that your APIM policies allow WebSocket upgrade requests.
- Check that the WebSocket protocol is enabled in your APIM instance.
- Verify that your backend service (this server) is accessible from your APIM instance.
- If using the React client, make sure you've replaced the Socket.IO client with the WebSocket client.
- If you're seeing errors with Socket.IO clients, use one of the native WebSocket clients provided.