puter.threads.subscribe()


Subscribes to events on a specific thread.

Syntax

puter.threads.subscribe(uuid, options, callback);

Parameters

uuid (string) (required)

The UUID of the thread to subscribe to.

options (object) (optional)

An object containing subscription options:

  • events (array) (optional): Array of event names to subscribe to. Default is all events. Possible events:
    • post: Emitted when a child thread is added
    • edit: Emitted when this thread is edited
    • delete: Emitted when this thread is deleted
    • child-edit: Emitted when a direct child thread is edited
    • child-delete: Emitted when a direct child thread is deleted

callback (function) (required)

Function to be called when a subscribed event occurs. The callback receives an event object:

{
  type: string,        // The type of event ('post', 'edit', 'delete', 'child-edit', 'child-delete')
  thread: {            // The thread that triggered the event
    uuid: string,      // The UUID of the thread
    content: any,      // The content of the thread
    parent_uuid: string // The UUID of the parent thread (if applicable)
  }
}

Return value

A subscription object with methods:

{
  unsubscribe: function // Call this function to stop receiving events
}

Permissions Required

The current actor needs the appropriate read permissions for the thread to subscribe to its events.

Example

<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        // Subscribe to all events on a thread
        const subscription = puter.threads.subscribe(
            '550e8400-e29b-41d4-a716-446655440000',
            { events: ['post', 'edit', 'delete', 'child-edit', 'child-delete'] },
            function(event) {
                console.log("Event received:", event.type);
                console.log("Thread data:", event.thread);
            }
        );
    </script>
</body>
</html>