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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:19:05+00:00 2026-06-16T01:19:05+00:00

So I have an application running node js with socket.io as a backend and

  • 0

So I have an application running node js with socket.io as a backend and normal javascript as frontend. My application has a login system which currently simply has the client send its login data as soon as it’s connected.

Now I figured it would be much nicer to have the login data sent along with the handshakeData, so I can directly have the user logged in while connecting (instead of after establishing a connection) respectively refuse authorization when the login data is invalid.

I’m thinking it would be best to put my additional data in the header part of the handshakeData, so any ideas how I could do that? (Without having to modify socket.io if possible, but if it’s the only way I can live with it)

  • 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-16T01:19:06+00:00Added an answer on June 16, 2026 at 1:19 am

    As a lot of comments have pointed out below the Socket.IO API changed in their 1.0 release. Authentication should now be done via a middleware function, see ‘Authentication differences’ @ http://socket.io/docs/migrating-from-0-9/#authentication-differences. I’ll include my orginal answer for anyone stuck on <1.0 as the old docs seem to be gone.

    1.0 and later:

    Client Side:

    //The query member of the options object is passed to the server on connection and parsed as a CGI style Querystring.
    var socket = io("http://127.0.0.1:3000/", { query: "foo=bar" });
    

    Server Side:

    io.use(function(socket, next){
        console.log("Query: ", socket.handshake.query);
        // return the result of next() to accept the connection.
        if (socket.handshake.query.foo == "bar") {
            return next();
        }
        // call next() with an Error if you need to reject the connection.
        next(new Error('Authentication error'));
    });
    

    Pre 1.0

    You can pass a query: param in the second argument to connect() on the client side which will be available on the server in the authorization method.

    I’ve just been testing it. On the client I have:

    var c = io.connect('http://127.0.0.1:3000/', { query: "foo=bar" });
    

    On the server:

    io.set('authorization', function (handshakeData, cb) {
        console.log('Auth: ', handshakeData.query);
        cb(null, true);
    });
    

    The output on the server then looked like:

    :!node node_app/main.js
       info  - socket.io started
    Auth:  { foo: 'bar', t: '1355859917678' }
    

    Update

    3.x and later

    You can pass an authentication payload using the auth param as the second argument to connect() in the client side.

    Client Side:

    io.connect("http://127.0.0.1:3000/", {
        auth: {
          token: "AuthToken",
        },
      }),
    

    In server side you can access it using socket.handshake.auth.token

    Server Side:

    io.use(function(socket, next){
        console.log(socket.handshake.auth.token)
        next()
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java EE web application which is running on 2 node cluster
I have an application which has been running happily under Java 1.5 for around
I have a Node.JS application running on Linux at AWS EC2 that uses the
I have a currently running PHP application that I want to add real-time feed
So far I have had a single node.js app. running socket.io. As number of
I'm running a node.js application which is acting as a man-in-the-middle proxy to proxy
I have an application up and running on Heroku with Express.js on Node.js with
I have some socket.io node.js servers running as amazon instances, when connecting directly to
I have a windows server running IIS and node.js with socket.io for an interactive
I'm developing a Winforms application which has been running for years with an explorer

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.