Question
can someone make a modification to my chatroom application which using node.js and socket.io to run, so that it can save the messages, username and
can someone make a modification to my chatroom application which using node.js and socket.io to run, so that it can save the messages, username and the time at which the message was sent in a database and display it to the new user? the database should be created using MySQL. my code is below. thanks alot for helping me out.
//--------------------------package.json----------------------//
{ "name": "simplechatapp", "version": "1.0.0", "description": "Simple chat app built with node.js and socket.io", "dependencies": { "express": "^4.16.3", "jquery": "^3.3.1", "mysql": "^2.15.0", "nodemon": "^1.17.3", "npm": "^6.0.0", "socket.io": "^2.1.0" } }
/--------------------------------------------------------------------------------------------------------------------------------------/
//--------------------------------------------------app.js---------------------------------------------------------------------------//
var express = require('express'); var app = express(); var server = require('http').createServer(app); var io = require('socket.io').listen(server); var users = {};
app.get('/', function (req, res) { res.sendFile(__dirname + '/public/index.html'); });
app.use('/public',express.static(__dirname + '/public'));
//Listen on port 3000 server.listen(process.env.PORT || 1000);
//listen on every connection io.sockets.on('connection', function(socket){
// Usernames socket.on('new user',function(data, callback){ // console.log("user connected..."); if(data in users){ callback(false); }
else{ callback(true); socket.nickname = data; users[socket.nickname] = socket; updateNicknames(); } });
function updateNicknames(){ io.sockets.emit('usernames', Object.keys(users)); }
//listen on new_message
socket.on('send message', function(data, callback){ var msg = data.trim(); if(msg.substr(0,3) === '/w '){ msg = msg.substr(3); var ind = msg.indexOf(' '); if(ind !== -1){ var name = msg.substring(0, ind); var msg = msg.substring(ind + 1); if(name in users){ users[name].emit('whisper', {msg: msg, nick: socket.nickname}); } else{ callback('Error! Enter a valid User.'); } } else{ callback('Error! Enter a message for your Whisper'); } } else{ io.sockets.emit('new message', {msg: msg, nick: socket.nickname}); } });
socket.on('disconnect', function(data){ if(!socket.nickname) return; delete users[socket.nickname]; updateNicknames(); });
socket.on('typing', function(data){ io.sockets.emit('typing', {nick : socket.nickname}); }); });
/---------------------------------------------------------------------------------------------------------------------------------------/
//--------------------------------------------------------index.html-----------------------------------------------------------------//
ChatRoom
Enter a Username:
/-------------------------------------------------------------------------------------------------------------------------------------------------------/
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started