Skip to Content
Documentation

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):

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:

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

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.

HookFires whenPermissionHow far to trust it
onPlayA member started a song: their app reported it, about a second in, once for each songlistening.readLive 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"
onScrobbleA play counted: half the song, or four minutes of it, was heardlistening.readReliable. A play made offline waits on the device and arrives later, with the time it really happened (playedAt)
onFavoriteA member marked a song, an album or an artist as a favourite, or took the mark awaylistening.read
onPlaylistChangeA public playlist was created, changed or deletedlibrary.readPlaylists their owners keep private aren't reported
onLibraryChangeSongs came into the library, changed or left itlibrary.readIds only, in batches
onScanCompleteA scan of the library finishedlibrary.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.

HookFires whenPermission
onPlayPlayback started, or went on after a pauselistening.read
onPausePlayback pausedlistening.read
onTrackChangeAnother song became the current onelistening.read
onSeekThe listener moved to another place in the songlistening.read
onVolumeChangeThe volume changedNone
onQueueChangeSongs were added to the queue, removed or movedlistening.read
onProgressPlayback moved on: at most once a second, while a song playslistening.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.

On this page