Browse Source

Ping hub when our lifestream is updated

master
Chimo 2 years ago
parent
commit
4cb69061f8
2 changed files with 77 additions and 0 deletions
  1. +72
    -0
      classes/Lifestream.js
  2. +5
    -0
      config.dist.json

+ 72
- 0
classes/Lifestream.js View File

@ -13,6 +13,8 @@
/* Utils: */
format_date,
http_get,
http_post,
ping_hub,
_setTimeout;
/**
@ -286,6 +288,11 @@
// Notify websockets
that.ws.broadcast( JSON.stringify( event ) );
that.wss.broadcast( JSON.stringify( event ) );
// Notify hub that our topic(s) have been updated
that.config.pubs.forEach( function( pub ) {
ping_hub( pub.hub, pub.topic );
} );
} );
} );
} );
@ -298,6 +305,46 @@
// ############# UTILS #############
ping_hub = function( hub, topic ) {
var post_data,
post_options,
post_req,
req = require( "https" ), // TODO: support http hubs
querystring = require( "querystring" );
post_data = querystring.stringify( {
"hub.mode": "publish",
"hub.url": topic
} );
console.log( "POST data: " + post_data );
post_options = {
host: hub,
port: 443, // TODO: support http hubs
followAllRedirects: true,
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": post_data.length
}
};
post_req = req.request(post_options, function( res ) {
res.setEncoding( "utf8" );
res.on( "data", function ( chunk ) {
console.log( "Response: " + chunk );
});
});
post_req.on( "error", function ( e ) {
console.log( e );
} );
post_req.write( post_data );
post_req.end();
};
// From: https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout#A_possible_solution
// TODO: We don't need to keep original functionality since we're calling it a different name
_setTimeout = function( vCallback, nDelay ) {
@ -356,5 +403,30 @@
} );
};
http_post = function( url, callback ) {
var req = ( url.substr( 0, 8 ) === "https://" ) ? require( "https" ) : require( "http" ),
handleResponse;
handleResponse = function( response ) {
var data = "";
response.on( "data", function( chunk ) {
data += chunk;
} );
response.on( "end", function() {
callback( data );
} );
};
req.post( url, handleResponse )
.on( "error", function( e ) {
/* FIXME: logger.debug( "Error trying to fetch topic: " + e.message );
logger.debug( "Topic: " + url ); */
console.log( "Got error trying to fetch topic: " + e.message );
console.log( "Topic: " + url );
} );
};
module.exports = Lifestream;
}() );

+ 5
- 0
config.dist.json View File

@ -11,6 +11,11 @@
"password": ""
},
"pubs": [
"hub": "",
"topic": ""
],
"subs": [
{
"topic": "",


Loading…
Cancel
Save