Webhook callbacks vs longpoll for stream results
Choose how to receive AudD stream recognition events: webhook callbacks when you can host a public HTTPS endpoint, longpoll when you can't.
On this page
We wanted to make the API as simple as possible: you send us the URLs of radio streams, we send you the recognition results in real time. Callbacks are the requests with recognition results our servers will send to your server. Alternatively, you can use longpoll from your client. The method you choose depends upon your setup, but if you can host a public HTTPS endpoint, we recommend receiving the results in callbacks. This article explains how callbacks compare to longpoll, and how you set up and consume them.
TL;DR
When you set a callback URL, our servers send results and notifications to your server in POST requests called callbacks, so you don’t have to poll for them. This is the preferred method, but it requires your server to be exposed to the internet over HTTPS. If your server is briefly down or we can’t connect to it, we’ll put the callbacks in a queue.
The other method is longpoll, and your client doesn’t have to be exposed to the internet to use it. If you don’t have a public URL that can receive POST requests (for example, you’re developing locally, or the results go to a browser widget or a mobile app, or you don’t have a static IP), send GET requests to https://api.audd.io/longpoll/. The longpoll requests are outgoing HTTPS requests, so longpoll works behind NAT and firewalls; often, this is what decides which method you use.
Longpoll needs a callback URL too. Please set the callback URL to an actual URL that can receive POST requests even if you want to use longpoll only. Otherwise, the poll returns no results. If you don’t have any such URLs, set
https://audd.tech/empty/as the callback URL: it always responds with200 OK.
You can use longpoll instead of or together with receiving callbacks. For example, a backend server can use callbacks to log airplay, while a widget can use longpoll to show the current song of the same stream.
Callbacks: AudD POSTs to you
To be able to receive callbacks from AudD, your server needs to be accessible from the web. Set the URL for callbacks using the setCallbackUrl API method. You can change the URL at any time using this method. The callbacks from all the streams will be sent to the specified URL as a JSON structure in the POST payload. There is no per-stream callback URL.
from audd import AudD
audd = AudD("your-api-token") # get your own token at dashboard.audd.io
audd.streams.set_callback_url("https://yourapp.example/audd-callback")
audd.streams.add(url="https://npr-ice.streamguys1.com/live.mp3", radio_id=3249)You’ll receive callbacks with results and with notifications. You’ll receive the first result seconds or minutes after you add the stream, and then one result for every new song.
- The results contain the recognized songs. The results will also contain the
radio_idyou chose for the stream, thetimestamp, and (by default) the played duration inplay_length. - Notifications are information about the state of the streams. If something happens to an audio stream, we’ll let you know by sending a notification so you can fix the stream or change its URL. There are 3 codes:
0(which means that everything’s fine),650(which means that we can’t connect to the stream) and651(which means that we don’t receive any music from the stream, only white noise).
{
"status": "success",
"result": {
"radio_id": 7,
"timestamp": "2020-04-13 10:31:43",
"play_length": 111,
"results": [
{
"artist": "Alan Walker, A$AP Rocky",
"title": "Live Fast (PUBGM)",
"album": "Live Fast (PUBGM)",
"release_date": "2019-07-25",
"label": "MER Recordings",
"score": 100,
"song_link": "https://lis.tn/LiveFastPUBGM"
}
]
}
}Please ensure that your handler responds with the 200 OK HTTP status when it receives the callbacks. If your server doesn’t respond to the callbacks with the 200 OK status or we can’t connect to your server, we’ll put the callbacks in a queue (see the delivery semantics below).
Longpoll: you pull from AudD
In addition to callbacks, you can use HTTP longpoll to get new results from any number of clients without having to spin up a web server or have a static IP. The longpoll address is https://api.audd.io/longpoll/. If you’re making the longpoll requests manually, here’s an example of a longpoll URL:
GET https://api.audd.io/longpoll/?category=92b1cc7f0&timeout=50&since_time=1652123144400The client sends its request, and waits for the answer. We send the response when an event is ready or when the timeout has elapsed. When you receive a response containing the timestamp field, use it as the updated since_time for the new request. As the subscription category, use the longpoll_category field from the getStreams response, or calculate the first nine characters of MD5(MD5("your api_token") + "radio_id"). You can share the categories with your clients or users, but please don’t include your API tokens in the client-side software. We’ve implemented longpoll clients in our SDKs. While a poll is running, the SDK keeps the since_time for you; if you start a new poll, it starts from the current time unless you pass since_time.
audd = AudD("your-api-token")
with audd.streams.longpoll(category="92b1cc7f0", timeout=50) as poll:
for match in poll.matches:
print(match.song.artist, "—", match.song.title)Longpoll still needs a callback URL configured. Please use longpoll after setting a working callback URL using the
setCallbackUrlmethod (or in the web interface). If you don’t have any such URLs, set the no-op receiverhttps://audd.tech/empty/as the callback URL. It responds with200 OKand discards the payload.
You can also use our widget, https://widget.audd.tech/?ch=-92b1cc7f0&background&history&shadow. It shows the last recognized song and, optionally, the history. The widget uses longpoll to get new songs, so you don’t need a backend at all. Use the longpoll category with ”-” before it as the ch URL parameter.
Delivery semantics: retries, ordering, and missed events
Callbacks have a retry queue. If your server doesn’t respond to the callbacks with the 200 OK status or we can’t connect to your server, we’ll put the callbacks in a queue. When your server is back online, we’ll gradually send all the backlog. We have a rate limit for the requests we’re sending: if you have fewer than 500 streams, you won’t receive more than 3 callbacks per second (with a burst size of 15).
Longpoll catches up by cursor. When you receive a response containing the timestamp field, use it as the updated since_time for the new request. You’ll then receive everything since that point. However, if you skip this step or wait too long between requests, some events can be lost or sent to you twice.
Ordering. You can tell the streams apart by the radio_id in every result: the integer you passed to addStream. Don’t rely on the order of the results from different streams. Use the radio_id and the timestamp in every result instead of the order of arrival.
You can disable longpoll for your streams by adding disable-lastsong=true as a parameter to your callback URL. This is useful if you want to use callbacks without actually keeping the results for polling.
The callbacks="before" option
By default, a result is sent after the song ends and contains the total streamed time of the song, play_length. Set callbacks="before" when adding the stream if you want to receive the callbacks as soon as a song starts playing (instead of when it ends):
audd.streams.add(
url="twitch:monstercat",
radio_id=3249,
callbacks="before",
)If you send before, you won’t receive the total played time (play_length) in the results: the song hasn’t finished yet. Note that this applies to both callbacks and longpoll. Use before if you want to show the song that is playing now. Use the default if you want to know how long the songs played, for airplay logs and reports.
Worked example
Server backend logging airplay: callbacks
- Set the URL for callbacks once using
setCallbackUrl("https://yourapp.example/audd-callback"). - Add the streams using the
addStreamAPI method. By default, we send the results after the songs end, so each result carriesplay_length. - Save every result with its
radio_id, and respond with200 OK. - You can restart the backend at any time for a deploy. When your server is back online, we’ll send all the backlog.
- If something happens to an audio stream, we’ll let you know by sending a notification so you can fix the stream or change its URL. Set up alerts for
650and651notifications.
Browser now-playing widget: longpoll
- Set a callback URL. If you don’t have any such URLs, set
https://audd.tech/empty/as the callback URL. - Add the stream with
callbacks="before", so the widget updates as soon as a song starts playing. - Embed our widget (use the longpoll category with ”-” before it as the
chURL parameter, likech=-<category>), or pollhttps://api.audd.io/longpoll/from your own front end with thecategory, and carry eachtimestampinto the nextsince_time.
Common mistakes
- Starting longpoll without a callback URL set. Please set the callback URL to an actual URL that can receive POST requests even if you want to use longpoll only. Otherwise, the poll returns no results.
- Returning a non-200 from your callback handler. If your server doesn’t respond to the callbacks with the 200 OK status or we can’t connect to your server, we’ll put the callbacks in a queue. Return
200 OKfirst, and then process the result. - Ignoring
since_timein longpoll. When you receive a response containing thetimestampfield, use it as the updatedsince_timefor the new request. Otherwise, you’ll receive old events again or miss new ones. - Putting your API token in client-side longpoll code. You can share the categories with your clients or users, but please don’t include your API tokens in the client-side software.
- Expecting
play_lengthwithcallbacks="before". If you sendbefore, you won’t receiveplay_lengthin the results. - Building a webhook receiver for a browser or mobile UI. Browsers and mobile apps can’t receive callbacks, as they can’t accept incoming POST requests. If the results are for the client side, use longpoll or our widget.
- Polling from a server that could receive callbacks. Receive the results in the callbacks; this is the recommended option.
Related
- Standard, enterprise, or streams: how to choose
- Build a now-playing widget
- Monitor radio airplay
- Securing your stream callback endpoint
- Test stream callbacks locally
- Recognize songs and receive stream results in n8n
- Recognize songs and receive stream results in Zapier and Make
- Streams docs
- API reference
Reading this as an AI agent? The raw Markdown is at concepts/callback-vs-longpoll.md, and the full index is /resources/llms.txt.