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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:17:16+00:00 2026-06-03T14:17:16+00:00

I have followed this guide: http://www.danielbaulig.de/socket-ioexpress/ to link express.js to socket.io and it works

  • 0

I have followed this guide: http://www.danielbaulig.de/socket-ioexpress/ to link express.js to socket.io and it works brilliantly.

I have the user logging in on one page (express.js POST request that sets a session object) and when they have authenticated it directs them to a new page where socket.io loads and on the server socket.io can grab the session that was set from express. So that all works fine.

Now I have users on the page that uses socket.io and when that page is refreshed – sometimes the socket.io connection is still there (ie. it doesn’t disconnect). What I am trying to do is alter the io.set('authorization') function to ensure that when the user connects – it will disconnect all existing socket.io instances that are still open to that client.

Here is the how it looks at the moment:

//socket.io connect to express
var parseCookie = require('connect').utils.parseCookie;
io.set('authorization', function (data, accept) {
    if (data.headers.cookie) 
    {
        data.cookie = parseCookie(data.headers.cookie);
        data.sessionID = data.cookie['express.sid'];
        // (literally) get the session data from the session store
        sessionStore.get(data.sessionID, function (err, session) 
        {
            if (err || !session) 
            {
                // if we cannot grab a session, turn down the connection
                //note: a session is created when the user logs in
                accept('Error', false);
            } 
            else 
            {
                //TODO something here before we accept the connection
                //??

                // save the session data
                data.session = session;
                //accept the connection
                accept(null, true);
            }
        });
    } 
    else 
    {
        return accept('No cookie transmitted.', false);
    }
});

How do I check if the client that is about to be accepted has a previous connection, and it is still open (and therefore we need to disconnect that old one).

The express session has a “username” in it – so we can get the session object and get the username, I just can’t figure out how to go through the list of all the socket.io clients and have a look at the express session for each one and check if it the same as the user authenticating currently.

  • 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-03T14:17:17+00:00Added an answer on June 3, 2026 at 2:17 pm

    In my application, I explicitly manage the socket.io sessions. On the server side, whenever a new connection is established, I do something like: socketio_session[user_id].push(session). This gives me access to all the sessions connected for a particular user. In your case, you may not need to store a list of sessions per user, but only store the latest session and onconnect force disconnect on the existing session, if any, before storing the new session.

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

Sidebar

Related Questions

I am using javascript to make XML to HTML. I have followed this guide:http://www.w3schools.com/xml/xml_to_html.asp
I wanted a sticky footer to I followed this guide: http://www.cssstickyfooter.com/using-sticky-footer-code.html I have in
My site was working fine until I followed this guide, http://www.howtoforge.com/how-to-set-up-apache2-with-mod_fcgid-and-php5-on-ubuntu-9.10 to set up
I followed this guide to display data from mysql database: http://www.flashrealtime.com/flash-builder-4-and-php-data-services/ But what to
I followed this guide here perfectly and have gone through it again but when
I followed this tutorial http://www.tugberkugurlu.com/archive/api-key-authorization-through-query-string-in-asp-net-web-api-authorizationfilterattribute to create custom Authorization filter. I have CarController with
I've followed through this( http://goo.gl/1LTX ) guide to setup FBA in SP 2010, but
I followed the guide on the very bottom of this article: http://symfony.com/doc/current/cookbook/assetic/asset_management.html I have
I have followed the tutorial at: http://www.15seconds.com/issue/040331.htm for making a BHO, however i doesnt
I have my moodle setup using this guide ( Ubuntu 11.04 ) http://docs.moodle.org/dev/Install_Moodle_On_Ubuntu_with_Nginx/PHP-fpm I

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.