Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A simple TCP based chat server written in node.js
// Load the TCP Library
net = require('net');
// Keep track of the chat clients
var clients = [];
// Start a TCP Server
net.createServer(function (socket) {
// Identify this client
socket.name = socket.remoteAddress + ":" + socket.remotePort
// Put this new client in the list
clients.push(socket);
// Send a nice welcome message and announce
socket.write("Welcome " + socket.name + "\n");
broadcast(socket.name + " joined the chat\n", socket);
// Handle incoming messages from clients.
socket.on('data', function (data) {
broadcast(socket.name + "> " + data, socket);
});
// Remove the client from the list when it leaves
socket.on('end', function () {
clients.splice(clients.indexOf(socket), 1);
broadcast(socket.name + " left the chat.\n");
});
// Send a message to all clients
function broadcast(message, sender) {
clients.forEach(function (client) {
// Don't want to send it to sender
if (client === sender) return;
client.write(message);
});
// Log it to the server output too
process.stdout.write(message)
}
}).listen(5000);
// Put a friendly message on the terminal of the server.
console.log("Chat server running at port 5000\n");

hey, i wrote almost the same code myself, am new to node.js. But what is happening is when i use it via telnet, the server transmits each keystroke at a time than whole sentences. doesnt this happen with your code?

is there a missing semicolon at:

// Identify this client
socket.name = socket.remoteAddress + ":" + socket.remotePort

?

yea

@joseprl89 JavaScript doesn't need semicolons, it works fine.

iamtrk commented Jul 10, 2013

hey nimish ,What actually happening in your case is you are transmitting the data to your self also besides other sockets. You need to remove yourself from the list while writing the data.
if (client === sender) return;does the work in the case of above chat server code.

murataka commented Nov 1, 2013

buggy

socket.on('data', function (data) {
broadcast(socket.name + "> " + data, socket);
});

if you broadcast every time the datas when received, you'll have a loop of broadcast :\

mark broadcast "abc" to rick
rick broadcast "abc" to mark
{loop}

I'd like to know if it is possible to make the client in node, instead of using telnet.

Thanks. I am new to node.js socket. Your code does help me to understand how to use.

@xeryan have you tested the code?

Because I did and could not reproduce what you've said.

Try running the server and then connecting via telnet, at least with two clients simultaneously.

Btw, the server will not (endlessly) loop the messages because it uses each socket to broadcast the messages. And as we know, each socket is linked to one client only.

Thanks for this creationix. Didn't realise how simple it was to create a TCP chat server. I put together an implementation based on yours with some tweaks in my gist–would be great to get your thoughts on it–https://gist.github.com/graphicbeacon/10422384

How we can use this code for send message to single user only?

Nice code! Thanks.
what if i want to close connection of a client in server by force? I want to authenticate my users and if it failed close connection!

I am seeing some special character in the first chat message! Seems like data is not set for the first time or it required some decoding because it shows lot of special characters. After one message is sent, it becomes okay.
PS: I am using putty as the telnet client.

vaghul commented Mar 22, 2016

Hi ,
Is there a way to send custom values on the handshake process ? i.e send custom data only when connecting the first time.

@nimish: If you are using a windows telnet client this will surely happen. Use some other telnet client which buffers the keystrokes until you press ENTER like telnet in Linux

i have a version working with line-by-line chats for windows telnet if anyone in interested...

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