Overview
The Invernaderos API uses STOMP (Simple Text Oriented Messaging Protocol) over WebSocket to provide real-time sensor data streaming to connected clients. This setup includes SockJS fallback for browsers that don’t support native WebSocket.WebSocket Endpoints
Endpoint Configuration
The API provides two endpoints:/ws/greenhouse- Primary endpoint with SockJS fallback support for maximum compatibility/ws/greenhouse-native- Native WebSocket endpoint without SockJS overhead
SockJS Fallback: When WebSocket is unavailable, SockJS automatically falls back to HTTP-based transports (XHR streaming, XHR polling, etc.) ensuring connectivity across all environments.
Connection Setup
Install Required Libraries
Add the STOMP.js and SockJS client libraries to your project:
- CDN
- npm
- yarn
Create STOMP Client
Wrap the SockJS socket with a STOMP client:This creates a STOMP protocol layer on top of the WebSocket/SockJS connection.
Complete JavaScript Example
Configuration Details
Message Broker Settings
The API uses an in-memory simple message broker with the following configuration:Destination Prefixes
| Prefix | Purpose | Example |
|---|---|---|
/topic | Broadcast messages (one-to-many) | /topic/greenhouse/messages |
/queue | Point-to-point messages | /queue/notifications |
/app | Application-bound messages | /app/sensor/update |
/user | User-specific messages | /user/alerts |
CORS Configuration
The WebSocket endpoints allow connections from all origins in development:Connection Lifecycle
Connection Establishment
Client initiates SockJS connection → Protocol negotiation → WebSocket upgrade (if available)
Troubleshooting
Connection fails with 404 error
Connection fails with 404 error
Verify the endpoint URL is correct:
- Use
http://protocol (notws://) for SockJS endpoints - Check that the API server is running and accessible
- Verify the port number (default: 8080)
No messages received after connection
No messages received after connection
Ensure you’ve subscribed to the correct topic:Check that sensor data is being published to the topic.
Connection drops frequently
Connection drops frequently
- Check network stability
- Implement reconnection logic with exponential backoff
- Consider using heartbeats to keep connection alive:
CORS errors in browser console
CORS errors in browser console
CORS is configured on the server side. If you encounter CORS issues:
- Verify the server’s
allowedOriginPatternsincludes your domain - Check that your frontend uses the correct protocol (http/https)
- Ensure credentials are not required (or properly configured)
Next Steps
WebSocket Topics
Learn about available topics and message formats
API Reference
Explore WebSocket API endpoints