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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:44:59+00:00 2026-05-23T19:44:59+00:00

I have a simple server, you send it a command, it replies back with

  • 0

I have a simple server, you send it a command, it replies back with an \r\n delimited response.

So I tried to get a command( callback ) method on my client. Check out this simplified code snippet:

var net = require('net');

var Client = function() {
   this.data = "";
   this.stream = net.createConnection(port, host);

   this.stream.on('data', function( data ) {
        var self = this;

        this.data += data;
        self.process()            
   };

   this.process = function() {
       var _terminator = /^([^\r\n]*\r\n)/;

       while( results = _terminator.exec(this.data) ) {
            var line = results[1];
            this.data = this.data.slice(line.length);

            this.emit('response', data);
       };
   };

   this.sendCommand = function( command, callback ) {
       var self = this;

       var handler = function( data ) {
            self.removeListener('response', handler);

            callback && callback(data);
       }

       this.addListener('response', handler);

       this.stream.write(command);
   };

   this.command_1 = function( callback ) {
        this.sendCommand( 'test', callback );
   };

   this.command_2 = function( callback ) {
        this.sendCommand( 'test2', callback );
   };
}

So I am doing a client.command_1( function() {} ) and then a client.command_2( function() {}) but in the callback of my command_2 I am getting the response from command_1.

Is this the right way to implement such a thing?

  • 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-23T19:45:00+00:00Added an answer on May 23, 2026 at 7:45 pm

    When you execute

    client.command_1( function() { 1; } );
    client.command_2( function() { 2; } );
    

    you add both callbacks as ‘result’ listeners, and when emit('result') happens for the first time, both callback are called (then first callback removes itself from a list). You need to set callbacks on some kind of request object, not on a client.

    simple code on what happens in your client:

    var e = new EventEmitter();
    e.on('result', function() { console.log(1); });
    e.on('result', function() { console.log(2); });
    // ...
    e.emit('result'); // here we trigger both callbacks which result in printing "1\n2\n"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a simple client/server scenario with one method: // client code $client
I have a simple Rtsp Client...The client send Rtsp Commands to Rtsp Server and
I have a simple client-server app on android. the android service communicates with the
I have a simple client/server setup. The server is in C and the client
I have a simple UDP client server written in C++ on Ubuntu 9.10 where
I have a somewhat simple Client/Server solution running over C# remoting (System.Runtime.Remoting). The MarshalByRef
I am new to java NIO. I have to write a simple server client
I am trying to learn WCF. I have a simple client and server application
I need to send a (probably one) simple one-way command from client processes to
I have a very simple TCP server written in C. It runs indefinitely, waiting

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.