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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:54:20+00:00 2026-06-10T02:54:20+00:00

Starting from the official chat example of Node.js. The user is prompted to emit

  • 0

Starting from the official chat example of Node.js.
The user is prompted to emit his name to the server through ‘register’ (client.html):

            while (name == '') {
               name = prompt("What's your name?","");
            }                
            socket.emit('register', name );

The server receives the name. I want it to make the name as the identifier of the socket. So when I need to send a message to that user I send it to the socket having his name (Names are stored in a database for the info).
Changes will take place here (server.js):

  socket.on('register', function (name) {
      socket.set('nickname', name, function () {         
         // this kind of emit will send to all! :D
         io.sockets.emit('chat', {
            msg : "naay nag apil2! si " + name + '!', 
            msgr : "mr. server"
         });
      });
   });

I’m struggling making this works, because I can’t go any farer if I can’t identify the socket. So any help will be really appreciated.
Update: I understand that nickname is a parameter for the socket, so the question is more specifically: how to get the socket that has “Kyle” as a nickname to emit it a 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-10T02:54:21+00:00Added an answer on June 10, 2026 at 2:54 am

    Store your sockets in a structure like this:

    var allSockets = {
    
      // A storage object to hold the sockets
      sockets: {},
    
      // Adds a socket to the storage object so it can be located by name
      addSocket: function(socket, name) {
        this.sockets[name] = socket;
      },
    
      // Removes a socket from the storage object based on its name
      removeSocket: function(name) {
        if (this.sockets[name] !== undefined) {
          this.sockets[name] = null;
          delete this.sockets[name];
        }
      },
    
      // Returns a socket from the storage object based on its name
      // Throws an exception if the name is not valid
      getSocketByName: function(name) {
        if (this.sockets[name] !== undefined) {
          return this.sockets[name];
        } else {
          throw new Error("A socket with the name '"+name+"' does not exist");
        }
      }
    
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Starting from the official example for pie chart. I'm not able the change the
Starting from this official statement that all xyAndroid versions previous API 11 doesn't support
Quote from hib official docs: Starting with version 3.0.1, Hibernate added the SessionFactory.getCurrentSession() method.
Starting from this example: http://support.microsoft.com/kb/828736 I have tried to add a test function in
Starting from Windows Server 2003, Windows included a new tool which calculates the effective
how to remove a string starting from '@'? for example admin@admin.com, i want to
so i am displaying first letter of the each Name starting from A and
I'm starting with CakePHP and I am following the blog tutorial from the official
Starting from scratch with very little knowledge of .NET, how much ASP.NET should I
Starting from scratch is hard. How do you do it? I quite like color

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.