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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:50:59+00:00 2026-06-10T08:50:59+00:00

I’m having an issue with Socket.io receiving messages just before a page navigation happens

  • 0

I’m having an issue with Socket.io receiving messages just before a page navigation happens – generally when the message is a direct result of some server-side action triggered by the navigation.

What I’m seeing right now looks like this:

  1. Socket.io connects
  2. User triggers a page navigation (submits a form, refreshes, etc.)
  3. Server-side logic sends a request to the socket.io server, which immediately dispatches the event to the still-connected client
  4. The client receives and confirms the request (I’m pretty sure there’s some confirmation of messages built-in to socket.io, correct me if I’m wrong), and would display a notification to the user, but then …
  5. The socket.io connection closes on the original page
  6. The new page loads and displays
  7. Socket.io opens a new connection, but there’s no new messages, because the last one was received and confirmed.

This isn’t really a bug, per se, since I don’t think it’s reasonable to expect Socket.io to close the connection in advance of a navigation occurring. However, I’m not really sure what the best way to handle this is. Currently I keep one connection open per-client at a time, and close the other when a new one connects. This doesn’t happen in this case though, since the first one has closed before the second one connects. I also could keep a list of all clients, but that wouldn’t solve this problem either, because the message would still be received by the first connection.

Can someone suggest a solution to this problem that would ensure the user always sees a notification for the message?

  • 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-10T08:51:01+00:00Added an answer on June 10, 2026 at 8:51 am

    Socket.io tracks logical connections with its own session IDs. If you watch the console when a client connects, you’ll see the IDs:

    info  - handshake authorized Q9syoIK47JI7dACYpxiA
    

    What’s important to understand is that those IDs are per-page, and completely separate from HTTP sessions. The Socket.io client library simply holds its session ID in a JavaScript variable. Therefore, upon navigation, the ID is obviously lost.

    So, upon navigation, this happens:

    1. User is on a page connected to sio session 1.
    2. We begin navigation to new page. On window.onbeforeunload, Socket.io initiates a synchronous XHR request to tell the server it is disconnecting. If it succeeds, the session (1) is immediately terminated; otherwise, the session will eventually time out.
    3. A new page is loaded. It will connect to the Socket.io server and be assigned a new session ID, 2.
    4. Anything you send to session 1 will obviously not be delivered since our client is now connected to session 2.

    With the base Socket.io functionality, it is impossible to distinguish between a user who navigates between pages and a new user. In either case, the user will connect to a new Socket.io session.

    Without knowing exactly how your app works or what you’re trying to accomplish, it’s hard to give a definitive recommendation for solving your problem.

    Most likely, what you need to be able to do is associate a Socket.io session with the user’s HTTP session. You can store notifications in a queue in the user’s session, and delete them when they are displayed. There are two ways of doing this:

    • Since you’re doing a full page load, you can send queued notifications directly down with the page itself. Delete the queue when you’ve successfully rendered.
    • On a new Socket.io connection, send unread notifications over the socket. Give .emit a callback function – this is the confirmation of delivery that Socket.io provides you. When delivery is confirmed, you can delete the notification queue from the user’s HTTP session.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I'm making a simple page using Google Maps API 3. My first. One marker

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.