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

  • Home
  • SEARCH
  • 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 6125217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:13:20+00:00 2026-05-23T16:13:20+00:00

When making requests in Node.JS to another HTTP server, you can listen for when

  • 0

When making requests in Node.JS to another HTTP server, you can listen for when the server closes the connection with request.on('close', function(){});. I’ve been searching for similar functionality within Node’s http.createServer(). I assumed that that request variable passed in to the callback had an on() function which I could call to listen for when the client closes the connection.

var http = require('http');
http.createServer(function(req, res) {
    req.on('close', function() { console.log("Connection closed"); });
}).listen(80, '127.0.0.1');

However, after reading the 0.4.9 docs on streams, I noticed that under the close event it said:

Not all streams will emit this. (For example, an incoming HTTP request will not emit ‘close’.)

Is there a way to listen for client connection terminations from the server in Node?

  • 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-23T16:13:21+00:00Added an answer on May 23, 2026 at 4:13 pm

    Well, you could access the socket directly by doing:

    req.socket.on('close', ...);
    

    However, doing this inside a request listener will bind the event for every request. So instead you should bind to the connection event:

    var server = http.createServer();
    server.on('request', function(req, res) { ... });
    server.on('connection', function(socket) {
      socket.on('close', function() {
        console.log(socket.remoteAddress + '\'s keep-alive session died!');
      });
    });
    

    EDIT:
    Maybe I should have mentioned, yes, technically the socket object for an http server will carry a reference to the response on the _httpMessage property. However, I would advise against using this as it is part of the internal API and is subject to change without any warning.

    It would be better to add your own property to get a reference to the last request made:

    server.on('request', function(req, res) {
      req.socket.currentRequest = req;
    });
    server.on('connection', function(socket) {
      socket.on('close', function() {
        var req = socket.currentRequest;
        console.log('Socket closed. Last request for: ' + req.url);
      });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making an IPhone app in which userid and password is required in
I'm making a program for registration on a website. For this, I use C++Builder
ASP.NET does not allow concurrent requests for the same session; meaning that a user
I need to make application that needs to poll server often, but GAE has
I need to have a MsSql database table and another 8 (identical) processes accessing
Hey, I'm trying to implement a ConcurrentQueue in C# for an asynchronous server. Items
I am considering utilizing Python to call various dlls that will perform things like
Alright I'm going to create a fairly complex form to post via AJAX a
I'm playing around with a webserver, using a unix socket and sendmsg / recvmsg
I am running a Facebook application that asks users questions. When they complete one

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.