Reputation: 109
I am trying to follow the IBM Bluemix course on Coursera.
I have deployed my custom Nodejs app (code that is available on Coursera) on IBM Bluemix.
var express = require('express');
var app = express();
var Client = require('ibmiotf');
var appConfig;
var serverPort = process.env.VCAP_APP_PORT || 3000;
var serverHost = process.env.VCAP_APP_HOST || 'localhost';
if (process.env.VCAP_SERVICES) {
var env = JSON.parse(process.env.VCAP_SERVICES);
appConfig = {
'org' : env["iotf-service"][0].credentials.org,
'id' : 'dna-nodeserver',
'auth-key' : env["iotf-service"][0].credentials.apiKey,
'auth-token' : env["iotf-service"][0].credentials.apiToken
}
} else {
appConfig = require('./application.json');
}
var responseString = 'Hello Coursera';
var appClient = new Client.IotfApplication(appConfig);
app.get('/', function(req, res) {
res.send(responseString);
});
var server = app.listen(serverPort, serverHost, function() {
var host = server.address().address;
var port = server.address().port;
console.log('Listening at http://%s:%s', host, port);
appClient.connect();
appClient.on('connect', function() {
appClient.subscribeToDeviceEvents('raspberrypi');
});
appClient.on("error", function (err) {
console.log("Error : "+err);
});
appClient.on('deviceEvent', function(deviceType, deviceId, eventType, format, payload) {
responseString = "Device Event from :: "+deviceType+" : "+deviceId+" of event "+eventType+" with payload : "+payload;
console.log("Device Event from :: "+deviceType+" : "+deviceId+" of event "+eventType+" with payload : "+payload);
});
});
The problem that I am facing is shown is the screenshot below:
Also, since I am receiving continuous events from the raspberry pi... the webpage (served by res.send(responseString)) should show the changes automatically ...without the need for me to manually refresh the page. But this does not seem to happen.
Any help would be appreciated. Thanks!
Upvotes: 0
Views: 938
Reputation: 9
Old thread, but thanks to Alexei for pointing me in the right direction.
In my case the same behaviour testing a local version of an event broker when the production version is still running in IBM Cloud.
You can have multiple listeners in this way if you generate an additional API key, save both sets of credentials, and check a suitable environment variable to see which set to apply.
In my app, I wrap these in a function in node:
function getEventBrokerCredentials(siteCode) {
var codeToCredentials = {
'REMOTE_BROKER': {
'orgId': 'abcde',
'id': 'ThisIsABroker',
'apikey': '111111111111111',
'authtoken': '2222222222222'
},
'LOCAL_BROKER': {
'orgId': 'abcde',
'id': 'ThisIsALocalBroker',
'apikey': '3333333333333333',
'authtoken': '4444444444444'
}
};
return codeToCredentials[siteCode];
}
var brokerCredentials = getEventBrokerCredentials(process.env.BROKER_HOST || 'REMOTE_BROKER');
var appClient = new IOTClient.IotfApplication({
'org': brokerCredentials.orgId,
'id': brokerCredentials.id,
'auth-key': brokerCredentials.apikey,
'auth-token': brokerCredentials.authtoken
});
and then use
BROKER_HOST=LOCAL_BROKER nodemon
for local testing and development.
There are many variations on this theme of course.
Upvotes: 0
Reputation: 1
You probably have multiple applications listening to events. Stop the previous Node Red Application on Bluemix. Instead only run the Nodejs app shown above.
Upvotes: 0