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.

7 min read View as Markdown
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 with 200 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.

Python
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_id you chose for the stream, the timestamp, and (by default) the played duration in play_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) and 651 (which means that we don’t receive any music from the stream, only white noise).
JSON
{
  "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=1652123144400

The 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.

Python
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 setCallbackUrl method (or in the web interface). If you don’t have any such URLs, set the no-op receiver https://audd.tech/empty/ as the callback URL. It responds with 200 OK and 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):

Python
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

  1. Set the URL for callbacks once using setCallbackUrl("https://yourapp.example/audd-callback").
  2. Add the streams using the addStream API method. By default, we send the results after the songs end, so each result carries play_length.
  3. Save every result with its radio_id, and respond with 200 OK.
  4. You can restart the backend at any time for a deploy. When your server is back online, we’ll send all the backlog.
  5. 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 650 and 651 notifications.

Browser now-playing widget: longpoll

  1. Set a callback URL. If you don’t have any such URLs, set https://audd.tech/empty/ as the callback URL.
  2. Add the stream with callbacks="before", so the widget updates as soon as a song starts playing.
  3. Embed our widget (use the longpoll category with ”-” before it as the ch URL parameter, like ch=-<category>), or poll https://api.audd.io/longpoll/ from your own front end with the category, and carry each timestamp into the next since_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 OK first, and then process the result.
  • Ignoring since_time in longpoll. When you receive a response containing the timestamp field, use it as the updated since_time for 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_length with callbacks="before". If you send before, you won’t receive play_length in 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

Reading this as an AI agent? The raw Markdown is at concepts/callback-vs-longpoll.md, and the full index is /resources/llms.txt.