Introduction
Directus provides WebSocket support for real-time bidirectional communication between clients and the server. WebSockets enable instant data updates, live subscriptions, and interactive operations without the overhead of repeated HTTP requests.WebSocket Endpoints
Directus exposes multiple WebSocket endpoints depending on your configuration:REST WebSocket
GraphQL WebSocket
Use
wss:// for secure connections (HTTPS sites) and ws:// for local development.Configuration
Enable and configure WebSocket connections using environment variables:Enable WebSockets
REST WebSocket Configuration
GraphQL WebSocket Configuration
Heartbeat Configuration
Logs Configuration
Authentication Modes
Directus supports three authentication modes for WebSocket connections:Public Mode
No authentication required. Connections are established with public permissions.Handshake Mode (Recommended)
Clients authenticate after establishing the WebSocket connection by sending an authentication message.Strict Mode
Clients must authenticate before the WebSocket connection is established, using URL parameters or headers.Authentication Methods
When using handshake mode, you can authenticate with different credential types:Access Token
Email and Password
Refresh Token
Message Format
All WebSocket messages use JSON format with the following structure:Client Messages
Server Responses
Error Responses
Connection Lifecycle
1. Establish Connection
2. Authenticate (if using handshake mode)
3. Wait for Authentication Response
4. Send Messages and Subscribe
5. Handle Disconnection
Heartbeat / Ping-Pong
WhenWEBSOCKETS_HEARTBEAT_ENABLED=true, the server sends periodic ping messages to keep connections alive:
Most WebSocket clients handle ping/pong automatically. You typically don’t need to implement this manually unless you’re using a custom client.
Rate Limiting
If rate limiting is enabled for the Directus instance, it also applies to WebSocket messages:Token Expiration
WebSocket connections automatically handle token expiration:- When your access token is about to expire, the server sends an error
- You have a grace period to re-authenticate (default: 10 seconds)
- Send a new auth message with valid credentials
- If you don’t re-authenticate in time, the connection is closed (in non-public mode)
Permission Handling
All WebSocket operations respect user permissions:- Subscriptions only receive updates for items the user can read
- Item operations (create, read, update, delete) are subject to role permissions
- Permission changes take effect immediately for active connections
- Unauthorized operations return error responses
Basic Example
Here’s a complete example of connecting and authenticating:Error Codes
Common WebSocket error codes:Best Practices
Use Unique IDs for Tracking
Use Unique IDs for Tracking
Always include a
uid field in your messages to track responses:Handle Reconnection
Handle Reconnection
Implement automatic reconnection logic for production:
Validate Messages
Validate Messages
Always validate incoming messages before using them:
Use Handshake Mode
Use Handshake Mode
Prefer handshake authentication over strict mode for better security:
Close Connections Properly
Close Connections Properly
Always close WebSocket connections when done:
Troubleshooting
Connection Refused
Connection Refused
Ensure WebSockets are enabled:
Authentication Fails
Authentication Fails
Check your authentication mode and credentials:
- Verify the
WEBSOCKETS_REST_AUTHsetting - Ensure your access token is valid
- Check that the user has the required permissions
Connection Timeout
Connection Timeout
Increase the authentication timeout:
Max Connections Reached
Max Connections Reached
Adjust the connection limit:
Next Steps
WebSocket Subscriptions
Subscribe to real-time collection updates
SDK Real-time
Use the Directus SDK for WebSocket operations
GraphQL Subscriptions
Real-time updates with GraphQL