Skip to content
This repository was archived by the owner on Jan 30, 2022. It is now read-only.

Commit 9a41861

Browse files
committed
V2.2 commit: Added Realtime Updating to Talks
I'm using Socket.io to handle the websockets that manage the message passing between clients
1 parent 18d7839 commit 9a41861

File tree

7 files changed

+7319
-36
lines changed

7 files changed

+7319
-36
lines changed

.eslintrc.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"parserOptions": {
3+
"ecmaVersion": 6
4+
},
5+
"rules": {
6+
"curly": [2, "all"]
7+
}
8+
}

index.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ var express = require('express'),
66

77
var app = express();
88

9+
// create a node httpserver using the express app as a backend
10+
var httpserver = require('http').Server(app);
11+
12+
// initalize socket.io object
13+
var io = require('socket.io')(httpserver);
14+
915
// use jade as the template engine
1016
app.set('view engine', 'pug');
1117

@@ -43,7 +49,16 @@ app.post('/api/postTalk', talksCtrl.createTalk);
4349
app.post('/api/hideTalk', talksCtrl.updateTalk);
4450
app.post('/api/unhideTalk', talksCtrl.updateTalk);
4551

52+
// Socket.io code
53+
io.on('connection', socket => {
54+
55+
// When a client sends a update, broadcast it to all other clients
56+
socket.on('update', data => {
57+
socket.broadcast.emit('change', data);
58+
})
59+
});
60+
4661
// start the server
47-
app.listen(3000, () => {
62+
httpserver.listen(3000, () => {
4863
console.log('Server listening on port 3000, press ctrl-C to quit...');
4964
});

package.json

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
{
2-
"name": "node-talks",
3-
"version": "1.0.0",
4-
"description": "A clone of the COSI talks flask app",
5-
"main": "index.js",
6-
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
8-
},
9-
"author": "Benjamin Lannon",
10-
"license": "MIT",
11-
"dependencies": {
12-
"body-parser": "^1.15.0",
13-
"express": "^4.13.4",
14-
"gulp": "^3.9.1",
15-
"gulp-jshint": "^2.0.0",
16-
"gulp-sass": "^2.3.1",
17-
"insubnet": "0.0.8",
18-
"jshint": "^2.9.1",
19-
"jshint-stylish": "^2.1.0",
20-
"morgan": "^1.7.0",
21-
"promise": "^7.1.1",
22-
"pug": "^2.0.0-alpha7",
23-
"request-ip": "^1.2.2",
24-
"sequelize": "^3.19.3",
25-
"sqlite3": "^3.1.1"
26-
}
2+
"name": "node-talks",
3+
"version": "1.0.0",
4+
"description": "A clone of the COSI talks flask app",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "Benjamin Lannon",
10+
"license": "MIT",
11+
"dependencies": {
12+
"body-parser": "^1.15.0",
13+
"express": "^4.13.4",
14+
"gulp": "^3.9.1",
15+
"gulp-jshint": "^2.0.0",
16+
"gulp-sass": "^2.3.1",
17+
"insubnet": "0.0.8",
18+
"jshint": "^2.9.1",
19+
"jshint-stylish": "^2.1.0",
20+
"morgan": "^1.7.0",
21+
"promise": "^7.1.1",
22+
"pug": "^2.0.0-alpha7",
23+
"request-ip": "^1.2.2",
24+
"sequelize": "^3.19.3",
25+
"socket.io": "^1.4.8",
26+
"sqlite3": "^3.1.1"
27+
}
2728
}

public/js/app.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,42 @@ function getTalks($scope, $http) {
77
});
88
}
99

10-
function postTalk($scope, $http) {
10+
function postTalk($scope, $http, socket) {
1111
// Set the data to be sent through the request to be the newTalk object
1212
var data = $scope.newTalk;
1313

1414
// Send the POST request
1515
$http.post("/api/postTalk", data).then(response => {
16-
$scope.talks = response.data; // Set the scope's talks to be the output of the api call
16+
$scope.talks = response.data; // Set the scope's talks to be the output of the api call
17+
socket.emit("update", response.data); // Send it to the websocket to broadcast the change to everyone else
1718
}, error => {
1819
alert("Talk submissions are only allowed in the 128.153.0.0/16 subnet");
1920
});
2021
}
2122

22-
function hideTalk($scope, $http) {
23+
function hideTalk($scope, $http, socket) {
2324
var data = {
2425
talkId: $scope.id,
2526
hiddenStatus: true
2627
};
2728

2829
$http.post("/api/hideTalk", data).then(response => {
2930
$scope.talks = response.data;
31+
socket.emit("update", response.data);
3032
}, error => {
3133
alert("Talk modifications are only allowed in the 128.153.0.0/16 subnet");
3234
});
3335
}
3436

35-
function unhideTalk($scope, $http) {
37+
function unhideTalk($scope, $http, socket) {
3638
var data = {
3739
talkId: $scope.id,
3840
hiddenStatus: false
3941
};
4042

4143
$http.post("/api/unhideTalk", data).then(response => {
4244
$scope.talks = response.data;
45+
socket.emit("update", response.data);
4346
}, error => {
4447
alert("Talk modifications are only allowed in the 128.153.0.0/16 subnet");
4548
});
@@ -49,21 +52,28 @@ function unhideTalk($scope, $http) {
4952
app.controller('talksController', ($scope, $http) => {
5053
getTalks($scope, $http); // Always load in talks at startup
5154

55+
let socket = io.connect();
56+
57+
socket.on('change', data => {
58+
$scope.talks = data;
59+
$scope.$apply();
60+
});
61+
5262
// Send a http POST request to create a new talk
5363
$scope.createTalk = () => {
54-
postTalk($scope, $http);
64+
postTalk($scope, $http, socket);
5565
$scope.newTalk = {}; // Clear out the input boxes
5666
};
5767

5868
// Hide a certain talk
5969
$scope.hide = (id) => {
6070
$scope.id = id;
61-
hideTalk($scope, $http);
71+
hideTalk($scope, $http, socket);
6272
};
6373

6474
// unhide a certain talk
6575
$scope.unhide = (id) => {
6676
$scope.id = id;
67-
unhideTalk($scope, $http);
77+
unhideTalk($scope, $http, socket);
6878
};
6979
});

0 commit comments

Comments
 (0)