Hooks
Hear what happens on the server and in the player, and how far to trust each event.
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):
"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:
import { songstress } from 'songstress/server'
songstress.hooks.onScrobble(async (song, play) => {
console.log(`${play.userId} heard "${song.title}" at ${play.playedAt}`)
})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
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 server entry's hooks, and what each listener is handed:
Prop
Type
Prop
Type
The client entry's:
Prop
Type
Disposing
Every on… returns an object with dispose(), which takes the listener away. Everything is disposed for you when
the extension stops.
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.