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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:23:19+00:00 2026-06-08T00:23:19+00:00

so I am inserting a document into mongo db as such io.sockets.on(‘connection’, function (socket)

  • 0

so I am inserting a document into mongo db as such

io.sockets.on('connection', function (socket) {
    connectLog(socket.handshake.address.address);

function connectLog(ipAddress) {
   db.collection('tracking', function (err, collection) {
        var currentTime = new Date();
        var doc={"ip":ipAddress, "connect": currentTime, "culture": "en" };
        collection.insert(doc, function () { });
    });

}

I have another event

function logout(id, disconnect) {

I’d like to update (or replace?) that record and add disconnect: (time) to it. How would I go about doing this? This way I can tell when a person connects and when they disconnect from the chat.

I am using socket.io so I’ll know their exact disconnect time

Thank you in advance

  • 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-08T00:23:21+00:00Added an answer on June 8, 2026 at 12:23 am

    First, read this about explicit vs. implicit disconnects: Socket.io: How to handle closing connections?. Basically, your handler for explicit logouts (which are good!) should call the same code as your disconnect handler, in case the user doesn’t get a chance to explicitly logout.

    So in addition to your logout code, you’ll also want:

    socket.on('disconnect', handleDisconnect)
    

    And in that disconnect/logout handler, you’ll want to find the most recent connection document for that user and update it.

    collection.findAndModify(
     {"address" : address}, //same address 
     [['connect', 'descending']], //the most recent, findAndModify only changes the first doc
     {$set: {disconnect: currentTime}}, //set the disconnect time
     function(err, object){/*deal with errors*/}
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When inserting copy into an HTML document I get from sources such as word
I have a problem inserting the values from checkboxes in an html document into
I'm inserting an img tag into my document with the new Element constructor like
I'm basically inserting information from a dataset into an excel document. We really don't
Has anyone had problems inserting a PNG file into a word document, using a
I'm dynamically inserting some html into the document (by using obj.innerHTML += 'some html').
I have a question on inserting images into a LaTeX document. I try to
For example, there are 5 object stores. I am thinking of inserting documents into
I'm inserting documents using node mongodb native driver into a mongodb (sic!). My objects
When inserting a row into a table with an auto increment primary key column,

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.