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 8586977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:27:04+00:00 2026-06-11T22:27:04+00:00

I am trying to learn about streams in node.js! server.js var net = require(net);

  • 0

I am trying to learn about streams in node.js!

server.js

var net = require("net");

var server = net.createServer(function(conn) {

  conn.write("welcome!");

  # echo the user input!
  conn.pipe(conn);
});

server.listen("1111", function() {
  console.log("port 1111 opened");
});

telnet test

The server currently echos the user’s input

$ telnet localhost 1111
welcome!
hello
hello

desired output

To demonstrate where/how I should process the stream on the server side, I would like to wrap the user’s input in {} before echoing it back

$ telnet localhost 1111
welcome!
hello
{hello}
  • 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-06-11T22:27:06+00:00Added an answer on June 11, 2026 at 10:27 pm

    This will basically accomplish the exact output you’ve requested:

    var net = require('net');
    
    var server = net.createServer(function(c) {
        c.setEncoding('utf8');
        c.on('data', function(d) {
            c.write('{' + d.trim() + '}\n');
        });
    });
    
    server.listen(9871);
    

    First let me call your attention to c.setEncoding('utf8'). This will set a flag on the connection that will automatically convert the incoming Buffer to a String in the utf8 space. This works well for your example, but just note that for improved performance between Sockets it would be better to perform Buffer manipulations.

    Simulating the entirety of .pipe() will take a bit more code.

    .pipe() is a method of the Stream prototype, which can be found in lib/stream.js. If you take a look at the file you’ll see quite a bit more code than what I’ve shown above. For demonstration, here’s an excerpt:

    function ondata(chunk) {
      if (dest.writable) {
          if (false === dest.write(chunk) && source.pause) {
              source.pause();
          }
      }
    }
    
    source.on('data', ondata);
    

    First a check is made if the destination is writable. If not, then there is no reason to attempt writing the data. Next the check if dest.write === false. From the documentation:

    [.write] returns true if the entire data was flushed successfully to the kernel buffer. Returns false if all or part of the data was queued in user memory.

    Since Streams live in kernel space, outside of the v8 memory space, it is possible to crash your machine by filling up memory (instead of just crashing the node app). So checking if the message has drained is a safety prevention mechanism. If it hasn’t finished draining, then the source will be paused until the drain event is emitted. Here is the drain event:

    function ondrain() {
        if (source.readable && source.resume) {
            source.resume();
        }
    }
    
    dest.on('drain', ondrain);
    

    Now there is a lot more we could cover with how .pipe() handles errors, cleans up its own event emitters, etc. but I think we’ve covered the basics.

    Note: When sending a large string, it is possible that it will be sent in multiple packets. For this reason it may be necessary to do something like the following:

    var net = require('net');
    
    var server = net.createServer(function(c) {
        var tmp = '';
        c.setEncoding('utf8');
        c.on('data', function(d) {
            if (d.charCodeAt(d.length - 1) !== 10) {
                tmp += d;
            } else {
                c.write('{' + tmp + d.trim() + '}\n');
                tmp = '';
            }
        });
    });
    
    server.listen(9871);
    

    Here we use the assumption that the string is ended by the new line character (\n, or ascii character code 10). We check the end of the message to see if this is the case. If not, then we temporarily store the message from the connection until the new line character is received.

    This may not be a problem for your application, but thought it would be worth noting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to learn about applets by studying the code for the Welcome
I'm trying to learn about caching in asp.net and am having difficulty understanding the
Trying to learn about php's arrays today. I have a set of arrays like
I'm trying learn more about RavenDB and at the moment to focus on how
I am trying to learn about the ScriptProperties object in GAS. I tried setting
I've been trying to learn about metaclasses in Python. I get the main idea,
I'm trying to learn about this feature of javascript I keep seeing in code,
I am currently trying to learn about OpenGL ES for the iPhone and following
I'm trying to learn about condition variables and how to use it in a
I'm trying to learn about openDatabase, and I think I'm getting it to INSERT

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.