~/git/blog

My brain-dump of random code/configuration.

02 Jan 2013

Pubsubhubbub With Octopress

In this article I explain how to set up octopress with pubsubhubbub, to get push-enabled feeds. In my example I use superfeedr, which is free to use.

After you signup up a hub, in my case higgsboson.superfeedr.com, you have to add a hub reference to your atom feed.

# ....

# pubsubhubbub
hub_url: http://higgsboson.superfeedr.com/ # <--- replace this with your hub

Insert this line:

{% raw %}

    {% if site.hub_url %}<link href="{{ site.hub_url }}" rel="hub"/>{% endif %}

{% endraw %}

into source/atom.xml. So it looks like this:

{% raw %}

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[{{ site.title }}]]></title>
  <link href="{{ site.url }}/atom.xml" rel="self"/>
  <link href="{{ site.url }}/"/>
  {% if site.hub_url %}<link href="{{ site.hub_url }}" rel="hub"/>{% endif %}
  <updated>{{ site.time | date_to_xmlschema }}</updated>
  <id>{{ site.url }}/</id>
  <author>
    <name><![CDATA[{{ site.author | strip_html }}]]></name>
    {% if site.email %}<email><![CDATA[{{ site.email }}]]></email>{% endif %}
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  {% for post in site.posts limit: 20 %}
  <entry>
    <title type="html"><![CDATA[{{ post.title | cdata_escape }}]]></title>
    <link href="{{ site.url }}{{ post.url }}"/>
    <updated>{{ post.date | date_to_xmlschema }}</updated>
    <id>{{ site.url }}{{ post.id }}</id>
    <content type="html"><![CDATA[{{ post.content | expand_urls: site.url | cdata_escape }}]]></content>
  </entry>
  {% endfor %}
</feed>

{% endraw %}

To push out updates, you have to ping your hub, this is easily done in your deploy rake task.

Add these lines to the end of your deploy task in your Rakefile:

require 'net/http'
require 'uri'
hub_url = "higgsboson.superfeedr.com" # <--- replace this with your hub
atom_url = "http://blog.higgsboson.tk/atom.xml" # <--- replace this with your full feed url
resp, data = Net::HTTP.post_form(URI.parse(hub_url),
    {'hub.mode' => 'publish',
    'hub.url' => atom_url})
raise "!! Hub notification error: #{resp.code} #{resp.msg}, #{data}" unless resp.code == "204"
puts "## Notified hub (" + hub_url + ") that feed #{atom_url} has been updated"

So you end up with something like this:

desc "Default deploy task"
task :deploy do
  # Check if preview posts exist, which should not be published
  if File.exists?(".preview-mode")
    puts "## Found posts in preview mode, regenerating files ..."
    File.delete(".preview-mode")
    Rake::Task[:generate].execute
  end

  Rake::Task[:copydot].invoke(source_dir, public_dir)
  Rake::Task["#{deploy_default}"].execute

  require 'net/http'
  require 'uri'
  hub_url = "higgsboson.superfeedr.com" # <--- replace this with your hub
  atom_url = "http://blog.higgsboson.tk/atom.xml" # <--- replace this with your full feed url
  resp, data = Net::HTTP.post_form(URI.parse(hub_url),
                                   {'hub.mode' => 'publish',
                                    'hub.url' => atom_url})
  raise "!! Hub notification error: #{resp.code} #{resp.msg}, #{data}" unless resp.code == "204"
  puts "## Notified hub (" + hub_url + ") that feed #{atom_url} has been updated"
end

Now whenever you run rake deploy, it will automatically update your hub.

If you have a jabber or google talk account, you can easily verify your setup by adding push-bot to your contact list and subscribe to your feed.

comments powered by Disqus