├── app.js ├── README └── notifications.js /app.js: -------------------------------------------------------------------------------- 1 | //console.log(process.env); 2 | 3 | require("./notifications.js"); -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | This Node application publishes notifications to the main Greenhouse web application. It receives messages from a Redis pub/sub topic and then pushes them out to subscribed browser clients via the Socket.IO Node module. 2 | 3 | The app can be started locally (assuming you have NodeJS set up correctly) by executing 'node app.js' from the root directory of the project. 4 | 5 | When running in local mode, it is expected that there is also already a local Redis instance running (on localhost using the default Redis port). 6 | 7 | When running on Cloud Foundry, it is expected that the app has a Redis service bound to it. 8 | 9 | The app will auto-detect its environment on startup, configuring the correct Redis connection based on what is detected. 10 | 11 | Socket.IO is currently configured to only use the Comet long-polling technique, as Cloud Foundry does not currently allow WebSocket traffic. -------------------------------------------------------------------------------- /notifications.js: -------------------------------------------------------------------------------- 1 | var http = require('http'), 2 | io = require('socket.io'), 3 | redis = require('redis'), 4 | redisListener = null, 5 | 6 | server = http.createServer(); 7 | server.listen(process.env.VMC_APP_PORT || 8088); 8 | 9 | //Set up Redis, dynamically discovering and connecting to the bound CloudFoundry service 10 | if (process.env.VCAP_SERVICES) { 11 | console.log("Bound services detected."); 12 | var services = JSON.parse(process.env.VCAP_SERVICES); 13 | for (serviceType in services) { 14 | console.log("Service: "+serviceType); 15 | console.log("Service Info: "+JSON.stringify(services[serviceType])); 16 | if (serviceType.match(/redis*/)) { 17 | var service = services[serviceType][0]; 18 | console.log("Connecting to Redis service "+service.name+":"+service.credentials.hostname+":"+service.credentials.port); 19 | redisListener = redis.createClient(service.credentials.port, service.credentials.hostname); 20 | redisListener.auth(service.credentials.password); 21 | break; 22 | } 23 | } 24 | } 25 | 26 | //Fall-back Redis connection for local development outside of CloudFoundry 27 | if (!redisListener && !process.env.VCAP_APP_PORT) { 28 | console.log("Connecting to local Redis service"); 29 | redisListener = redis.createClient(); 30 | } 31 | 32 | if (!redisListener) { 33 | console.error("Fatal condition - no connection to Redis established"); 34 | process.exit(1); 35 | } 36 | 37 | redisListener.on("connect", function(){ console.log("Redis listener connection established."); }); 38 | 39 | redisListener.on("error", function(err) { 40 | console.log("Error thrown by redis listener connection: "+err); 41 | process.exit(1); 42 | }); 43 | 44 | // socket.io for pushing messages to the browser 45 | var io = io.listen(server, {transports: ['xhr-polling'], transportOptions: {'xhr-polling': {duration: 10000}} }); 46 | 47 | console.log(io.options); 48 | 49 | redisListener.subscribe("notifications"); 50 | 51 | redisListener.on("message", function(channel, message){ 52 | io.broadcast(JSON.parse(message)); 53 | }); --------------------------------------------------------------------------------