├── mod.json ├── .gitignore ├── Procfile ├── VertxLeaderboard.gif ├── app.json ├── data.js ├── eb_client.js ├── server.js ├── README.md ├── app.js ├── index.html └── event-bus.js /mod.json: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | *.iml -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: vertx run server.js -------------------------------------------------------------------------------- /VertxLeaderboard.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/siddii/vertx-leaderboard/master/VertxLeaderboard.gif -------------------------------------------------------------------------------- /app.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "JavaScript Leaderboard", 3 | "description": "Sample application demonstrating Vert.x Event Bus with AngularJS data binding", 4 | "logo": "http://vertx.io/logo-white-big.png", 5 | "keywords": [ 6 | "Vert.x", 7 | "Angular", 8 | "Event Bus" 9 | ], 10 | "env": { 11 | "BUILDPACK_URL": "https://github.com/mthenw/heroku-buildpack-vertx.git" 12 | } 13 | } -------------------------------------------------------------------------------- /data.js: -------------------------------------------------------------------------------- 1 | //Fill in some dummy data 2 | var data = [ 3 | { 4 | "name": "jQuery", 5 | "upVote": 0, 6 | "downVote": 0 7 | }, 8 | { 9 | "name": "AngularJS", 10 | "upVote": 0, 11 | "downVote": 0 12 | }, 13 | { 14 | "name": "CanJS", 15 | "upVote": 0, 16 | "downVote": 0 17 | }, 18 | { 19 | "name": "ExtJS", 20 | "upVote": 0, 21 | "downVote": 0 22 | }, 23 | { 24 | "name": "Meteor", 25 | "upVote": 0, 26 | "downVote": 0 27 | } 28 | ]; 29 | -------------------------------------------------------------------------------- /eb_client.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var eventBus = null; 4 | var address = "js-frameworks"; 5 | 6 | function publish(message) { 7 | initConn(); 8 | eventBus.publish(address, {text: message}); 9 | } 10 | 11 | function subscribe(cb) { 12 | initConn(); 13 | eventBus.registerHandler(address, function (response) { 14 | cb(response); 15 | }); 16 | } 17 | 18 | function initConn(cb) { 19 | if (!eventBus) { 20 | eventBus = new vertx.EventBus("/leaderboard"); 21 | 22 | eventBus.onopen = function () { 23 | console.log('Eventbus Connected!'); 24 | subscribe(cb); 25 | }; 26 | 27 | eventBus.onclose = function () { 28 | console.log('Eventbus Closed'); 29 | eventBus = null; 30 | }; 31 | } 32 | } -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var vertx = require('vertx'); 4 | 5 | var server = vertx.createHttpServer(); 6 | var container = require('vertx/container'); 7 | var eventBus = require("vertx/event_bus"); 8 | 9 | //load some dummy data 10 | load('data.js'); 11 | 12 | // Serve the static resources 13 | server.requestHandler(function (req) { 14 | var uri = req.uri(); 15 | if (uri == "/") req.response.sendFile("index.html"); 16 | 17 | if (uri == '/data') { 18 | req.response.end(JSON.stringify(data)); 19 | } 20 | 21 | if (uri && (uri.indexOf('js') !== -1 || uri.indexOf('.css') !== -1)) { 22 | req.response.sendFile(uri.substring(1)); 23 | } 24 | }); 25 | 26 | var sockJSServer = vertx.createSockJSServer(server); 27 | sockJSServer.bridge({prefix: "/leaderboard"}, [{}], [{}]); 28 | 29 | sockJSServer.on('socket-created', function (msg) { 30 | return true; 31 | }); 32 | 33 | 34 | eventBus.registerHandler("js-frameworks", function(message) { 35 | data = message.text; 36 | }); 37 | 38 | server.listen(parseInt(container.env.get('PORT') ? container.env.get('PORT') : '9090')); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | vertx-leaderboard [](https://heroku.com/deploy) 2 | ================= 3 | Sample app demonstrating the power of [Vert.x's](http://vertx.io/) *distributed event bus* & [AngularJS's](http://angularjs.org) *data binding* 4 | 5 | ## Introduction 6 | This sample app demonstrates the power of Vert.x distributed event bus along with AngularJS data binding. 7 | Launch the app in multiple tabs or browser windows, play with it & see what happens. Cool eh? 8 | 9 | ### Demo 10 |  11 | 12 | ## Running the app on cloud 13 | The easiest way to test this app is running it on Heroku. 14 | * Get a Free developer account with Heroku (if you don't have one) 15 | * Fork this repo and/or Click on the "Deploy to Heroku" button at the top 16 | * Navigate to the deployed application url 17 | 18 | ## Running the app locally (if you have vertx installed) 19 | ```bash 20 | vertx run server.js 21 | ``` 22 | * Navigate to [http://localhost:9090](http://localhost:9090) 23 | 24 | 25 | ### Build pack 26 | Thanks to the build pack - https://github.com/mthenw/heroku-buildpack-vertx.git for easier heroku deployment :) -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | angular.module('Leaderboard', []).controller("LeaderboardController", ['$scope', '$http', function ($scope, $http) { 4 | 5 | $http.get('/data').success(function (data) { 6 | $scope.frameworks = data; 7 | }).error(function (error) { 8 | console.error('Error = ', error); 9 | }); 10 | 11 | $scope.addFramework = function () { 12 | var frameworks = angular.copy($scope.frameworks); 13 | frameworks.push({ 14 | name: $scope.framework, 15 | upVote: 1, 16 | downVote: 0 17 | }); 18 | publish(frameworks); 19 | }; 20 | 21 | function addVote(name, vote) { 22 | var frameworks = angular.copy($scope.frameworks); 23 | for (var i = 0; i < frameworks.length; i++) { 24 | if (frameworks[i].name === name) { 25 | frameworks[i][vote]++; 26 | break; 27 | } 28 | } 29 | publish(frameworks); 30 | } 31 | 32 | $scope.upVote = function (framework) { 33 | addVote(framework.name, 'upVote'); 34 | }; 35 | 36 | $scope.downVote = function (framework) { 37 | addVote(framework.name, 'downVote'); 38 | }; 39 | 40 | initConn(function (response) { 41 | $scope.frameworks = response.text; 42 | $scope.$apply(); 43 | }); 44 | }]); -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 || Name | 51 |Up Vote | 52 |Down Vote | 53 |
|---|---|---|
| {{framework.name}} | 57 |{{framework.upVote}} 58 | 59 | | 60 |{{framework.downVote}} 61 | 62 | | 63 |