Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple TCP chat server with NodeJS, based on the example provided by creationix.
var net = require('net');
var sockets = [];
var port = 8000;
var guestId = 0;
var server = net.createServer(function(socket) {
// Increment
guestId++;
socket.nickname = "Guest" + guestId;
var clientName = socket.nickname;
sockets.push(socket);
// Log it to the server output
console.log(clientName + ' joined this chat.');
// Welcome user to the socket
socket.write("Welcome to telnet chat!\n");
// Broadcast to others excluding this socket
broadcast(clientName, clientName + ' joined this chat.\n');
// When client sends data
socket.on('data', function(data) {
var message = clientName + '> ' + data.toString();
broadcast(clientName, message);
// Log it to the server output
process.stdout.write(message);
});
// When client leaves
socket.on('end', function() {
var message = clientName + ' left this chat\n';
// Log it to the server output
process.stdout.write(message);
// Remove client from socket array
removeSocket(socket);
// Notify all clients
broadcast(clientName, message);
});
// When socket gets errors
socket.on('error', function(error) {
console.log('Socket got problems: ', error.message);
});
});
// Broadcast to others, excluding the sender
function broadcast(from, message) {
// If there are no sockets, then don't broadcast any messages
if (sockets.length === 0) {
process.stdout.write('Everyone left the chat');
return;
}
// If there are clients remaining then broadcast message
sockets.forEach(function(socket, index, array){
// Dont send any messages to the sender
if(socket.nickname === from) return;
socket.write(message);
});
};
// Remove disconnected client from sockets array
function removeSocket(socket) {
sockets.splice(sockets.indexOf(socket), 1);
};
// Listening for any problems with the server
server.on('error', function(error) {
console.log("So we got problems!", error.message);
});
// Listen for a port to telnet to
// then in the terminal just run 'telnet localhost [port]'
server.listen(port, function() {
console.log("Server listening at http://localhost:" + port);
});
@anshulgithub

This comment has been minimized.

Copy link

@anshulgithub anshulgithub commented May 19, 2014

How we can use this code for sending message to only single user, not all users?

@kdamball

This comment has been minimized.

Copy link

@kdamball kdamball commented Jun 20, 2014

@anshulgithub You can do so by broadcasting to a single user and not all. broadcast() can be altered to send to a single user only depending on the criteria

@rooton

This comment has been minimized.

Copy link

@rooton rooton commented Dec 6, 2014

Hi.

Thats chat server, not client.

@graphicbeacon

This comment has been minimized.

Copy link
Owner Author

@graphicbeacon graphicbeacon commented Dec 28, 2014

@rooton You are correct–it is a chat server. Filename has been changed to reflect this. Thanks for pointing this out.

@graphicbeacon

This comment has been minimized.

Copy link
Owner Author

@graphicbeacon graphicbeacon commented Dec 29, 2014

@rooton However I do have a chat client developed with ExpressJS and Socket.io at https://github.com/graphicbeacon/Simple-Chat-App-with-NodeJS--Socket.io-and-Express

@azadkanwar

This comment has been minimized.

Copy link

@azadkanwar azadkanwar commented Jun 6, 2015

hello sir.. Can you tell me how to make pool in node.js .. bcoz i want to make a app which connects users to customer care.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment