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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:45:31+00:00 2026-05-27T09:45:31+00:00

I can only seem emit messages to users when their socket id has been

  • 0

I can only seem emit messages to users when their socket id has been directly stored within the io.sockets.on(‘connect’) function. I don’t know why it doesn’t work when trying to store their socket id after they have logged in.

Working:

  var clients = {};
  /** A new socket connection has been accepted */
  io.sockets.on('connection', function (socket)
  {
    //Used to access session id
    var hs = socket.handshake;            
        clients[socket.id] = socket; // add the client data to the hash   
        users['brownj2'] = socket.id; // connected user with its socket.id

    socket.on('user-login', function(user){
      if(!users[username]){
        //users[username] = socket.id; // connected user with its socket.id
      }
    })

    socket.on('page-load', function(pid)
    {
      if(users['brownj2'])
      {        
        clients[users['brownj2']].emit('hello');
      }
      else
      {
        console.log("NOT FOUND BROWNJ2");
      }
    }
  }

Not working:

  var clients = {};
  /** A new socket connection has been accepted */
  io.sockets.on('connection', function (socket)
  {
    //Used to access session id
    var hs = socket.handshake;            
        clients[socket.id] = socket; // add the client data to the hash            

    socket.on('user-login', function(user){          
      if(!users['brownj2']){
        users['brownj2'] = socket.id; // connected user with its socket.id
      }
    })

    socket.on('page-load', function(pid)
    {
      if(users['brownj2'])
      {        
        clients[users['brownj2']].emit('hello');
      }
      else
      {
        console.log("NOT FOUND BROWNJ2");
      }
    }
  }

JavaScript Client-side code snippet

var socket = io.connect('');
socket.on("hello", function(){
  alert("Hi Jack");
  console.log("WEBSOCKET RECIEVED");
})

Solution: Thanks @alessioalex
I have had to remove the reference to socket.io from the login page, and add the following into io.sockets.on(‘connection’)

var hs = socket.handshake;

sessionStore.get(hs.sessionID, function(err, session){
  console.log("SESSION: " + util.inspect(session));

  clients[socket.id] = socket; // add the client data to the hash
  validUsers[session.username] = socket.id; // connected user with its socket.id
})
  • 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-27T09:45:32+00:00Added an answer on May 27, 2026 at 9:45 am

    There are some problems with your code, the first being you shouldn’t authenticate users through Socket.IO, you should make sure they can connect only after they authenticated. If you are using Express, then the following article can help you a lot: http://www.danielbaulig.de/socket-ioexpress/

    Also you should be avoiding sending messages like so, since that is part of Socket.IO internally and may change:

    io.sockets.socket(id).emit('hello');
    

    Instead (if you want to send a message to a specific client) it’s better to keep an object for example with the connected clients (and remove the client once disconnected):

    // the clients hash stores the sockets
    // the users hash stores the username of the connected user and its socket.id
    io.sockets.on('connection', function (socket) {
      // get the handshake and the session object
      var hs = socket.handshake;
      users[hs.session.username] = socket.id; // connected user with its socket.id
      clients[socket.id] = socket; // add the client data to the hash
      ...
      socket.on('disconnect', function () {
        delete clients[socket.id]; // remove the client from the array
        delete users[hs.session.username]; // remove connected user & socket.id
      });
    }
    
    // we want at some point to send a message to user 'alex'
    if (users['alex']) {
      // we get the socket.id for the user alex
      // and with that we can sent him a message using his socket (stored in clients)
      clients[users['alex']].emit("Hello Alex, how've you been");
    }
    

    Sure, io.sockets.socket(id) may work (didn’t actually test), but also it can be always changed as it’s part of the Socket.IO internals, so my solution above is more ‘safe’.

    Another thing you may want to change in your code on the client side is var socket = io.connect(''); with var socket = io.connect('http://localhost');, as we can see in the official example of Socket.IO here: http://socket.io/#how-to-use

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

Sidebar

Related Questions

Can't locate stored files, but they seem to be downloaded only for the first
This has been bugging me for the past several hours and I can't seem
My problem is that I can only seem to click the button once, if
I can only seem to find how to return arrays from my function. Here
I'm using cURL to scrape web pages but I can only seem to scrape
I can only seem to get them to come up as threaded. I'm using
In Visio 2007, I can only seem to set the cardinality of the Parent-to-child
I've looked at the documentation and can only seem to find a way to
I've looked a lot into this, but I can only seem to get webView
I can only see the JPanel added at last. Previous ones seem to be

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.