1

I am new to NodeJS and started to learn by building a simple command line chat application. I have the following code for Server and Client. Client-Server communication is successful but I am not able to capture 'adduser' event from the client. Please tell me where I am going wrong.

Server:

var net = require('net');

var chatServer = net.createServer(function(socket){
    socket.pipe(socket);
}),
    userName="";

chatServer.on('connection',function(client){
    console.log("ChatterBox Server\n");
    client.write("Welcome to ChatterBox!\n");
    client.on('data',function(data){
        console.log(""+data);
    });
    client.on('adduser',function(n){
        console.log("UserName: "+ n);
        userName = n;
    });
});

chatServer.listen(2708);

Client:

var net = require('net');
var client = new net.Socket();
client.connect(2708,'127.0.0.1');
client.on('connect',function(){
    client.emit('adduser',"UserName");
});
console.log("Client Connected!\n");
client.on('data',function(data){
    console.log(""+data);
});
Prashaanth
  • 41
  • 1
  • 4
  • I think this answers your question: https://stackoverflow.com/questions/6933646/socket-emit-in-a-simple-tcp-server-written-in-nodejs – Derek Brown Oct 17 '17 at 02:03
  • You can use the plugin for chat application. Here is the link https://www.npmjs.com/package/rs-chat – Ravi Dec 17 '18 at 05:34

1 Answers1

0

I guess you don't have to do from the client side :

client.connect(2708,'127.0.0.1');

Just write your client like this is sufficient.

var net = require('net');
var client = new net.Socket();

client.connect(2708, '127.0.0.1',function(){
    console.log("Client Connected!\n");
    client.emit('adduser',"UserName");
});

client.on('data',function(data){
    console.log(""+data);
});

client.on('close', function() {
    console.log('Connection closed');
});

So the server side :

var net = require('net');
var sockets = [];
var port = 2708;
var guestId = 0;

var server = net.createServer(function(socket) {
    // Increment
    guestId++;

    socket.nickname = "Guest" + guestId;
    var userName = socket.nickname;

    sockets.push(socket);

    // Log it to the server output
    console.log(userName + ' joined this chat.');

    // Welcome user to the socket
    socket.write("Welcome to telnet chat!\n");

    // Broadcast to others excluding this socket
    broadcast(userName, userName + ' joined this chat.\n');

    socket.on('adduser',function(n){
        console.log("UserName: "+ n);
        userName = 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);

});

So you've got an object "users" inside the "user" when is connected, push user to the array users but you need to do (server side) on('close', ... to remove the user from users when connected is false ... etc

  • Thanks for your insight on this. I did the same thing but the "adduser" event emitted in client is not captured in Server to give a name for the connected client. – Prashaanth Oct 17 '17 at 06:23
  • @Prashaanth i was thinking about socket.io and i dunno if this net work similar, try this one if adduser isn't running so the reason is : isn't socket.io and the lib NET can't add new event emitter, the solution switch from data and add a object with multi criteria to catch your data. –  Oct 17 '17 at 07:25
  • this will possibly give us answer. https://stackoverflow.com/questions/6933646/socket-emit-in-a-simple-tcp-server-written-in-nodejs – Prashaanth Oct 17 '17 at 07:29
  • I guess if you can't take you data by using new emitter server client side you have only one solution take your datas by using on('data', ... this solution is using **telnet** , socket.io use **real websocket TCP** and the api get more information directly form the socket API. On final words switch socket.io is very simple and you can add what you want as new emitter and mutch example can give you fast resultat. –  Oct 17 '17 at 07:36
  • I have to delete this answer ;) gl for the next. –  Oct 17 '17 at 07:40