Skip to content

Instantly share code, notes, and snippets.

@mandeepm91
Created November 27, 2017 16:39
Show Gist options
  • Save mandeepm91/914f852e05ebdba3f9149f1d43fb94a9 to your computer and use it in GitHub Desktop.
Save mandeepm91/914f852e05ebdba3f9149f1d43fb94a9 to your computer and use it in GitHub Desktop.
Basic chatroom server using Node.js
// run the server using `node server.js` command
// connect to the server using `nc <IP address> 8080`
const server = require('net').createServer();
let sockets = {};
let counter = 1;
function timestamp () {
const now = new Date();
return `${(now.getMonth()+1)}/${now.getDate()} ${now.getHours()}:${now.getMinutes()}`
}
function allUserNamesString () {
return Object.keys(sockets).map(key => {
return sockets[key].name;
}).join(', ');
}
function printOnlineUsers (socket) {
socket.write(`Users currently online: ${allUserNamesString()} \n> `);
}
server.on('connection', socket => {
console.log('new connection');
socket.write('Please enter your name to continue\n> ');
socket.on('data', (message) => {
if (!socket.id) {
// user is providing their name
// trimming is needed to eliminate newline from end
socket.id = counter++;
socket.name = message.toString().trim();
sockets[socket.id] = socket;
socket.write(`Welcome ${socket.name}!\n`);
printOnlineUsers(socket);
Object.entries(sockets).forEach(([id, cs]) => {
if (Number(id) !== Number(socket.id)) {
cs.write(`<${socket.name} just joined! [${timestamp()}]>\n`);
printOnlineUsers(cs);
}
});
return;
}
// user is typing some message
// broadcast this to all other users except self
Object.entries(sockets).forEach(([id, cs]) => {
if (Number(id) !== Number(socket.id)) {
cs.write(`${socket.name} [${timestamp()}]: ${message}`);
}
cs.write('> ');
});
});
socket.on('close', () => {
console.log(`Connection Closed: ${socket.id}`);
delete sockets[socket.id];
Object.entries(sockets).forEach(([id, cs]) => {
if (Number(id) !== Number(socket.id)) {
cs.write(`<${socket.name} just left [${timestamp()}]>\n`);
printOnlineUsers(cs);
}
});
});
});
server.listen(8080, () => {
console.log('opened server on', server.address());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment