With webhooks, Pusher can repost interesting events that occur in your app to a URL of your choice.
We’ve built a demo, Filtrand, to show you how it works. If you go to the Filtrand home page and enter a subject you are interested in, you will see a stream of tweets that mention this subject.
“How does it work?”
Filtrand includes a Node.js server hosted on Heroku. This server uses a module called Twitter Node to stream in tweets that contain one of the subjects specified by a user. Each tweet streamed in is emitted on all the channels to which it pertains. That is, on all the channels with names that appear in the tweet.
So, how does the Node.js server know when a user becomes interested in a subject? Well, Filtrand could have gone old school and posted requests from the subject form to an action on its Node.js server. That would have worked.
But, what if the user loses interest in the subject and closes their browser? The Node.js server would still be dutifully pumping out tweets about Scarlett Johansson, but no one would be listening.
Luckily, we built Filtrand with magic as our base metal: we built it with webhooks.
“What are these Pusher Webhooks?”
Webhooks exposes an internal event called channel existence. Each time the first person joins a channel, a
channel_occupied event is emitted, and each time the last person leaves a channel, a
channel_vacated event is emitted.
To enable this functionality in your app, go to your account dashboard, find your app, click on the webhooks link, click the enable webhooks check box, click the enable channel existence events check box and enter the URL to which you want to post channel existence events.
We did this for Filtrand, so when someone expresses an interest in anarcho-primivitism, our Node.js server gets informed of this via a webhook from Pusher and starts requesting tweets on the subject from Twitter. When the last person on the anarcho-primivitism channel closes their browser or leaves the Filtrand subject page, another webhook is sent to the Node.js server and it stops requesting tweets on that subject.
“How do I get started?”
Read the documentation, or just head straight to your dashboard to set things up.