The Ocha API includes a WebSocket server that runs on the same port as the HTTP API. This lets clients receive real-time push events — for example, when an administrator creates a new product — without polling REST endpoints. The WebSocket server is built with theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/floriansalvi/HEIG-VD_Ocha-api/llms.txt
Use this file to discover all available pages before exploring further.
ws library and is attached directly to the Node.js HTTP server.
Server details
The WebSocket server shares the HTTP server’s port. The port is read from thePORT environment variable (default 5001 as documented in the environment reference).
| Detail | Value |
|---|---|
| Protocol | ws:// (upgrade from HTTP) |
| Default port | 5001 |
| Environment var | PORT |
| Connection URL | ws://localhost:5001 |
Because the WebSocket server shares a port with the HTTP API, no additional firewall rules or port mappings are required beyond what is already open for the REST API.
Connection handshake
When a client successfully connects, the server immediately sends a welcome message:JSON.parse() after receiving.
Broadcast events
The server can push events to all connected clients. Currently one event type is defined:new_product
Broadcast whenever an administrator creates a new product via POST /api/v1/products. The payload contains the full product document as persisted to the database.
JavaScript client example
The example below connects to the WebSocket server and logs all incoming events to the console. In a browser or Node.js environment, use the built-inWebSocket global (or the ws package for Node.js).
Open a WebSocket connection
Create a
WebSocket instance pointing to ws://localhost:5001 (replace the host and port with your server’s address in production).Receive the welcome message
The server sends
{ "message": "Connecté au WebSocket" } immediately after the connection is accepted.Listen for broadcast events
Parse incoming messages as JSON and branch on the
type field to handle each event kind.Testing with Postman
Next steps
Create a product
Trigger a
new_product WebSocket broadcast by creating a product as an admin.Roles and permissions
Learn which endpoints require admin access, including product creation.