Skip to content

Instantly share code, notes, and snippets.

@graphicbeacon
Last active October 10, 2023 16:37
Show Gist options
  • Save graphicbeacon/10422384 to your computer and use it in GitHub Desktop.
Save graphicbeacon/10422384 to your computer and use it in GitHub Desktop.
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
Copy link

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

@kdamball
Copy link

@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
Copy link

rooton commented Dec 6, 2014

Hi.

Thats chat server, not client.

@graphicbeacon
Copy link
Author

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

@graphicbeacon
Copy link
Author

@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
Copy link

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.

@HalanoSiblee
Copy link

That is amazing Thanks

@micromotores
Copy link

how send message to especific guestId?? dont sent message to broadcast

@Raajmathankumardys
Copy link

xd

@Raajmathankumardys
Copy link

please give client-side code

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