Real-time File Monitoring in Containers
Cloudflare has introduced real-time file watching capabilities to Sandboxes, enabling developers to monitor filesystem changes as they happen inside containers. The new sandbox.watch() method returns a Server-Sent Events stream backed by native inotify, providing a low-level, efficient mechanism for tracking file modifications.
API and Usage
The sandbox.watch(path, options) method accepts a directory path and optional configuration:
- Path: The directory to monitor
- Recursive: Watch subdirectories (defaults to
false) - Include: Glob patterns to filter events (omit to receive all events)
Each event includes a type field (create, modify, delete, or move) and the affected path. Move events additionally include a from field with the original path.
Integration Patterns
Developers can consume the stream in two ways:
Browser streaming: Return the stream directly as a Response with Content-Type: text/event-stream to send real-time updates to clients.
Server-side consumption: Use the parseSSEStream utility to iterate over events within a Worker without forwarding them to clients, enabling programmatic handling of filesystem changes.
Getting Started
Update to the latest version with npm i @cloudflare/sandbox@latest. Full API documentation is available in the Sandbox file watching reference.