Introduction
WebSocket subscriptions allow you to receive real-time updates when items are created, updated, or deleted in your Directus collections. Subscriptions maintain a persistent connection and push updates to clients instantly as they happen.Prerequisites
Before using subscriptions, ensure:-
WebSockets are enabled:
- You have an active WebSocket connection
- You’re authenticated (if required by your auth mode)
Subscribe to a Collection
To subscribe to updates for a collection, send asubscribe message:
init event confirms the subscription is active.
Subscription Message Format
Subscription Events
Subscriptions emit events when items are created, updated, or deleted:Create Event
Update Event
Delete Event
Delete events return an array of deleted item IDs, not the full item data.
Subscribe with Query Parameters
Control what data you receive by adding query parameters:Select Specific Fields
Apply Filters
Only receive updates for items matching specific criteria:Limit Results
Filter by Event Type
Subscribe only to specific event types:Create Events Only
Update Events Only
Delete Events Only
Subscribe to Specific Item
Monitor changes to a single item:123 is updated or deleted.
Subscribe with Relationships
Include related data in subscription updates:Unsubscribe
Stop receiving updates by sending anunsubscribe message:
uid, all subscriptions for the connection will be removed.
Multiple Subscriptions
You can have multiple active subscriptions on a single connection:uid.
Permission Handling
Subscriptions respect user permissions:- You can only subscribe to collections you have read access to
- You only receive updates for items you can read
- If a filter query requires permissions you don’t have, you’ll get an error
- If permissions change and you lose access, updates stop silently
Complete Example
Here’s a complete example with authentication and subscriptions:Using with React
Example React hook for WebSocket subscriptions:Best Practices
Use Unique Subscription IDs
Use Unique Subscription IDs
Always provide unique
uid values for each subscription to track them independently:Request Only Needed Fields
Request Only Needed Fields
Reduce bandwidth by requesting only the fields you need:
Filter at the Source
Filter at the Source
Use filter queries instead of filtering in the client:
Unsubscribe When Done
Unsubscribe When Done
Always unsubscribe when you no longer need updates:
Handle Reconnection
Handle Reconnection
Re-establish subscriptions after reconnecting:
Troubleshooting
No Updates Received
No Updates Received
Verify:
- The subscription was successful (received
initevent) - You have read permissions for the collection
- Items match your filter criteria
- The WebSocket connection is still active
Permission Errors
Permission Errors
Ensure you have the correct permissions:Check your role’s read permissions for the collection.
Invalid Collection Error
Invalid Collection Error
Make sure the collection exists and is accessible:
Cannot Subscribe to Specific Item
Cannot Subscribe to Specific Item
Some system collections don’t support item-level subscriptions:
Next Steps
WebSocket Overview
Learn about WebSocket connection and authentication
SDK Real-time
Use the Directus SDK for easier WebSocket management
GraphQL Subscriptions
Real-time updates using GraphQL
Query Filters
Learn about filter syntax and query parameters