Skip to main content

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:
  1. WebSockets are enabled:
  2. You have an active WebSocket connection
  3. You’re authenticated (if required by your auth mode)

Subscribe to a Collection

To subscribe to updates for a collection, send a subscribe message:
Response:
The 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

You’ll only receive the specified fields in updates.

Apply Filters

Only receive updates for items matching specific criteria:
You’ll only receive updates for published articles in the tech or science categories.

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:
You’ll receive updates only when item 123 is updated or deleted.
You cannot subscribe to specific items in the directus_fields or directus_relations system collections. Subscribe to the entire collection instead.

Subscribe with Relationships

Include related data in subscription updates:
Response with related data:

Unsubscribe

Stop receiving updates by sending an unsubscribe message:
Response:
If you don’t provide a uid, all subscriptions for the connection will be removed.

Multiple Subscriptions

You can have multiple active subscriptions on a single connection:
Each subscription is tracked independently via its 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
Permission error example:

Complete Example

Here’s a complete example with authentication and subscriptions:

Using with React

Example React hook for WebSocket subscriptions:

Best Practices

Always provide unique uid values for each subscription to track them independently:
Reduce bandwidth by requesting only the fields you need:
Use filter queries instead of filtering in the client:
Always unsubscribe when you no longer need updates:
Re-establish subscriptions after reconnecting:

Troubleshooting

Verify:
  • The subscription was successful (received init event)
  • You have read permissions for the collection
  • Items match your filter criteria
  • The WebSocket connection is still active
Ensure you have the correct permissions:
Check your role’s read permissions for the collection.
Make sure the collection exists and is accessible:
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