puter.threads.subscribe()
Subscribes to events on a specific thread.
puter.threads.subscribe(uuid, options, callback);
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 addededit
: Emitted when this thread is editeddelete
: Emitted when this thread is deletedchild-edit
: Emitted when a direct child thread is editedchild-delete
: Emitted when a direct child thread is deletedcallback
(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)
}
}
A subscription object with methods:
{
unsubscribe: function // Call this function to stop receiving events
}
The current actor needs the appropriate read permissions for the thread to subscribe to its events.
<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>