ClaudeCloudflare Developer PlatformMar 3, 2026, 12:00 AM

Agents - Real-time file watching in Sandboxes

A condensed section focused on the key takeaways first.

Original Post

Quick Digest

Summary

A condensed section focused on the key takeaways first.

claudeenmodel: claude-sonnet-4-20250514

Cloudflare Sandboxes Add Real-time File Watching with sandbox.watch()

Key Points

  • Real-time filesystem watching via sandbox.watch() method
  • Server-Sent Events stream with native inotify support
  • Support for create, modify, delete, and move events

Summary

Cloudflare Sandboxes now support real-time filesystem monitoring through the new sandbox.watch() method. This feature provides Server-Sent Events streams backed by native inotify, enabling Workers to receive filesystem events (create, modify, delete, move) as they occur within containers.

Key Points

  • Real-time monitoring: Watch filesystem changes with native inotify support
  • Event streaming: Returns standard ReadableStream that can be proxied to browser clients or consumed server-side
  • Event types: Supports create, modify, delete, and move operations with path information
  • Flexible filtering: Includes recursive option and glob pattern filtering via include parameter
  • Server-side consumption: Use parseSSEStream() utility to iterate over events within Workers
  • Move event details: Move events include both target path and original from path

Usage Options

  • recursive: Watch subdirectories (default: false)
  • include: Array of glob patterns for event filtering

Installation

npm i @cloudflare/sandbox@latest

Full Translation

Translations

A translation section that keeps the flow of the original article.

claudejamodel: claude-sonnet-4-20250514

Agents - Sandboxesでのリアルタイムファイル監視

Sandboxesでのリアルタイムファイル監視

2026年3月3日

Sandboxesがsandbox.watch()を通じてリアルタイムファイルシステム監視をサポートするようになりました。このメソッドはネイティブinotifyに基づくServer-Sent Eventsストリームを返すため、コンテナ内で発生するcreatemodifydeletemoveイベントをWorkerがリアルタイムで受信できます。

sandbox.watch(path, options)

ディレクトリパスとオプションのフィルターを渡します。返されるストリームは標準的なReadableStreamで、ブラウザクライアントに直接プロキシしたり、サーバーサイドで消費したりできます。

JavaScript

// ブラウザクライアントにイベントをストリーミング
const stream = await sandbox.watch("/workspace/src", {
  recursive: true,
  include: ["*.ts", "*.js"],
});

return new Response(stream, {
  headers: { "Content-Type": "text/event-stream" },
});

TypeScript

// ブラウザクライアントにイベントをストリーミング
const stream = await sandbox.watch("/workspace/src", {
  recursive: true,
  include: ["*.ts", "*.js"],
});

return new Response(stream, {
  headers: { "Content-Type": "text/event-stream" },
});

parseSSEStreamを使用したサーバーサイド消費

parseSSEStreamを使用して、クライアントに転送することなくWorker内でイベントを反復処理できます。

JavaScript

import { parseSSEStream } from "@cloudflare/sandbox";

const stream = await sandbox.watch("/workspace/src", {
  recursive: true
});

for await (const event of parseSSEStream(stream)) {
  console.log(event.type, event.path);
}

TypeScript

import { parseSSEStream } from "@cloudflare/sandbox";
import type { FileWatchSSEEvent } from "@cloudflare/sandbox";

const stream = await sandbox.watch("/workspace/src", {
  recursive: true
});

for await (const event of parseSSEStream<FileWatchSSEEvent>(stream)) {
  console.log(event.type, event.path);
}

各イベントにはtypeフィールド(createmodifydelete、またはmove)と影響を受けたpathが含まれます。moveイベントには元のパスを含むfromフィールドも含まれます。

オプション

オプション説明
recursivebooleanサブディレクトリを監視します。デフォルトはfalseです。
includestring[]イベントをフィルタリングするためのGlobパターン。すべてのイベントを受信するには省略します。

アップグレード

最新バージョンに更新するには:

npm i @cloudflare/sandbox@latest

完全なAPI詳細については、Sandboxファイル監視リファレンスを参照してください。