-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
mark_occurrences : { | ||
interval : 0, | ||
disable : false, | ||
retain : true | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
This is the source code for the UBC CS Alumni lecture: [What is this Cloud Thing?](https://www.cs.ubc.ca/event/2012/11/alumni-lecture-andrew-eisenberg-what-cloud-thing) | ||
|
||
This code comes with two apps, rabbit-producer and rabbit-consumer. These applications each run a simple node server to and communicate using [rabbitmq](http://www.rabbitmq.com/). | ||
|
||
To run: | ||
|
||
1. register for an account at http://cloudfoundry.com | ||
2. get the [vmc command line tool](http://docs.cloudfoundry.com/tools/vmc/installing-vmc.html) | ||
3. vmc target http://api.cloudfoundry.com | ||
4. vmc login (your user name and passwprd) | ||
5. cd rabbit-producer | ||
6. vmc push rabbit-producer --runtime node08 (choose your own name) | ||
7. Use the default options and bind a rabbitmq service | ||
8. cd ../rabbit-consumer | ||
6. vmc push rabbit-consumer --runtime node08 (choose your own name) | ||
7. Use the default options and bind to the same rabbitmq service defined in step 7 | ||
|
||
You should now have 2 node server running on cloud foundry. You can send messages from the producer to the consumer. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/*global require console __dirname process */ | ||
/*jslint node:true es5:true */ | ||
|
||
var WebSocketServer = require('ws').Server, | ||
http = require('http'), | ||
express = require('express'), | ||
amqp = require('amqp'), | ||
htmlEscape = require('sanitizer/sanitizer').escape, | ||
sockjs = require('sockjs'); | ||
|
||
var path = process.env.PWD + '/public'; | ||
// use express module as a static file server | ||
var app = express(); | ||
|
||
// use sockjs library to ensure compatibility across broswers and servers w/o websocket support | ||
var wsServer = sockjs.createServer({ jsessionid: true }); | ||
var server = http.createServer(app); | ||
|
||
app.configure(function() { | ||
app.use(app.router); | ||
app.use(express.static(path)); | ||
app.use(express.directory(path)); | ||
app.use(express.errorHandler({ | ||
dumpExceptions: true, | ||
showStack: true | ||
})); | ||
wsServer.installHandlers(server, { prefix: '/msg' }); | ||
}); | ||
|
||
// Cloud Foundry-specific | ||
server.listen(process.env.VCAP_APP_PORT || 8083); | ||
function rabbitUrl() { | ||
if (process.env.VCAP_SERVICES) { | ||
// Cloud Foundry-specific | ||
var conf = JSON.parse(process.env.VCAP_SERVICES); | ||
return conf['rabbitmq-2.4'][0].credentials.url; | ||
} else { | ||
return "amqp://localhost"; | ||
} | ||
} | ||
|
||
/////////////// | ||
// Socket stuff | ||
/////////////// | ||
var socketConns = []; | ||
function addSocketConn(ws) { | ||
socketConns.push(ws); | ||
} | ||
function removeSocketConn(ws) { | ||
for (var i = 0; i < socketConns.length; i++) { | ||
if (socketConns[i] === ws) { | ||
socketConns.slice(i, 1); | ||
} | ||
} | ||
} | ||
|
||
function writeMessage(msg) { | ||
for (var i = 0; i < socketConns.length; i++) { | ||
// send the message to the client over the socket | ||
socketConns[i].write(htmlEscape(msg.body)); | ||
} | ||
} | ||
|
||
// listen for new socket connections | ||
wsServer.on('connection', function(ws) { | ||
console.log('WS connection received: %s.', JSON.stringify(ws.address)); | ||
addSocketConn(ws); | ||
ws.on('close', function() { | ||
removeSocketConn(ws); | ||
}); | ||
}); | ||
|
||
|
||
/////////////// | ||
// Queue stuff | ||
/////////////// | ||
|
||
console.log("Starting ... AMQP URL: " + rabbitUrl()); | ||
// create a new connection | ||
/** @type Stream */ | ||
var conn = amqp.createConnection({ | ||
url: rabbitUrl() | ||
}); | ||
|
||
function setup() { | ||
var exchange = conn.exchange('cf-exchange', { | ||
'type': 'fanout', | ||
durable: false | ||
}, function() { | ||
var queue = conn.queue('cf-queue', { | ||
durable: false | ||
}, function() { | ||
queue.subscribe(function(msg) { | ||
// pass the message from the queue to the clients | ||
// through the websockets | ||
writeMessage(msg); | ||
}); | ||
queue.bind(exchange.name, 'cf-queue'); | ||
}); | ||
}); | ||
} | ||
conn.on('ready', setup); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Adapted from https://github.com/andregoncalves/twitter-nodejs-websocket |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.