# Hooks

> Hear what happens on the server and in the player, and how far to trust each event.

Page: https://songstress.app/docs/develop/hooks/

A hook tells your extension what happened. Nothing waits for a listener, and no hook can refuse or delay anything.
Events reach listeners through a bounded queue, never in playback's path: under a flood, events are dropped rather
than the music made to wait.

A hook is delivered only when the manifest lists it under `hooks.server` or `hooks.client`, with the entry
(`ENTRY_REQUIRED`) and the permission it needs (`PERMISSION_REQUIRED`):

```json title="songstress.json"
"entry": { "server": "dist/server.js" },
"hooks": { "server": ["onScrobble"] },
"permissions": { "listening.read": { "reason": "Hear what you play, to send it on." } }
```

A listener's first argument is the thing itself, and its second what is known of the moment:

```ts title="src/server.ts"
import { songstress } from 'songstress/server'

songstress.hooks.onScrobble(async (song, play) => {
  console.log(`${play.userId} heard "${song.title}" at ${play.playedAt}`)
})
```

## Server hooks [#server-hooks]

They see every app of every member who granted the extension their listening, phones included. A play started from
a car, or from a watch that controls the phone, goes through the phone's player and is reported like any other.

| Hook | Fires when | Permission | How far to trust it |
| --- | --- | --- | --- |
| `onPlay` | A member started a song: their app reported it, about a second in, once for each song | `listening.read` | Live only. A play that starts offline is never reported, a locked Android phone may report late or not at all, a watch playing on its own reports none, and an audition of a search result reports nothing. Use it for "now playing" |
| `onScrobble` | A play counted: half the song, or four minutes of it, was heard | `listening.read` | Reliable. A play made offline waits on the device and arrives later, with the time it really happened (`playedAt`) |
| `onFavorite` | A member marked a song, an album or an artist as a favourite, or took the mark away | `listening.read` | |
| `onPlaylistChange` | A public playlist was created, changed or deleted | `library.read` | Playlists their owners keep private aren't reported |
| `onLibraryChange` | Songs came into the library, changed or left it | `library.read` | Ids only, in batches |
| `onScanComplete` | A scan of the library finished | `library.read` | |

A server delivers `onPlay` at most once: an event it can't hand over at once (a flood, or its extension host
restarting) is dropped, never told late. It delivers `onScrobble` at least once, in the order plays were counted,
going back a week at most. A play is told again when the server stopped while delivering it, or when your code
crashed in it. So a listener that keeps or sends plays treats one it has already seen as done: a play is its
member's `userId` with its `playedAt`.

## Client hooks [#client-hooks]

They follow the player of the one device the code runs on, while its app is open. In the background client code is
stopped, and when the app comes back it is told the current state. What must not be missed belongs in a server hook.

| Hook | Fires when | Permission |
| --- | --- | --- |
| `onPlay` | Playback started, or went on after a pause | `listening.read` |
| `onPause` | Playback paused | `listening.read` |
| `onTrackChange` | Another song became the current one | `listening.read` |
| `onSeek` | The listener moved to another place in the song | `listening.read` |
| `onVolumeChange` | The volume changed | None |
| `onQueueChange` | Songs were added to the queue, removed or moved | `listening.read` |
| `onProgress` | Playback moved on: at most once a second, while a song plays | `listening.read` |

`onPlay` is the one hook both entries have, and they don't mean the same: the server's fires once for each song a
member starts, the client's each time this device's player starts or resumes.

## The listeners [#the-listeners]

The server entry's hooks, and what each listener is handed:

The hooks of the server entry. They see every app of every member who
granted the extension their listening, phones included.

| Field | Type | Description |
|---|---|---|
| `onPlay` | `(listener: (song: Song, play: PlayEvent) => Awaitable<void>) => Disposable` | A member started a song: their app reported it, about a second in, once for each song. Live only: a play that starts offline, or on a locked Android phone, may arrive late or never. For "what was played", use onScrobble. Permission `listening.read`. |
| `onScrobble` | `(listener: (song: Song, play: ScrobbleEvent) => Awaitable<void>) => Disposable` | A play counted: half the song or four minutes of it was heard. Reliable: a play made offline waits on the device and arrives later, with the time it really happened. Permission `listening.read`. |
| `onFavorite` | `(listener: (item: Favorite, event: FavoriteEvent) => Awaitable<void>) => Disposable` | A member marked a song, an album or an artist as a favourite, or took the mark away. Permission `listening.read`. |
| `onPlaylistChange` | `(listener: (playlist: Playlist, event: PlaylistChangeEvent) => Awaitable<void>) => Disposable` | A public playlist was created, changed or deleted. Permission `library.read`. |
| `onLibraryChange` | `(listener: (change: LibraryChange) => Awaitable<void>) => Disposable` | Songs came into the library, changed or left it. Permission `library.read`. |
| `onScanComplete` | `(listener: (scan: ScanSummary) => Awaitable<void>) => Disposable` | A scan of the library finished. Permission `library.read`. |

A play as the server heard of it.

| Field | Type | Description |
|---|---|---|
| `userId` | `string` | The member who is listening. |
| `startedAt` | `string` | When the song started, as an ISO 8601 time. |
| `client?` | `string \| undefined` | The app that reported it, when it said its name. |

The client entry's:

The hooks of the client entry. They follow the player of the one device
the code runs on, live, while its app is open and in front: client code is
stopped in the background and told the current state when it comes back.
All but onVolumeChange need the permission `listening.read`.

| Field | Type | Description |
|---|---|---|
| `onPlay` | `(listener: (song: Song, state: PlayerState) => Awaitable<void>) => Disposable` | Playback started or went on after a pause. |
| `onPause` | `(listener: (song: Song, state: PlayerState) => Awaitable<void>) => Disposable` | Playback paused. |
| `onTrackChange` | `(listener: (song: Song \| null, state: PlayerState) => Awaitable<void>) => Disposable` | Another song became the current one; null when the player emptied. |
| `onSeek` | `(listener: (positionS: number, state: PlayerState) => Awaitable<void>) => Disposable` | The listener moved to another place in the song. |
| `onVolumeChange` | `(listener: (volume: number) => Awaitable<void>) => Disposable` | The volume changed; from 0 to 1. No permission. |
| `onQueueChange` | `(listener: (queue: QueueState) => Awaitable<void>) => Disposable` | Songs were added to the queue, removed from it or moved. |
| `onProgress` | `(listener: (positionS: number, state: PlayerState) => Awaitable<void>) => Disposable` | Playback moved on: at most once a second, and only while a song plays. |

## Disposing [#disposing]

Every `on…` returns an object with `dispose()`, which takes the listener away. Everything is disposed for you when
the extension stops.

<Callout title="A scrobbler">
To report plays to a service, declare a `scrobble` provider instead of listening to `onScrobble`: Songstress then
hands it each play, and tries again itself when the service fails. See [Providers](/docs/develop/providers/).
</Callout>
