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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:29:10+00:00 2026-05-29T05:29:10+00:00

I want to send data to the specified user, and no one else. currently

  • 0

I want to send data to the specified user, and no one else.

currently to broadcast to everyone I do:

socket.on('newChatMessage', function (message) {
socket.broadcast.emit('newChatMessage_response', { data: message});
});

now what I want to do is simply something like:

socket.on('newChatMessage', function (message) {
socket.to(5).send('newChatMessage_response', { data: message});
});

to(5) would be the user with the ID of 5.

  • 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-29T05:29:11+00:00Added an answer on May 29, 2026 at 5:29 am

    I think the missing piece for you is inside this function: io.sockets.on('connection', function(socket) { ... });– That function executes every time there is a connection event. And every time that event fires, the ‘socket’ argument references a brand new socket object, representing the newly connected user. You then need to store that socket object somewhere in a way that you can look it up later, to call either the send or emit method on it, if you need to send messages that will be seen by only that socket.

    Here’s a quick example that should get you started. (Note: I haven’t tested this, nor do I recommend that it’s the best way to do what you’re trying to do. I just wrote it up in the answer editor to give you an idea of how you could do what you’re looking to do)

    var allSockets = [];
    io.sockets.on('connection', function(socket) {
      var userId = allSockets.push(socket);
      socket.on('newChatMessage', function(message) {
        socket.broadcast.emit('newChatMessage_response', {data: message});
      });
      socket.on('privateChatMessage', function(message, toId) {
        allSockets[toId-1].emit('newPrivateMessage_response', {data: message});
      });
      socket.broadcast.emit('newUserArrival', 'New user arrived with id of: ' + userId);
    });
    

    Edit: If by id you’re referring to the id value that socket.io assigns, not a value that you’ve assigned, you can get a socket with a specific id value using var socket = io.sockets.sockets['1981672396123987']; (above syntax tested using socket.io v0.7.9)

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

Sidebar

Related Questions

I want to send ViewData.model to Special action with the last user data entry.
I want to send data through URL.I've tried the below code: Intent browserIntent =
I want to send data within a WLAN as fast as possible to another
Hi I want to send data to the server and then have the php
I created the list that have device detected, and I want to send data
I want to constantly send data to the iPod. But how can I do
Hello Sir i want send list of data to php server i use following
In my program, I stat the files they want and send the data over.
I want to send the form data to server-side. I have two ways: Normal
In my code I want to send two parameters in my data. Name is

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.