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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:50:05+00:00 2026-05-27T01:50:05+00:00

Is it possible for client to send a socket data when it gets disconnected

  • 0

Is it possible for client to send a socket data when it gets
disconnected (user closes the browser, or tab)?
Because, I found that default bind on disconnect doesn’t take any input data.

//on server side 
socket.on('disconnect',function(){ 
    //in other binds, it takes 'data' as an input but this does not 
}); 

I tried to resolve this problem by using the jQuery’s unload method
like below

//on client side 
$(window).unload(function(){ 
    socket.emit('depart','I am leaving'); 
}); 

However, it does not seem to emit at all on unload. (maybe socket gets out of scope on unload?)
Is there a way to send data through websocket to the server when client disconnects?

Secondly, Is there any way that I can store a string data in socket? For instance,
socket.userName = userNameVar;

–update–
Here is how I set up environment on the serverside

io.sockets.on('connection',function(socket){
    socket.on('depart',function(data){
        console.log(data);
        console.log("user left");
    });
    socket.on('disconnect',function(){
        console.log("user disconnected");
    });
});

and following code is in the client side.

var socket = io.connect(hostVar);

$(window).unload(function(){ 
    socket.emit('depart','I am leaving'); 
}); 
  • 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-27T01:50:06+00:00Added an answer on May 27, 2026 at 1:50 am

    The reason you cannot send data on disconnect is because you are disconnected. What you are asking is somewhat akin to saying “Can i still talk to the person on the phone after they’ve hung up on me?”

    If you want to emit the ‘depart’ event, then you still have to capture it inside your ‘connect’ handler on server side:

    As in:

    io.socket.on('connection', function(socket) {
          socket.on('depart', function(msg) {
                // you should get depart message here
    
          });
    })
    

    Your depart event will come BEFORE the connection is terminated. disconnect is only raised by server upon a successful disconnection and therefore it cannot receive any messages.

    Give it a try and tell me if this works 🙂

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

Sidebar

Related Questions

Is it possible to have XML-embedded JavaScript executed to assist in client-side (browser-based) XSL
Possible Duplicate: Java socket sends some data during connection to server I have two
Is it possible to create a service on Azure that handles a socket (ip:port)
Is it possible to have a server and a client with 1 socket connection
I send binary data from client (Debian 6.0.3) to server (Windows Server 2003). To
In ASIO, s it possible to create another socket that has the same source
Possible Duplicate: Client Id for Property (ASP.Net MVC) In my View I'm using jquery
Is it possible for a client to pass an RMI object as an argument
I need to do as much as possible on the client side. In more
My client would like a business application to support 'every possible device'. The application

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.