Skip to main content

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

The main WebSocket endpoint for REST-style operations including subscriptions, CRUD operations, and heartbeat.

GraphQL WebSocket

WebSocket endpoint for GraphQL subscriptions and operations.
Use wss:// for secure connections (HTTPS sites) and ws:// for local development.

Configuration

Enable and configure WebSocket connections using environment variables:

Enable WebSockets

This is the master switch for all WebSocket functionality.

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.
Use case: Public data that doesn’t require authentication. Example connection:
Clients authenticate after establishing the WebSocket connection by sending an authentication message.
Use case: Most applications where you want secure WebSocket connections. Example connection:

Strict Mode

Clients must authenticate before the WebSocket connection is established, using URL parameters or headers.
Use case: High-security environments where unauthenticated connections should be rejected immediately. Example connection:

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

When WEBSOCKETS_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:
When rate limited, you’ll receive an error response:

Token Expiration

WebSocket connections automatically handle token expiration:
  1. When your access token is about to expire, the server sends an error
  2. You have a grace period to re-authenticate (default: 10 seconds)
  3. Send a new auth message with valid credentials
  4. 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

Always include a uid field in your messages to track responses:
Implement automatic reconnection logic for production:
Always validate incoming messages before using them:
Prefer handshake authentication over strict mode for better security:
Always close WebSocket connections when done:

Troubleshooting

Ensure WebSockets are enabled:
Check your authentication mode and credentials:
  • Verify the WEBSOCKETS_REST_AUTH setting
  • Ensure your access token is valid
  • Check that the user has the required permissions
Increase the authentication timeout:
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