Skip to content

Commit

Permalink
Merge pull request #32 from drewuse/myChatApp
Browse files Browse the repository at this point in the history
In app chat functionality
  • Loading branch information
jhonatancruz authored Apr 14, 2019
2 parents 5ce979d + cb70050 commit 2a185a0
Show file tree
Hide file tree
Showing 1,252 changed files with 249,098 additions and 21 deletions.
51 changes: 46 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var createError = require('http-errors');
var logger = require('morgan');


var indexRouter = require('./routes/index');
var sellRouter = require('./routes/sellForm');
var profileRouter = require('./routes/profile')
var chatRouter = require('./routes/chat');
var googleAuthRouter = require('./routes/authentication')
var logoutRouter = require('./routes/logout')

Expand All @@ -17,6 +20,36 @@ const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;

var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);

// io.on('connection', function(socket){
// socket.on('chat message', function(msg){
// io.emit('chat message', msg);
// });
// });
io.on('connection', (socket) => {
console.log('New user connected')

//default username
socket.username = "Anonymous"

//listen on change_username
socket.on('change_username', (data) => {
socket.username = data.username
})

//listen on new_message
socket.on('new_message', (data) => {
//broadcast the new message
io.sockets.emit('new_message', {message : data.message, username : socket.username});
})

//listen on typing
socket.on('typing', (data) => {
socket.broadcast.emit('typing', {username : socket.username})
})
})

// view engine setup
var handlebars = hbs.create({
Expand All @@ -29,6 +62,14 @@ var handlebars = hbs.create({
defaultLayout:'layout.hbs',
layoutsDir:__dirname+'/views/layouts/'
});

app.use(function(req,res,next){
res.io =io;
next();
});



app.engine('hbs', handlebars.engine);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
Expand All @@ -45,13 +86,15 @@ app.use(expressSession({
resave: false,
saveUninitialized: false,
}));

app.use(passport.initialize());
app.use(passport.session());


app.use('/', indexRouter);
app.use('/sell', sellRouter);
app.use('/profile', profileRouter);
app.use('/chat',chatRouter);
app.use('/auth/google/callback', googleAuthRouter);
app.use('/logout', logoutRouter);

Expand All @@ -68,10 +111,8 @@ app.use(function(err, req, res, next) {

// render the error page
res.status(err.status || 500);
res.render('error');
res.render('error', { title: 'DrewUse'});
});




module.exports = app;
module.exports = {app:app, server:server};
6 changes: 3 additions & 3 deletions bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('drewuse:server');
var app = require('../app').app;
var debug = require('debug')('drewUse:server');
var http = require('http');

/**
Expand All @@ -19,7 +19,7 @@ app.set('port', port);
* Create HTTP server.
*/

var server = http.createServer(app);
var server = require('../app').server;

/**
* Listen on provided port, on all network interfaces.
Expand Down
19 changes: 19 additions & 0 deletions models/chat_model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
let mongoose = require('mongoose');
var Schema = mongoose.Schema;

var chatDataSchema= new Schema({
// Initial info
users:[{email:String}],
messages:[{message:String, byWho: String, timestamp:Number}],
dateChatCreated: Number,
dateChatCreatedComputed: String,
dateChatStartedBy: String,
selectedImageUrl: String,
selectedPostTitle: String,
selectedPersonSelling: String
},{collection:'chats'});


var chatData = mongoose.model('chats', chatDataSchema);

module.exports = chatData;
2 changes: 2 additions & 0 deletions node_modules/after/.npmignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions node_modules/after/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions node_modules/after/LICENCE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 115 additions & 0 deletions node_modules/after/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions node_modules/after/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions node_modules/after/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2a185a0

Please sign in to comment.