Skip to content

Instantly share code, notes, and snippets.

@creationix
Created November 19, 2010 20:47
Show Gist options
  • Save creationix/707146 to your computer and use it in GitHub Desktop.
Save creationix/707146 to your computer and use it in GitHub Desktop.
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");
@myeac
Copy link

myeac commented Mar 2, 2018

how can i test this code?? im really new with Node.js and i'm trying to learn this Technology in order to créate a nodejs server that helps with the communication between 3 android devices. I will apreciate any help or recomendaciones. Thanks! :D

@hscheuerle
Copy link

This is great for learning the server-side. I couldn't figure out how to write in Node without an npm package like ws or websocket. I've always learned the most implementing production APIs with lower level libraries.

@vaghul, also looking to learn protocol messages by writing the headers myself. They are so thoroughly reused that there seems to be no reason for simple instructions to exist. Let me know what you found since your post if you see this.

@myeac, you probably figured it out by now, but check out any starter nodejs tutorials, its 'node ' to run node scripts.

@MarkSh1
Copy link

MarkSh1 commented May 17, 2018

I think it will be better for low speed users :

socket.on('myEvent', function (message)
{
  socket.write(message);
}
);
...........
// 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.emit('myEvent', message);
  });
 // Log it to the server output too  
  process.stdout.write(message)
}

@codedokode
Copy link

codedokode commented May 30, 2018

Found this using Google. Sorry, but this code has two important mistakes. The main mistake is that when client sends a message to the chat, it won't come as a single packet. It can be broken into parts (and multibyte characters can be broken into bytes) and on('data', ...) callback would be called several times. That is because TCP is a stream-oriented protocol and doesn't have a concept of "messages". To fix this, you need some reader that will read data from the socket, collect it (it is called "to buffer the data"), split at new lines and emit line-by-line. Luckily, there is a library for that: carrier:

    var lineReader = carrier.carry(socket);
    lineReader.on('line',  function(line) {
        console.log('got one line: ' + line);
    });

Second, you don't handle errors. There always can be errors while working with network, something can fail. Node.JS by default doesn't report these errors so it is your responsibility. Without displaying them it would be really difficult to debug your program. You won't even know that something was wrong. So you should add a code like this:

socket.on('error', function (e) {
    console.log("Error on client socket " + socket.name);
    console.log(e);
});

The same should be done for server socket that accepts connections:

var serverSocket = net.createServer(...);
serverSocket.on('error', function (e) {
    console.log("Error on server socket");
    console.log(e);
});

References:

Update: as I have read in Node docs, you don't have to handle errors. If you don't set an error handler, any error will just crash your script so you will know about it. But if you want your server to continue working if there is problem with a single client then you should write an error handler that prints the error and closes broken client's connection.

@bhxlla
Copy link

bhxlla commented Jul 24, 2018

Can we actually connect two individual users by their IP addresses by this ?
Or is this for the same IP but multiple ports acting as different Clients...?

@AliSawari
Copy link

Nice!!
I was implementing this then I ran across this Gist.
see here

@adrwh
Copy link

adrwh commented Feb 17, 2019

This is cool, can it be re-written to use pipe() instead of listening to events?

@TeSsErAcT25
Copy link

TeSsErAcT25 commented Jun 21, 2019

To All those who are complaining That this code is buggy
This is only the server code
You have to write the Client code by yourself to make this run properly
Here is a simple Client Code

var net = require('net')
var client = net.connect({port : 5000},function(){
    console.log('connected to the server')
})

client.on('data',function(data){
    console.log(data.toString())
    //client.end()
})
client.write("Hello !!!!")

@creationix
Copy link
Author

Also note that this code was written almost a decade ago! The node.js APIs and best practices have changed significantly since then.

@ahmednawazkhan
Copy link

ahmednawazkhan commented Oct 2, 2019

Hi. I just want to open a socket that accepts tcp traffic. and when I telnet to that port I get notified ( may be console.log ). Is it even possible to telnet into a opened socket?

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