Introduction
Directus supports GraphQL subscriptions for real-time updates when items are created, updated, or deleted in your collections. Subscriptions use WebSockets to maintain a persistent connection and push updates to clients as they happen.GraphQL subscriptions are only available for item collections on the
/graphql endpoint, not for system collections.WebSocket Endpoint
Subscriptions connect via WebSocket:Use
wss:// for secure connections (HTTPS sites) and ws:// for local development.Configuration
Enable GraphQL subscriptions via WebSockets with environment variables:Enable GraphQL Subscriptions
Authentication Modes
Configure how clients authenticate:strict: Clients must authenticate before connecting (via URL or headers)handshake: Clients authenticate during connection initializationpublic: No authentication required (for public data only)
Authentication Timeout
Set timeout for handshake authentication:Custom WebSocket Path
Subscription Types
For each collection, Directus generates a subscription:Basic Subscription
Subscribe to all mutations on a collection:key: The ID of the affected itemevent: The mutation type (create,update, ordelete)data: The item data (null for delete events)
Filter by Event Type
Subscribe only to specific event types:create: Item was createdupdate: Item was updateddelete: Item was deleted
Subscribe to Specific Fields
Request only the fields you need:Subscribe with Relationships
Include related data in subscription updates:Authentication
Handshake Authentication
Authenticate during connection initialization:Strict Authentication
Authenticate via URL parameters:Client Implementation
Using graphql-ws
Using Apollo Client
Using the Directus SDK
Subscription Events
Create Event
Fired when an item is created:Update Event
Fired when an item is updated:Delete Event
Fired when an item is deleted:Delete events don’t include item data, only the
key (ID) of the deleted item.Permission Handling
Subscriptions respect user permissions:- Users only receive updates for items they have read access to
- If permissions change and a user loses access, they won’t receive further updates
- Permission errors are silently ignored (no notification sent)
Using Variables
Use variables to make subscriptions dynamic:Connection Lifecycle
Connection States
- Connecting: Establishing WebSocket connection
- Connected: Connection established, can subscribe
- Reconnecting: Lost connection, attempting to reconnect
- Disconnected: Connection closed
Handling Connection Errors
Auto-Reconnection
Most GraphQL clients handle reconnection automatically. Configure retry behavior:Complete Example
Here’s a full example of using subscriptions in a React application:Best Practices
Only Subscribe to Needed Fields
Only Subscribe to Needed Fields
Request only the fields you need to reduce bandwidth:
Filter by Event Type
Filter by Event Type
If you only need specific events, filter them:
Handle Reconnection
Handle Reconnection
Always implement reconnection logic for production:
Clean Up Subscriptions
Clean Up Subscriptions
Always clean up subscriptions when components unmount:
Handle Delete Events
Handle Delete Events
Remember that delete events don’t include data:
Troubleshooting
Connection Refused
Connection Refused
Ensure WebSockets are enabled:
Authentication Errors
Authentication Errors
Check your authentication mode and token:
No Updates Received
No Updates Received
Verify:
- User has read permissions for the collection
- Subscription query syntax is correct
- WebSocket connection is established
Connection Timeout
Connection Timeout
Increase the authentication timeout:
Next Steps
GraphQL Queries
Learn how to query your data
GraphQL Mutations
Create, update, and delete data