Skip to content

Instantly share code, notes, and snippets.

@woganmay
Created April 25, 2017 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woganmay/adfba2607dd7702f749ef0013e55ab02 to your computer and use it in GitHub Desktop.
Save woganmay/adfba2607dd7702f749ef0013e55ab02 to your computer and use it in GitHub Desktop.
Subscribe to a Mastodon streaming URL with NodeJS
// 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