Skip to content

Commit

Permalink
Added mqlight connection
Browse files Browse the repository at this point in the history
  • Loading branch information
David George committed Apr 13, 2017
1 parent 9f9dec2 commit 892f55d
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 50 deletions.
3 changes: 3 additions & 0 deletions .env → .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
WORKSPACE_ID=<workspace id>
CONVERSATION_USERNAME=<conversation username>
CONVERSATION_PASSWORD=<conversation password>
MQLIGHT_LOOKUP_URL=<mqlight_lookup_url>
MQLIGHT_USER=<mqlight_user>
MQLIGHT_PASSWORD=<mqlight_password>
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.env
node_modules
npm-debug.log
npm-debug.log
70 changes: 39 additions & 31 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
'use strict';

// Configure dotenv to get environment variables
require('dotenv').config();

var messagehubConfig = require('./messagehub');

// This application uses express as its web server
// for more info, see: http://expressjs.com
var express = require('express');
Expand All @@ -25,42 +29,46 @@ var conversation = new ConversationV1({
version_date: '2017-02-03'
});

// Enable body parsing
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
messagehubConfig().then(setup);

// Serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));
function setup (mqlightClient) {

// Get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();
// Enable body parsing
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// Serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));

var messageReceived = function (message, fn) {
// Setup payload
var payload = {
workspace_id: process.env.WORKSPACE_ID,
context: message.context,
input: message.input
}
// Get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();

// Send message to Conversation service
conversation.message(payload,
function(err, data) {
if (err) {
return io.emit('message', err);
var messageReceived = function (message, fn) {
// Setup payload
var payload = {
workspace_id: process.env.WORKSPACE_ID,
context: message.context,
input: message.input
};

// Send message to Conversation service
conversation.message(payload,
function (err, data) {
if (err) {
return io.emit('message', err);
}
return fn(data);
}
return fn(data);
}
);
}
);
}

// Start socket.io
io.on('connection', function (socket) {
socket.on('message', messageReceived);
});
// Start socket.io
io.on('connection', function (socket) {
socket.on('message', messageReceived);
});

// Start server
http.listen(appEnv.port, '0.0.0.0', function() {
console.log("server starting on " + appEnv.url);
});
// Start server
http.listen(appEnv.port, '0.0.0.0', function() {
console.log("server starting on " + appEnv.url);
});
}
76 changes: 76 additions & 0 deletions messagehub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use strict';

// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
var cfenv = require('cfenv');
var mqlight = require('mqlight');
var uuid = require('node-uuid');
var winston = require('winston');

var SERVICE_NAME = 'messagehub';

// Get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();

// Establish credentials
var opts = {};

var service = appEnv.getService(SERVICE_NAME);

if (service) {
opts.service = service.credentials.mqlight_lookup_url;
opts.user = service.credentials.user;
opts.password = service.credentials.password;

if (!opts.hasOwnProperty('service') ||
!opts.hasOwnProperty('user') ||
!opts.hasOwnProperty('password')) {
throw new Error('Error - Check that app is bound to service');
}
}
else if (process.env.MQLIGHT_LOOKUP_URL &&
process.env.MQLIGHT_USER &&
process.env.MQLIGHT_PASSWORD) {
opts.service = process.env.MQLIGHT_LOOKUP_URL;
opts.user = process.env.MQLIGHT_USER;
opts.password = process.env.MQLIGHT_PASSWORD;
} else {
throw new Error('No mqlight service is bound or configured');
}

opts.id = 'TUTORIALBOT_' + uuid.v4().substring(0, 7);

// Message Handler
function handleMessage (data, delivery) {
winston.info('handleMessage', JSON.parse(data));
}

function config () {
return new Promise (function (resolve, reject) {

winston.info('config', opts);

var mqlightClient = mqlight.createClient(opts, function (err) {
if (err) {
winston.error('Connection to ' + opts.service + ' using client-id ' +
mqlightClient.id + ' failed: ' + err);
reject(err);
} else {
winston.info('Connected to ' + opts.service + ' using client-id ' +
mqlightClient.id);
}

mqlightClient.on('message', handleMessage);

mqlightClient.on('error', function (err) {
winston.error(err);
});

resolve(mqlightClient);

});

});
}

module.exports = config;
38 changes: 20 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
{
"name": "tutorialbot",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "4.13.x",
"body-parser": "1.17.1",
"cfenv": "1.0.x",
"dotenv": "^2.0.0",
"socket.io": "1.7.3",
"watson-developer-cloud" : "2.8.2"
},
"repository": {},
"engines": {
"node": "4.x"
}
"name": "tutorialbot",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"body-parser": "1.17.1",
"cfenv": "1.0.x",
"dotenv": "2.0.0",
"express": "4.13.x",
"socket.io": "1.7.3",
"mqlight": "2.0.2017033100",
"watson-developer-cloud": "2.8.2",
"winston": "2.3.1"
},
"repository": {},
"engines": {
"node": "6.x"
}
}

0 comments on commit 892f55d

Please sign in to comment.