Skip to content

Instantly share code, notes, and snippets.

@silverbucket
Last active May 13, 2017 15:57
Show Gist options
  • Save silverbucket/5976366 to your computer and use it in GitHub Desktop.
Save silverbucket/5976366 to your computer and use it in GitHub Desktop.
Example sockethub-client use for twitter
<html
<head><title>Twitter Example</title></head>
<body>
<script type="text/javascript" src="http://localhost:10550/sockethub/socket.io.js"></script>
<script type="text/javascript" src="http://localhost:10550/activity-streams.js"></script>
<script type="text/javascript" src="http://localhost:10550/sockethub-client.js"></script>
<script type="text/javascript">
window.onload = function () {
var sc = new SockethubClient(io("http://localhost:10550". { path: "/sockethub" });
sc.socket.on("message", function (message) {
console.log("message: ", message); // incoming/fetched tweets show up on this channel
});
sc.socket.on("failure", function (job) {
// a job sent to sockethub (message or credentials channel) failed
console.log("failure: ", job);
});
sc.socket.on("completed", function (job) {
// a command sent to sockethub (message or credentials channel) completed
console.log("completed: ", job);
if (job.object["@type"] === "credentials") {
// credentials successfully registered with sockethub, try to fetch tweets
fetchTweets(job.actor["@id"]);
} else if (job.object["@type"] === "fetch") {
// a fetch command was successful, this doesn't mean we have the tweets (those come in
// on the "messages" channel, just that we successfully sent the fetch request to twitter.
}
});
// register twitter credentials with sockethub, sending a credentials object (job)
// on the credentials channel
sc.socket.send(("credentials", {
context: "irc",
actor: {
"@id": "http://twitter.com/@slvrbckt"
},
object: {
"@type": "creentials",
consumer_key: "##FILL_ME_IN##",
consumer_secret: "##FILL_ME_IN##",
access_token: "##FILL_ME_IN##",
access_token_secret: "##FILL_ME_IN##"
}
});
function fetchTweets(actor) {
sc.socket.send("message", {
context: "twitter",
"@type": "fetch",
actor: { "@id": "http://twitter.com/@slvrbckt" }
});
});
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment