Last active
November 6, 2018 04:49
-
-
Save rykov/a5fdf75aefc6fa7b34da to your computer and use it in GitHub Desktop.
[Unofficial] Listening to live Gemfury package updates
This file contains 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
// This sample script will listen to package updates in a Gemfury | |
// account. To get your API token, go to the Tokens section of | |
// your Dashboard, and look for "Full Access token". | |
// To get started, first install the dependencies: | |
// npm install ws xmlhttprequest request centrifuge@2.x.x | |
// | |
// Then run the script with your API token and account username: | |
// node ./live.js <API token> <username> | |
global.XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; | |
global.WebSocket = require('ws'); | |
const Centrifuge = require("centrifuge"); | |
const request = require('request'); | |
var auth = process.argv[2] | |
var acct = process.argv[3] | |
if(auth === undefined || acct === undefined) { | |
console.log("Usage: live.js <token> <username>") | |
process.exit(1) | |
} | |
var requestOptions = { | |
url: 'https://api.fury.io/1/centrifuge/config?meta=true&as=' + acct, | |
headers: { 'Authorization': auth }, | |
method: "POST", | |
}; | |
var centrifuge, done = (function wait() { | |
if (!done) setTimeout(wait, 1000); | |
})(); | |
// Request configuration from API, and then connect to Centrifuge | |
request(requestOptions, function (error, response, body) { | |
if (error && response.statusCode !== 200) { | |
console.log("Error: " + error + " Status: " + response.statusCode); | |
return; | |
} | |
var config = JSON.parse(body); | |
centrifuge = new Centrifuge(config.url, { | |
refreshHeaders: requestOptions.headers, | |
refreshEndpoint: requestOptions.url, | |
// debug: true, | |
}); | |
centrifuge.setToken(config.token); | |
centrifuge.subscribe(config.channel, { | |
subscribe: function() { | |
console.log("Connected to " + acct); | |
}, | |
error: function(err) { | |
console.log("Error: " + err); | |
done = true; | |
}, | |
publish: function(payload) { | |
var event = payload.data; | |
console.log("Event: " + event.name); | |
console.log("Args: " + JSON.stringify(event.args)); | |
}, | |
}); | |
centrifuge.connect(); | |
console.log("Connecting...") | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment