Skip to content

Instantly share code, notes, and snippets.

@ivikash
Forked from tnylea/appjs
Last active December 11, 2015 03:19
Show Gist options
  • Save ivikash/4536606 to your computer and use it in GitHub Desktop.
Save ivikash/4536606 to your computer and use it in GitHub Desktop.
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
var mongo = require('mongodb');
var d = new Date();
var Server = mongo.Server,
Db = mongo.Db,
BSON = mongo.BSONPure;
dataToPush = {
data : {
name : "null",
msg : "null",
channel : "null",
is_spam : false,
is_delete : false,
}
}
var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('chatDB', server, {safe: true});
app.listen(3000);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
// name and emails in the chat
var names = {};
var emails = {};
var user_count = 0;
io.sockets.on('connection', function (socket) {
add_new_user(socket);
add_new_message(socket);
remove_user(socket);
});
function add_new_user(socket){
socket.on('new_user', function(name, email){
if(name != null && email != null){
// store the name
socket.name = name;
// store the email
socket.email = email;
socket.user_index = user_count;
// add the new user to the names array
names[socket.user_index] = name;
// add the new user email to the emails array
emails[socket.user_index] = email;
// let the new user now that they've been added to the chat
socket.emit('add_message', 'New User Added', name);
// let everyone else know that the new user has joined the chat
socket.broadcast.emit('add_message', name, 'joined the chat');
// update connected users list
io.sockets.emit('update_users', names, emails);
// increment the user count
user_count += 1;
// update user_count
io.sockets.emit('update_user_count', user_count);
}
});
}
function add_new_message(socket){
socket.on('send_message', function (data) {
// when message is sent add the message
io.sockets.emit('add_message', socket.name, data);
dataToPush.user = 123
dataToPush.data.name = socket.name;
dataToPush.data.email = socket.email;
dataToPush.data.msg = data;
populateDB();
});
}
function remove_user(socket){
socket.on('disconnect', function(){
if(socket.name != null){
// remove the name from the name list
delete names[socket.user_index];
// remove the email from the email list
delete emails[socket.user_index];
// update list of users
io.sockets.emit('update_users', names, emails);
// echo globally that this client has left
socket.broadcast.emit('add_message', socket.name, 'quit the chat');
// decrement user_count
user_count -= 1;
// update user_count
io.sockets.emit('update_user_count', user_count);
}
});
}
db.open(function(err, db) {
if(!err) {
console.log("Connected to 'chatDB' database");
db.collection('chat', {safe:true}, function(err, collection) {
if (err) {
console.log("Connected to database");
}
});
}
else{
console.log('Error in database connection')
}
});
function findAll () {
db.collection('chat', function(err, collection) {
collection.find().toArray(function(err, items) {
console.log(items);
});
});
};
var populateDB = function() {
db.collection('chat', function(err, collection) {
collection.insert(dataToPush, {safe:true}, function(err, result) {});
});
};
findAll()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment