Building a Real-Time Activity Stream on Cloud Foundry with Node.js, Redis and MongoDB--Part II #1

Open
wants to merge 2 commits into
from

Projects

None yet

1 participant

@ciberch
Owner

This is the source code for the tutorial on building an Activity Stream Part 2 http://blog.cloudfoundry.com/2012/06/05/node-activity-streams-app-2/

Once you are done making these changes you can easily increase the number of instances

Example:

vmc instances node-express-start 4
@ciberch ciberch commented on the diff Jun 5, 2012
package.json
@@ -6,6 +6,9 @@
"engines" : ["node"],
"repository" : { "type":"git", "url":"http://github.com/mape/node-express-boilerplate" },
"dependencies" : {
+ "activity-streams-mongoose": ">=0.0.11",
+ "mongoose": "",
+ "underscore": "",
@ciberch
ciberch Jun 5, 2012

Run npm install after these changes

@ciberch ciberch commented on the diff Jun 5, 2012
siteConfig.js
@@ -47,5 +50,11 @@ if (cf.cloud) {
settings.redisOptions.host = redisConfig.hostname;
settings.redisOptions.pass = redisConfig.password;
}
+
+ if (cf.mongodb['mongo-asms']) {
@ciberch
ciberch Jun 5, 2012

Locally you need to start running mongo. On Cloud Foundry just do

vmc create-service mongodb mongo-asms
vmc bind-service mongo-asms node-express-start # Or whatever the name is of your app
@ciberch ciberch commented on the diff Jun 5, 2012
lib/socket-io-server.js
});
- client.on('disconnect', function() { console.log('disconnect'); });
+ client.on('disconnect', function() {
+ console.log('********* disconnect');
+ asmsServer.unsubscribe(desiredStream);
@ciberch
ciberch Jun 5, 2012

Avoid memory leaks by unsubscribing from Redis

@ciberch ciberch commented on the diff Jun 5, 2012
public/js/jquery.client.js
- }
- $$('#bubble ul').prepend($li);
- $$('#bubble').scrollTop(98).stop().animate({
- 'scrollTop': '0'
- }, 500);
- setTimeout(function() {
- $li.remove();
- }, 5000);
-
- setTimeout(function() {
- socketIoClient.send('pong');
- }, 1000);
+ socketIoClient.on('message', function(json) {
+ var doc = JSON.parse(json);
+ if (doc) {
+ var msg = doc.actor.displayName + " " + doc.title + " " + doc.object.displayName;
@ciberch
ciberch Jun 5, 2012

Now we have a full activity so we can use its properties to build a more meaningful message

On Part 3 we will see how to use jade templates server side and client side

@ciberch ciberch commented on the diff Jun 5, 2012
public/js/jquery.client.js
+ msg+= ": " + doc.object.content;
+ }
+
+ var $li = $('<li>').text(msg).append($('<img class="avatar">').attr('src', doc.actor.image.url));
+ if (doc.provider && doc.provider.icon && doc.provider.icon.url) {
+ $li.append($('<img class="service">').attr('src', doc.provider.icon.url));
+ }
+ $$('#stream ul').prepend($li);
+ $$('#bubble').scrollTop(98).stop().animate({
+ 'scrollTop': '0'
+ }, 5000);
+ setTimeout(function() {
+ $li.remove();
+ }, 5000);
+
+ if (doc.verb == "connect") {
@ciberch
ciberch Jun 5, 2012

Doing something semi interesting with the different activities

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment