Skip to main content

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:
Authentication Modes:
  • strict: Clients must authenticate before connecting (via URL or headers)
  • handshake: Clients authenticate during connection initialization
  • public: 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:
This subscription fires when items are created, updated, or deleted.

Basic Subscription

Subscribe to all mutations on a collection:
Response Fields:
  • key: The ID of the affected item
  • event: The mutation type (create, update, or delete)
  • data: The item data (null for delete events)

Filter by Event Type

Subscribe only to specific event types:
Available Events:
  • create: Item was created
  • update: Item was updated
  • delete: 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:
Variables:

Connection Lifecycle

Connection States

  1. Connecting: Establishing WebSocket connection
  2. Connected: Connection established, can subscribe
  3. Reconnecting: Lost connection, attempting to reconnect
  4. 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

Request only the fields you need to reduce bandwidth:
If you only need specific events, filter them:
Always implement reconnection logic for production:
Always clean up subscriptions when components unmount:
Remember that delete events don’t include data:

Troubleshooting

Ensure WebSockets are enabled:
Check your authentication mode and token:
Verify:
  • User has read permissions for the collection
  • Subscription query syntax is correct
  • WebSocket connection is established
Increase the authentication timeout:

Next Steps

GraphQL Queries

Learn how to query your data

GraphQL Mutations

Create, update, and delete data