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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:41:11+00:00 2026-05-27T14:41:11+00:00

Straight to the point: I am using node.js, socket.io and redis for a private

  • 0

Straight to the point:
I am using node.js, socket.io and redis for a private chat system.

On connect user passes his website id (userID) to node.js server. He may have multiple connections so I have to pair socketID (of each connection) and userID somehow. I has thinking about using redis to store userID->sessionID pairs. However, when user disconnects I need to remove that pair from redis.. but I have only socketID not userID so I can’t select by that key..

Now, am I approaching this the wrong way or should I store both userID->socketID and socketID->userID pairs? Maybe someone could offer more elegant solution?

  • 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-27T14:41:11+00:00Added an answer on May 27, 2026 at 2:41 pm

    A more elegant solution would be to make each socket connect to the channel userID, for example:

    io.sockets.on('connection', function (socket) {
      socket.join(userID);
    });
    
    // when you want somebody to send a message to userID you can do:
    io.sockets.in(userID).emit(message);
    

    There are two things you need to take care of here:

    1. Make sure that only userID can connect to his channel, thus verify the session ( read more here: http://www.danielbaulig.de/socket-ioexpress/ )
    2. On connection increase the value for userID in redis (so that you know a new connection for that user is listening) and on disconnect decrease the userID value (so that you know the number of connections still listening). If the value is 0 then you emit a message to the chat stating that userID has left (since the number of connections listening to the userID channel is 0).

    When other users will want to send a message to userID, they don’t need to connect to the userID channel, they can send a message to the chat channel and pass userID as a property. For example:

    var chat = io
      .of('/chat')
      .on('connection', function (socket) {
        // connected to public chat
      })
      .on('message', function (data) {
        if (data.userID && data.message) {
          io.sockets.in(userID).emit('UserX: ' + data.message);
        }
      });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'll get straight to the point. I'm building a website on an existing database,
I'll get straight to the point, I am using jQuery to clone a div
This is a pretty straight forward attempt. I haven't been using python for too
I'm doing some straight up asynchronous calls from javascript using the XMLHTTPRequest object. On
Have you tried using straight javascript (rather straight ECMAscript 3 standard) in Flex applications,
Straight to the point: I have this javascript: for(item=1;item<5;item++) { xmlhttp=new XMLHttpRequest(); xmlhttp.open(GET,'zzz.php', true);
Hello all C++ Experts, It's me again. I will get straight to the point.
I'm trying to create a user interface using Google Web Toolkit v2.4. For a
Let me get my question straight, using the @OnDelete here will delete this and
I'm currenlty using SSIS for some process flow, scripting, and straight data import. Most

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.