Created
April 25, 2017 10:11
-
-
Save woganmay/adfba2607dd7702f749ef0013e55ab02 to your computer and use it in GitHub Desktop.
Subscribe to a Mastodon streaming URL with NodeJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// npm install websocket | |
var WebSocketClient = require('websocket').client; | |
var client = new WebSocketClient(); | |
client.on('connectFailed', function(error) { | |
console.log('Connect Error: ' + error.toString()); | |
}); | |
client.on('connect', function(connection) { | |
connection.on('error', function(error) { | |
console.log("Connection Error: " + error.toString()); | |
}); | |
connection.on('close', function() { | |
console.log("Connection closed"); | |
}); | |
connection.on('message', function(update) { | |
var status = {}; | |
switch(update.type) | |
{ | |
case "utf8": status = JSON.parse(JSON.parse(update.utf8Data).payload); break; | |
default: status = {}; break; | |
} | |
// status is now a Mastodon toot object | |
}); | |
}); | |
client.connect('wss://...', 'echo-protocol'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment