Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

. #18

Open
wants to merge 46 commits into
base: master
Choose a base branch
from
Open

. #18

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
d865a69
framework complete, serving with express
grandsupreme Apr 5, 2017
cc1ca46
test:
grandsupreme Apr 5, 2017
3f7e071
storePost function
eglital Apr 5, 2017
5f18c9c
rewrote storePost to create keyed hashes, began work on getMessages s…
grandsupreme Apr 5, 2017
cd673c7
store messages id
eglital Apr 5, 2017
ee6e6ba
created promise structure to deliver message objects array to .get / …
grandsupreme Apr 5, 2017
decd1e3
adding time
eglital Apr 5, 2017
c58a9e5
created login page that stores username cookie and redirects to home …
grandsupreme Apr 5, 2017
60f26af
added user to storePost, and added to messages display in index.handl…
grandsupreme Apr 5, 2017
1ffd06d
fixed syntax indent
grandsupreme Apr 5, 2017
350a69b
moved main page display to getMessages module
grandsupreme Apr 5, 2017
313c112
tomato
grandsupreme Apr 5, 2017
9ba200a
add room
eglital Apr 5, 2017
eadd10f
merge wierdness
grandsupreme Apr 5, 2017
580a2db
t
grandsupreme Apr 5, 2017
8da08a7
trying to solve merge
eglital Apr 5, 2017
ed018a9
t
grandsupreme Apr 5, 2017
618fd3a
merge issue
eglital Apr 5, 2017
68c3e67
Merge branch 'master' of https://github.com/eglital/project_superchat
grandsupreme Apr 5, 2017
e8005de
added storePost
grandsupreme Apr 5, 2017
58389cc
yeup
grandsupreme Apr 5, 2017
2aa910f
websockets and other updates
eglital Apr 6, 2017
6cb9821
Changes for heroku deployment
grandsupreme Apr 6, 2017
26e9482
changed listen method to listen to all ips
grandsupreme Apr 6, 2017
7318526
revert to no ip in server.listen
grandsupreme Apr 6, 2017
d6d60d0
added Procfile >> we: node index.js
grandsupreme Apr 6, 2017
cacd74e
modified for heroku redis
grandsupreme Apr 6, 2017
b4009ab
changed bootstrap urls to https - not serving in heroku
grandsupreme Apr 6, 2017
d092c18
hardcoded redis url in index.js
grandsupreme Apr 6, 2017
0131ee7
hardcoded redis url
grandsupreme Apr 6, 2017
3d57e9f
url
grandsupreme Apr 6, 2017
fb6397b
fix port issue
eglital Apr 6, 2017
c59f37a
comment
eglital Apr 6, 2017
319f6de
fixed redis implementation
grandsupreme Apr 6, 2017
93b1dab
merge conflict resolution
grandsupreme Apr 6, 2017
9fceb2b
commented out empty chat room population conditional in / get router …
grandsupreme Apr 6, 2017
b0d1157
Refactored syntax and formatting
grandsupreme Apr 6, 2017
9cdcb41
troubleshooting
grandsupreme Apr 6, 2017
ae1d0fb
Chatbot logic implemented
grandsupreme Apr 6, 2017
aa34e17
sockets fix
grandsupreme Apr 6, 2017
00980d6
implemented chatbot into the sockets
grandsupreme Apr 6, 2017
cec55e0
finished chatbot implementation
grandsupreme Apr 6, 2017
f2fbc48
trying new message indicator
eglital Apr 6, 2017
5f45fff
update socket
eglital May 28, 2017
470da0c
update redis url
eglital May 28, 2017
e225a1b
redis
eglital May 28, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: node index.js
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# project_superchat
Build a realtime multi-room chat application. Make it super.


111 changes: 111 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const express = require("express")
const app = express()
const server = require('http').createServer(app)
const io = require('socket.io')(server)
const bodyParser = require('body-parser')
const expressHandlebars = require("express-handlebars");
const cp = require('cookie-parser');
const path = require('path');
const redisClient = require('./services/redis/createClient');
//Helper Modules
const {addMessage, getMessagesForRoom, compareMessageTimes} = require('./services/redis/messageHandler')
const {addRoom, getRoomIDs} = require('./services/redis/roomHandler');
const checkSuperbot = require('./services/chatbot/superbot.js')
const port = process.env.PORT || '3000'

app.use(bodyParser.urlencoded({ extended: true }));
app.use(cp())

const hbs = expressHandlebars.create({
defaultLayout: "main",
});
app.engine("handlebars", hbs.engine);
app.set("view engine", "handlebars");

app.use(express.static(path.join(__dirname, 'public')));

app.use(
"/socket.io",
express.static(__dirname + "node_modules/socket.io-client/dist/")
);

