Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6222389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:18:18+00:00 2026-05-24T08:18:18+00:00

For education purposes I am creating a little chat with node.js using TCP. I

  • 0

For education purposes I am creating a little chat with node.js using TCP.

I am using the windows console to connect with my node server but when I am typing all the characters are streamed one by one. They don’t arive as strings. How can I manage to handle those streams so my users don’t can write complete words.

My Code:

var net = require("net");

Array.prototype.remove = function(e) {
  for (var i = 0; i < this.length; i++) {
    if (e == this[i]) { return this.splice(i, 1); }
  }
};

function Chatter(stream) {
  this.name = null;
  this.stream = stream;
}

var chatters = [];

var server = net.createServer(function(stream) {
    var chatter = new Chatter(stream);
    chatters.push(chatter);

    stream.setTimeout(0);
    stream.setEncoding("utf8");

    stream.addListener("connect", function(){
        stream.write("Hallo, wer bist du?:\n");
    });

    stream.addListener("data", function (data) {
        if(chatter.name == null) {
            chatter.name = data.match(/\S+/);
            stream.write("....................\n");
            chatters.forEach(function(c){
                if (c != chatter) {
                    c.stream.write(chatter.name + " ist dem Chat beigetreten!\n");
                }
            });
            return;
        }

        var command = data.match(/^\/(.*)/);
        if (command) {
            if (command[1] == 'users') {
                chatters.forEach(function(c) {
                    stream.write("- " + c.name + "\n");
                });
            }
            else if (command[1] == 'quit') {
                stream.end();
            }
        }

        chatters.forEach(function(c) {
            if(c != chatter) {
                c.stream.write(chatter.name + ": " + data);
            }
        });
    });

    stream.addListener("end", function(){
        chatters.remove(chatter);
        chatters.forEach(function(c) {
            c.stream.write(chatter.name + " hat den Chat verlassen.\n");
        });

    stream.end();
    });
});

server.listen(8000);

For the record that code is from this site


ADDITION:

setEncoding('utf8') is supposed to change the emiting of data, but it doesn’t work for me 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:18:18+00:00Added an answer on May 24, 2026 at 8:18 am

    The solution to your problem is to store all received characters in a buffer and when an END_OF_NICK character is encountered (say, \n), use the buffer as the name.

    var buffer = ""; // stores received characters
    stream.addListener("data", function (data) {
        if(chatter.name == null) { // still receiving characters for the name
            buffer += data; // append received characters to the buffer
            if (buffer.indexOf('\n') == -1) return; // if there's no END_OF_NICK character, keep waiting for it
            chatter.name = buffer.match(/\S+/); // use the name in the buffer
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently creating a website a little bit like Digg.com. There are different category
I am creating an RSS reader purely for my own educational purposes and am
Hey Simple little app for educational purposes, its a simple uitableview and I want
I am implementing a Harris corner detector for educational purposes but I'm stuck at
In educational purposes I'm writing a HTTP server in C++. When receiving a request,
I am writing a super tiny web server for educational purposes. For the following
For my self-education purposes, I would like to investigate the code of a complex
I'm making a simple boost::any -like class for educational purposes, but I can't figure
I'm trying to make canvas draw code for the purposes of electronics education. Please
I'm writing a simple todo app for educational purposes. I store all todos in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.