YouTube's Wonky WebSub
Published: 2021-12-22 19:55:33

---
title: YouTube's Wonky WebSub
published: true
date: 2021-12-16 17:16:00 UTC
tags: 
canonical_url: https://kevincox.ca/2021/12/16/youtube-websub/
---

<main><p>I was implementing <a href="https://www.w3.org/TR/websub/">WebSub</a> for <a href="https://feedmail.org">FeedMail</a> and was disappointed that YouTube’s feeds don’t have support. While disappointing this was not much of a surprise, YouTube’s feeds are well-known to be very basic and under-loved.</p>
<p>But then I stumbled across <a href="https://developers.google.com/youtube/v3/guides/push_notifications">https://developers.google.com/youtube/v3/guides/push_notifications</a>. Wait a second! PubSubHubbub support? PubSubHubbub was the original name for WebSub, so it sounded like it should work, but why didn’t FeedMail detect support?</p>
<h2 id="not-advertised"><a href="https://kevincox.ca/2021/12/16/youtube-websub/#not-advertised">Not Advertised.</a></h2>
<p>You are supposed to advertise support for WebSub by retuning a hub reference in the feed. This can either be a <code>&lt;link&gt;</code> element in the feed document or a <code>Link:</code> header. YouTube does neither. No wonder FeedMail thought that YouTube didn’t support it!</p>
<p>No problem, this should be easy to fix! Just special case YouTube feeds and inject the correct hub.</p>
<div><pre><code><span>if</span> <span>url</span><span>.as_str</span><span>()</span><span>.starts_with</span><span>(</span><span>"https://www.youtube.com/feeds/videos.xml?"</span><span>)</span> <span>{</span>
	<span>fetched_feed</span><span>.links</span><span>.push_value</span><span>(</span>
		<span>hyperx</span><span>::</span><span>header</span><span>::</span><span>LinkValue</span><span>::</span><span>new</span><span>(</span><span>"https://pubsubhubbub.appspot.com/"</span><span>)</span>
			<span>.push_rel</span><span>(</span><span>hyperx</span><span>::</span><span>header</span><span>::</span><span>RelationType</span><span>::</span><span>Hub</span><span>));</span>
<span>}</span>
</code></pre></div>
<h2 id="wrong-topic-url"><a href="https://kevincox.ca/2021/12/16/youtube-websub/#wrong-topic-url">Wrong Topic URL</a></h2>
<p>Now FeedMail could subscribe, but it wasn’t receiving updates. There are more layers of weirdness to deal with.</p>
<p>WebSub has the concept of a <a href="https://www.w3.org/TR/websub/#x5-1-subscriber-sends-subscription-request">Topic URL</a> which is basically the url of the feed. Unfortunately I realized that YouTube required something special.</p>
<blockquote><p>Set the topic URL to <code>https://www.youtube.com/xml/feeds/videos.xml?channel_id=CHANNEL_ID</code>, where <code>CHANNEL_ID</code> is the <a href="https://developers.google.com/youtube/v3/docs/channels#id">YouTube channel ID</a> for which you want to retrieve push notifications.</p></blockquote>
<p>So instead of using the feed URL (<code>http://www.youtube.com/feeds/videos.xml?channel_id=CHANNEL_ID</code>) you need to use a slightly different URL. Notice <code>https://</code> and <code>/xml/</code> that differ.</p>
<p>Ok, I guess we can swap that out, and swap it back when handling callbacks.</p>
<div><pre><code><span>if</span> <span>self_link</span><span>.as_str</span><span>()</span><span>.starts_with</span><span>(</span><span>"http://www.youtube.com/feeds/videos.xml?"</span><span>)</span> <span>{</span>
	<span>let</span> <span>mut</span> <span>topic</span> <span>=</span> <span>self_link</span><span>.clone</span><span>();</span>
	<span>topic</span><span>.set_scheme</span><span>(</span><span>"https"</span><span>)</span><span>.unwrap</span><span>();</span>
	<span>topic</span><span>.set_path</span><span>(</span><span>"xml/feeds/videos.xml"</span><span>);</span>
	<span>Some</span><span>(</span><span>topic</span><span>)</span>
<span>}</span> <span>else</span> <span>{</span>
	<span>None</span>
<span>}</span>
</code></pre></div>
<h2 id="custom-payload"><a href="https://kevincox.ca/2021/12/16/youtube-websub/#custom-payload">Custom Payload</a></h2>
<p>Now we were finally getting notified of new videos! But something was wrong, the feed title was messed up and the body of the email FeedMail sent me was empty.</p>
<p>In a PubSubHubbub or WebSub ping the body should be the content of the feed. The only exception for Atom feeds is that previously existing <code>&lt;entry&gt;</code> elements can be removed. YouTube does not do this, they send a custom payload which notably:</p>
<ul>
<li>Has a generic title (“YouTube video feed”) instead of title of the regular feed (which is the channel name).</li>
<li>Contains no description.</li>
<li>Contains no thumbnails.</li>
</ul>
<p>At this point I really reconsidered if it was worth supporting this largely proprietary setup. It may have been sunk-cost fallacy, but I had already done most of the work. At this point I decided that I would just use the “WebSub” ping from YouTube as a notification, I would fall back to fetching the source feed to actually send notifications.</p>
<div><pre><code><span>+ if broken_push(&amp;final_url) {
+ crate::fetch_feed(global, &amp;req).await?
+ } else {
</span> let feed = feed_rs::parser::parse_with_uri(
  		std::io::Cursor::new(body),
  		Some(final_url.as_str()))?;
  
  	crate::FetchedFeed{
  		feed: Some(feed),
  		links,
  		final_url,
<span>+ }
+ }
</span></code></pre></div>
<p>Part of the reason that I decided to support YouTube is that this type of bug is likely to come up for other hubs as well, so it is nice to have the option to maintain a list of broken feeds that use WebSub for notification only.</p>
<p>Well there we go. Working push notifications for YouTube!</p>
<h2 id="implementation-curiosity"><a href="https://kevincox.ca/2021/12/16/youtube-websub/#implementation-curiosity">Implementation Curiosity</a></h2>
<p>The thing that confuses me the most about this is that the hub YouTube uses (<a href="https://pubsubhubbub.appspot.com/">pubsubhubbub.appspot.com</a>), while being owned by Google, is a general purpose hub. (In fact it is probably the most popular hub!) It seems like it would need custom code-paths to support this wonky YouTube WebSub. I guess there is a secret endpoint where YouTube can push the content for a specific URL, that way the push can be sent out without the content ever having actually been accessible at that URL.</p>
<h2 id="stub-curiosity"><a href="https://kevincox.ca/2021/12/16/youtube-websub/#stub-curiosity">Stub Curiosity</a></h2>
<p>Even weirder is the stub at the URL you subscribe to.</p>
<div><pre><code>% curl https://www.youtube.com/xml/feeds/videos.xml?channel_id=CHANNEL_ID
<span>&lt;?xml version="1.0" encoding="utf-8"?&gt;</span>
<span>&lt;!--
  This is a static file. The corresponding feed (identified in the
  &lt;link rel="self"/&gt; tag) should be used as a topic on the
  http://pubsubhubbub.appspot.com hub to subscribe to video updates.
--&gt;</span>
<span>&lt;feed</span> <span>xmlns=</span><span>"http://www.w3.org/2005/Atom"</span><span>&gt;</span>
  <span>&lt;link</span> <span>rel=</span><span>"hub"</span> <span>href=</span><span>"http://pubsubhubbub.appspot.com"</span><span>/&gt;</span>
  <span>&lt;link</span> <span>rel=</span><span>"self"</span> <span>href=</span><span>"https://www.youtube.com/xml/feeds/videos.xml"</span> <span>/&gt;</span>
  <span>&lt;title&gt;</span>YouTube video feed<span>&lt;/title&gt;</span>
<span>&lt;/feed&gt;</span>
</code></pre></div>
<p>This <strong>almost</strong> looks like a fake feed that you could subscribe to for notifications. However, as the comment suggests it is just a static file. Most notably the <code>&lt;link rel=self&gt;</code> doesn’t contain a channel ID! The comment seems to suggest that the URL is correct, but it clearly doesn’t contain the required information!</p>
<p>I <em>think</em> that if the channel ID was simply included in the <code>self</code> URL this would be a good enough fake feed that most RSS readers would allow you to subscribe to it, and if they supported WebSub you would even get updates! However, without the working link this file is just useless, I wonder if this is just to bypass some check in <a href="https://pubsubhubbub.appspot.com/">pubsubhubbub.appspot.com</a> that would otherwise complain about 404s, but do you know what else would bypass 404s? JUST USING THE FEED URL AS THE TOPIC URL!</p>
<h2 id="youtube-please-fix"><a href="https://kevincox.ca/2021/12/16/youtube-websub/#youtube-please-fix">YouTube, please fix?</a></h2>
<p>Looking at this it seems pretty easy to fix, it would just take a couple of changes and wouldn’t break the API for existing users.</p>
<ol>
<li>Add the hub link to the regular feed.</li>
<li>Ping the new endpoint in addition to pushing the custom payload to the previous endpoint.</li>
</ol>
<p>Then the old API can be removed according to YouTube’s usual deprecation policy if desired.</p>
<p>This would make YouTube channel feeds WebSub compatible without affecting existing users. If we can just get the attention of someone at YouTube this would be a nice 20% project. Surely there are no politics around here 😅</p></main>