io.on("connection", client => {

client.on("send post", (newPost, username, room) => {
addMessage(newPost, username, room, Date.now());
let superBotResponse = checkSuperbot(newPost)
if (superBotResponse) {
addMessage(superBotResponse, 'superBot', room, Date.now());
io.emit('new post', [newPost, username, room], [superBotResponse, 'superBot', room]);
} else {
io.emit('new post', [newPost, username, room]);
}
});

client.on("add room", (newRoom) => {
addRoom(newRoom);
io.emit('new room', newRoom);
});

client.on("change room", (roomName) => {
getMessagesForRoom(roomName).then(messages => {
client.emit('change room messages', messages)
})
})

});

// redisClient.flushall();

app.get('/', (req, res) => {
if (!req.cookies.username) {
res.redirect("/login")
} else {
let roomNames = [];
let username = req.cookies.username;
getRoomIDs()
.then(roomIDsArray => {
roomIDsArray.forEach(roomID => {
roomNames.push(roomID.substr(5))
})
if (!roomNames.length) {
addRoom("Cats");
roomNames.push("Cats");
}
getMessagesForRoom('Cats') //Cats is default room
.then(messages => {
messages = messages.sort(compareMessageTimes)
res.render('index', {roomNames, messages, username})
})
})
}
})

app.get('/login', (req, res) => {
if(req.cookies.username) {
res.redirect('/')
} else {
res.render('login')
}
})

app.post('/login', (req, res) => {
let username = req.body.newUser;
res.cookie("username", username)
res.redirect('/')
})

app.get('/signout', (req, res) => {
res.clearCookie("username")
res.redirect('/login')
})

server.listen(port, function(err) {
console.log(`listening on ${port}`);
});

//IF FLUSH DATABASE, ADD TO GET '/' TO RENDER HOMEPAGE
// if (!roomNames.length) {
// addRoom("Cats");
// roomNames.push("Cats");
// }
33 changes: 33 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "project_superchat",
"version": "1.0.0",
"description": "Build a realtime multi-room chat application. Make it super.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"

},
"repository": {
"type": "git",
"url": "git+https://github.com/markmarkyesyes/project_superchat.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/markmarkyesyes/project_superchat/issues"
},

"homepage": "https://github.com/markmarkyesyes/project_superchat#readme",
"dependencies": {
"body-parser": "^1.17.1",
"cookie-parser": "^1.4.3",
"express": "^4.15.2",
"express-handlebars": "^3.0.0",
"redis": "^2.7.1",
"shortid": "^2.2.8",
"socket.io": "^1.7.3",
"socket.io-client": "^1.7.3"
}
}
96 changes: 96 additions & 0 deletions public/js/socketEvents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
$(document).ready(function() {
var socket = io.connect("https://egles-superchat.herokuapp.com//");
// var socket = io.connect('http://localhost:3000');

//Event Listeners
socket.on("new post", (userResponse, botResponse) => {
//User response elements
let newPost = userResponse[0];
let username = userResponse[1];
let room = userResponse[2];
if ($("#currentRoom").text() === room) {
//User response
$(".messages").append(
`<li class="left clearfix">
<span class="chat-img1 pull-left">
<h6>${username}</h6>
</span>
<div class="chat-body1 clearfix">
<p>${newPost}</p>
</div>
</li>`
);
if (botResponse) {
response = botResponse[0];
$(".messages").append(
`<li class="left clearfix">
<span class="chat-img1 pull-left">
<h6>SuperBot</h6>
</span>
<div class="chat-body1 clearfix">
<p>${response}</p>
</div>
</li>`
);
}
} else {
console.log("room ", room);
$(`#${room}`).find(".indicator").append("TEXT");
}
});

socket.on("new room", newRoom => {
$("#rooms").prepend(
`<li class="left clearfix">
<div class="chat-body clearfix">
<div class="header_sec roomName">
<strong class="primary-font roomText" id="{newRoom}">${newRoom}</strong><div id="indicator" class="hidden"></div>
</div>
</div>
</li>`
);
});

socket.on("change room messages", messages => {
$(".messages").empty();
messages.forEach(message => {
$(".messages").prepend(
`<li class="left clearfix">
<span class="chat-img1 pull-left">
<h6>${message.username}</h6>
</span>
<div class="chat-body1 clearfix">
<p>${message.body}</p>
</div>
</li>`
);
});
});

//Event Handlers
$("#sendPost").click(() => {
let newPost = $("#newPost").val();
let username = $("#username").text();
let room = $("#currentRoom").text();
$("#newPost").val("");
socket.emit("send post", newPost, username, room);
});

$("#addRoom").click(() => {
let newRoom = $("#newRoom").val();
$("#newRoom").val("");
if (newRoom.length) {
socket.emit("add room", newRoom);
}
});

$("ul#rooms").on("click", ".roomName", event => {
let roomName = $(event.target).find(".roomText").text();
// if text is clicked, set roomName to text element
if (!roomName) {
roomName = $(event.target).text();
}
$("#currentRoom").text(roomName);
socket.emit("change room", roomName);
});
});
Loading