How to subscribe youtube push notification

Youtube Pubsubhubbub

Let’s imagine if you want to get notified from your server side when a new youtube video is uploaded or edited in a particular channel, so that you can take any action like sending email to your users or sending push notification to your mobile app. How do you do it? Well here we will learn how to subscribe to a youtube push notification in order to achieve it using youtube pubsubhubbub.

In this tutorial I have used spring boot. But that’s not an issue. You can do it in any backend framework.
I have tested and used this in my website: Free online tool. It’s a free tools for everyone.

Step 1: Setup configuration

First go open this link https://pubsubhubbub.appspot.com/subscribe. Here you have fill up some information about your subscription. The page should look like the following image.

  • Callback URL: You have to put your server url or ip address endpoint here so that pubsubhubbub can send a GET request. For example you can put your url like this: https://yourdomain.com/youtubePubSub. When pubsubhubbub will send a GET request to your link you have read it. Remember, make sure your url endpoint can handle both GET and POST requests.
  • Topic URL: In this field you need to specify for which channel you want to subscribe to the push notification. You have to put the id of the channel link this: https://www.youtube.com/xml/feeds/videos.xml?channel_id=your_channel_id
  • Verify type: Keep it Asynchronous.
  • Mode: Set it Subscribe.

Rest of the fields you can leave it blank.

Step 2: Coding part

As I said I have used spring boot, but you can do it in any framework.

The route you have just put in the callback url, you need to handle the GET request data now. It’s very easy. You just need to return the specific key which is inside the request. You can understand the by the following code:

@ResponseBody
@GetMapping("/youtubePubSub")
public String onSubscribe(@RequestParam Map allRequestParams){
    System.out.println(allRequestParams);
    return allRequestParams.get("hub.challenge");
}

You see I have returned the value of the key hub.challenge. This way pusubhubbub will verify your url. At this point we are almost done.

Next we need to receive the notification when a video is uploaded or updated. At the same endpoint we have to handle the POST request now. Pusubhubbub will send data as Atom feed notifications when the channel will do the following:

  • uploads a video
  • updates a video’s title
  • updates a video’s description

These are xml data. So please make sure your callback url can consume/receive xml format. Check the following code:

@ResponseBody
@PostMapping(value = "/youtubePubSub", consumes = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_ATOM_XML_VALUE, MediaType.APPLICATION_JSON_VALUE },
        produces = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_ATOM_XML_VALUE, MediaType.APPLICATION_JSON_VALUE })
public void onYoutubeVideoPublished(@RequestBody String s){
    System.out.println("call got");
    //Do your job here.
}

Step 3: Automation

Pubsubhubbub will expire your subscription after 5 days from the day you subscribe. So better if we can keep subscribing using any automatic process. I have used Spring Boot’s scheduler. But you can also run cron job if you use Laravel or Django or Node JS etc. Auto subscribing code:

//345600000 milliseconds = 4 days
@Scheduled(fixedDelay = 345600000)
public void subscribeYoutube(){
    HashMap map = new HashMap<>();
    map.put("hub.callback", "https://yourdomain/youtubePubSub");
    map.put("hub.topic", "https://www.youtube.com/xml/feeds/videos.xml?channel_id=UCXqX37Zc3Y6uJYjvoP1O7pg");
    map.put("hub.verify", "async");
    map.put("hub.mode", "subscribe");
    
    //I have used retrofit to make POST request.
    //You can simply use other way to make this request
    new Constants().getApiService().subscribeYoutubeNotification("https://pubsubhubbub.appspot.com/subscribe", map).enqueue(new Callback() {
        @Override
        public void onResponse(Call call, Response response) {
            System.out.println(response.code());
        }

        @Override
        public void onFailure(Call call, Throwable throwable) {
            System.out.println(throwable.getMessage());
        }
    });
}