Overview
The Directus SDK provides real-time functionality through WebSocket connections, allowing you to subscribe to live updates for collections, send custom messages, and handle WebSocket events.Setup
Basic Real-time Client
Create a client with real-time capabilities:- TypeScript
- JavaScript
Authentication Modes
Therealtime() composable supports three authentication modes:
handshake- Authenticate during connection (default, recommended)strict- Pass token in URL query parameterpublic- No authentication (for public data)
Handshake Mode (Recommended)
- TypeScript
- JavaScript
Public Mode
- TypeScript
- JavaScript
Subscriptions
Subscribe to Collection Updates
Subscribe to real-time updates for a collection:- TypeScript
- JavaScript
Subscription Events
Subscriptions emit different event types:init- Initial data when subscription startscreate- New item createdupdate- Existing item updateddelete- Item deleted
- TypeScript
- JavaScript
Unsubscribe
Stop listening to updates:- TypeScript
- JavaScript
WebSocket Events
Listen to WebSocket Events
Handle low-level WebSocket events:- TypeScript
- JavaScript
Send Custom Messages
Send Messages
Send custom messages through the WebSocket:- TypeScript
- JavaScript
Ping/Pong Example
- TypeScript
- JavaScript
Connection Management
Connect
- TypeScript
- JavaScript
Disconnect
- TypeScript
- JavaScript
Check Connection Status
- TypeScript
- JavaScript
Configuration
Advanced Configuration
- TypeScript
- JavaScript
Custom WebSocket URL
- TypeScript
- JavaScript
Full Example
Complete example with authentication and subscriptions:- TypeScript
- JavaScript
Type Signatures
Next Steps
Items Operations
Perform CRUD operations on items
Authentication
Authenticate for protected subscriptions
WebSocket API
Learn about WebSocket protocol details
Flows
Trigger flows from real-time events