Create a /config.js file (For configuration like Database connection and port)
module.exports = { 'SERVERPORT': '8080', 'WEBADDRESS': 'http://localhost/', 'LOG_LEVEL': 'INFO', 'LOG_FILE_PATH': 'my_log.txt', // MySQL Information 'MYSQL_HOST': 'localhost', 'MYSQL_USER': 'root', 'MYSQL_PASSWORD': '', 'MYSQL_DB': 'enterprisedev2' }
Create a /app.js file(Here all nodejs code for working the chat)
var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); config = require('./config.js'); var mysql = require("mysql"); request = require('request'), util = require('./util') app.get('/', function(req, res) { res.sendFile(__dirname + '/chat.html'); }); // Log any uncaught exceptions process.on('uncaughtException', function (exception) { console.log(exception); // to see your exception details in the console fs.appendFile(config['LOG_FILE_PATH'], exception, null); }); /* Make DB Connection */ var con = mysql.createConnection({ host : config['MYSQL_HOST'], user : config['MYSQL_USER'], password : config['MYSQL_PASSWORD'], database : config['MYSQL_DB'] }); //Check the DB Connection status con.connect(function(err) { if (err) { console.log('Error connecting to Db'); return; } }); io.on('connection', function(socket) { socket.on('chat message', function(msg) { /** Insert into database **/ request(config['WEBADDRESS'] + "ajax/add-chat-message/user_id/" + socket.uid + "/tour_id/" + socket.vid+"/text/"+msg, function(err, res, body) { io.sockets["in"](socket.room).emit('chat message', msg,socket.username,socket.vid ); }); }); socket.on('new user', function(message, callback) { //console.log('New User -' + message); }); socket.on('disconnect', function(message) { console.log('user disconnected'); }); var usernames = {}; socket.on('adduser', function(username,vid,uid) { console.log(uid); var roomName='room_'+vid; // store the username in the socket session for this client socket.username = username; socket.vid = vid; //user id socket.uid = uid; // store the room name in the socket session for this client socket.room = roomName; // add the client's username to the global list usernames[username] = username; socket.join(roomName); //socket.emit('chat message',' You have connected to room1','ChatBot'); //to same window socket.broadcast.to(roomName).emit('chat message', username + ' has connected to this room','ChatBot'); //For other windows OR console. }); socket.on('updateDetails',function(data){ console.log(data); }); }); http.listen(config['SERVERPORT'], function() { console.log('listening on *:'+config['SERVERPORT']); });
Create chat.html (Here all HTML code where chat will dispaly)
Question: How to RUN?
Open /chat.html in browser
Question: Now install the required module in node.js?
Go to the folder where app.js is exist. Execute following command
npm install express npm install mysql npm install log4js npm install request
Question: Do i need to install socket.io?
Yes, IF not installed please install.
Helpful link to install socket.
http://stackoverflow.com/questions/18990494/how-to-install-node-js-npm-socket-io-and-use-them
Question: How to run node?
node app.js