puter.events.unsubscribe()

Websites Puter Apps Node.js Workers

The Events API is in beta. Event shapes, limits, and behavior may change between releases.

Ends a subscription created with puter.events.onPersistent(). It stops immediately, and any undelivered backlog is dropped.

For a session subscription from puter.events.onLocal(), use subscription.off().

On a website with nobody signed in, it asks the user to sign in first, as other Puter.js calls do. An app running on Puter is always signed in.

Syntax

puter.events.unsubscribe(subId)

Parameters

subId (String) (required)

The subId of the subscription, from onPersistent() or puter.events.list().

Return value

A Promise that resolves when the subscription is gone.

An id this caller doesn't hold (already ended, or created by another app) is reported as not existing, so the call doesn't reveal which subscriptions exist. If it rejects with any other code, the subscription is still running, and so is any handler it runs on this page. It rejects with { message, code }:

code Meaning
subscription_does_not_exist No such subscription, or it isn't this caller's.
auth_canceled Nobody was signed in, and the user closed the sign-in without finishing it.
too_many_requests Over the unsubscribe rate limit — its own, not the subscribe one.
events_disabled Events aren't enabled on this server.
events_failed The server sent a response the SDK couldn't read.

An app can only end subscriptions it created. An account session can end any of them, including ones left by deleted apps. A scoped access token, such as the one in a getReadURL() URL, can't end any, whether the account or an app created it.

Examples

Create a persistent subscription, then end it

<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        (async () => {
            const dir = puter.randName();
            await puter.fs.mkdir(dir);

            const sub = await puter.events.onPersistent({ subject: `fs:${dir}` });
            puter.print(`watching as ${sub.subId}<br>`);

            await puter.events.unsubscribe(sub.subId);
            puter.print('stopped<br>');

            // Ending it twice is refused the same way an unknown id is.
            try {
                await puter.events.unsubscribe(sub.subId);
            } catch (error) {
                puter.print(`second attempt: ${error.code}<br>`);
            }
        })();
    </script>
</body>
</html>